← Back to team overview

dolfin team mailing list archive

Re: xy format for solutions.

 



On 08/09/10 18:15, Anders Logg wrote:
Yes, why not. Could you send a sample output in the xy format?


I would suggest merging the xy and xyx classes.

Garth

--
Anders


On Wed, Sep 08, 2010 at 06:11:22PM +0100, ndlopes@xxxxxxxxx wrote:
Hi,

Is it possible to consider a new output format for 1D problems?
I'm using it for some time and now I've updated it to dolfin-0.9.9.

The solutions file contain a list of  x y  coordinates.
(analogous to the xyz format)
It could be used directly with Xgraphic (wich is part of Xd3d),   gnuplot or
with simple python/matplotlib  scripts for instance.

Hope it works.

Nuno Lopes


# Bazaar merge directive format 2 (Bazaar 0.90)
# revision_id: ndl@an9-20100908165948-xqvxgx0834mlrkpt
# target_branch: bzr+ssh://bazaar.launchpad.net/~dolfin-\
#   core/dolfin/main/
# testament_sha1: 1a4383b33310156a890260ef4c35ceba6d7930e8
# timestamp: 2010-09-08 18:01:16 +0100
# base_revision_id: johannr@xxxxxxxxx-20100908084437-u2bw3adyeepm4de3
#
# Begin patch
=== modified file 'dolfin/io/File.cpp'
--- dolfin/io/File.cpp	2010-09-06 15:19:48 +0000
+++ dolfin/io/File.cpp	2010-09-08 16:59:48 +0000
@@ -9,19 +9,20 @@
  // Modified by Ola Skavhaug 2009.
  //
  // First added:  2002-11-12
-// Last changed: 2010-09-06
+// Last changed: 2010-09-08

+#include<boost/filesystem.hpp>
  #include<fstream>
-#include<boost/filesystem.hpp>
+#include<dolfin/main/MPI.h>
+#include<dolfin/log/dolfin_log.h>
  #include<dolfin/function/Function.h>
-#include<dolfin/log/dolfin_log.h>
-#include<dolfin/main/MPI.h>
  #include "File.h"
  #include "XMLFile.h"
  #include "PythonFile.h"
  #include "VTKFile.h"
  #include "RAWFile.h"
  #include "XYZFile.h"
+#include "XYFile.h"
  #include "BinaryFile.h"

  using namespace dolfin;
@@ -34,14 +35,11 @@
    const std::string extension = boost::filesystem::extension(path);

    // Create directory if we have a parent path
-  if (path.has_parent_path())
+  if ( path.has_parent_path() )
    {
      const boost::filesystem::path directory = path.parent_path();
-    if (!boost::filesystem::is_directory(directory))
-    {
-      cout<<  "Creating directory \""<<  directory.string()<<  "\"."<<  endl;
-      boost::filesystem::create_directories(directory);
-    }
+    cout<<  "Creating directory \""<<  directory.string()<<  "\"."<<  endl;
+    boost::filesystem::create_directories(directory);
    }

    // Choose format based on extension
@@ -64,6 +62,8 @@
      file.reset(new RAWFile(filename));
    else if (extension == ".xyz")
      file.reset(new XYZFile(filename));
+  else if (extension == ".xy")
+    file.reset(new XYFile(filename));
    else if (extension == ".bin")
      file.reset(new BinaryFile(filename));
    else
@@ -89,6 +89,9 @@
    case xyz:
      file.reset(new XYZFile(filename));
      break;
+  case xy:
+    file.reset(new XYFile(filename));
+    break;
    case binary:
      file.reset(new BinaryFile(filename));
      break;

=== modified file 'dolfin/io/File.h'
--- dolfin/io/File.h	2010-08-26 21:44:34 +0000
+++ dolfin/io/File.h	2010-09-08 16:59:48 +0000
@@ -7,7 +7,7 @@
  // Modified by Ola Skavhaug 2009
  //
  // First added:  2002-11-12
-// Last changed: 2010-02-10
+// Last changed: 2010-09-08

  #ifndef __FILE_H
  #define __FILE_H
@@ -33,7 +33,7 @@
    public:

      /// File formats
-    enum Type {xml, vtk, python, raw, xyz, binary};
+    enum Type {xml, vtk, python, raw, xyz, xy, binary};

      /// Create a file with given name
      File(const std::string filename, std::string encoding = "ascii");

=== added file 'dolfin/io/XYFile.cpp'
--- dolfin/io/XYFile.cpp	1970-01-01 00:00:00 +0000
+++ dolfin/io/XYFile.cpp	2010-09-08 16:59:48 +0000
@@ -0,0 +1,143 @@
+// Copyright (C) 2005-2007 Garth N.Wells.
+// Licensed under the GNU LGPL Version 2.1.
+//
+// Modified by Nuno Lopes 2010.
+//
+// First added:  2010-09-08
+
+#include<sstream>
+#include<fstream>
+#include<dolfin/fem/FiniteElement.h>
+#include<dolfin/mesh/Mesh.h>
+#include<dolfin/mesh/MeshFunction.h>
+#include<dolfin/mesh/Vertex.h>
+#include<dolfin/mesh/Cell.h>
+#include<dolfin/function/Function.h>
+#include<dolfin/function/FunctionSpace.h>
+#include<dolfin/la/Vector.h>
+#include "XYFile.h"
+
+using namespace dolfin;
+
+//----------------------------------------------------------------------------
+XYFile::XYFile(const std::string filename) : GenericFile(filename)
+{
+  type = "XY";
+}
+//----------------------------------------------------------------------------
+XYFile::~XYFile()
+{
+  // Do nothing
+}
+//----------------------------------------------------------------------------
+void XYFile::operator<<(const Function&  u)
+{
+  // Update xy file name and clear file
+  xy_name_update(counter);
+
+  // Write results
+  results_write(u);
+
+  // Increase the number of times we have saved the function
+  counter++;
+
+  cout<<  "Saved function "<<  u.name()<<  " ("<<  u.label()
+<<  ") to file "<<  filename<<  " in xgraphic xy format."<<  endl;
+}
+//----------------------------------------------------------------------------
+void XYFile::results_write(const Function&  u) const
+{
+  // Open file
+  std::ofstream fp(xy_filename.c_str(), std::ios_base::app);
+  if (!fp)
+    error("Unable to open file %s", filename.c_str());
+
+  const uint rank = u.function_space().element().value_rank();
+  if(rank>  1)
+    error("Only scalar functions can be saved in xy format.");
+
+  // Get number of components
+  uint dim = 1;
+  for (uint i = 0; i<  rank; i++)
+    dim *= u.function_space().element().value_dimension(i);
+
+  const Mesh&  mesh = u.function_space().mesh();
+
+  // Allocate memory for function values at vertices
+  const uint size = mesh.num_vertices()*dim;
+  Array<double>  values(size);
+
+  // Get function values at vertices
+  u.compute_vertex_values(values,mesh);
+
+  // Write function data at mesh vertices
+
+  if ( dim>  1 )
+    error("Cannot handle XY file for non-scalar functions. ");
+
+
+
+  std::ostringstream ss;
+  ss<<  std::scientific;
+  for (VertexIterator vertex(mesh); !vertex.end(); ++vertex)
+  {
+        ss.str("");
+        ss<<vertex->x(0)<<""<<  values[ vertex->index()];
+        ss<<std::endl;
+        fp<<ss.str( );
+  }
+}
+//----------------------------------------------------------------------------
+void XYFile::xy_name_update(int counter)
+{
+  std::string filestart, extension;
+  std::ostringstream fileid, newfilename;
+
+  fileid.fill('0');
+  fileid.width(6);
+
+  filestart.assign(filename, 0, filename.find("."));
+  extension.assign(filename, filename.find("."), filename.size());
+
+  fileid<<  counter;
+  newfilename<<  filestart<<  fileid.str()<<  ".xy";
+
+  xy_filename = newfilename.str();
+
+  // Make sure file is empty
+  FILE* fp = fopen(xy_filename.c_str(), "w");
+  if (!fp)
+    error("Unable to open file %s", filename.c_str());
+  fclose(fp);
+}
+//----------------------------------------------------------------------------
+template<class T>
+void XYFile::mesh_function_write(T&  meshfunction)
+{
+  // Update xy file name and clear file
+  xy_name_update(counter);
+
+  Mesh&  mesh = meshfunction.mesh();
+
+  if( meshfunction.dim() != mesh.topology().dim() )
+    error("XY output of mesh functions is implemenetd for cell-based functions only.");
+
+  // Open file
+  std::ofstream fp(xy_filename.c_str(), std::ios_base::app);
+
+  fp<<mesh.num_cells( )<<std::endl;
+  for (CellIterator cell(mesh); !cell.end(); ++cell)
+    fp<<  meshfunction.get( cell->index() )<<  std::endl;
+
+  // Close file
+  fp.close();
+
+  // Increase the number of times we have saved the mesh function
+  counter++;
+
+  cout<<  "saved mesh function "<<  counter<<  " times."<<  endl;
+
+  cout<<  "Saved mesh function "<<  mesh.name()<<  " ("<<  mesh.label()
+<<  ") to file "<<  filename<<  " in XY format."<<  endl;
+}
+//----------------------------------------------------------------------------

=== added file 'dolfin/io/XYFile.h'
--- dolfin/io/XYFile.h	1970-01-01 00:00:00 +0000
+++ dolfin/io/XYFile.h	2010-09-08 16:59:48 +0000
@@ -0,0 +1,45 @@
+// Copyright (C) 2005-2007 Garth N.Wells.
+// Licensed under the GNU LGPL Version 2.1.
+//
+// Modified by Nuno Lopes 2010.
+//
+// First added:  2010-02-17
+
+#ifndef __XY_FILE_H
+#define __XY_FILE_H
+
+#include<fstream>
+#include "GenericFile.h"
+
+namespace dolfin
+{
+
+  class XYFile : public GenericFile
+  {
+  public:
+
+    /// Simple and light file format for use with Xd3d. Supports
+    /// scalar solution on 2D convex domains. The files only have a
+    /// list of xy coordinates 'x , u(x)=y'
+
+    XYFile(const std::string filename);
+    ~XYFile();
+
+    void operator<<  (const Function&  u);
+
+  private:
+
+    void results_write(const Function&  u) const;
+    void xy_name_update(const int counter);
+
+    template<class T>
+    void mesh_function_write(T&  meshfunction);
+
+    // raw filename
+    std::string xy_filename;
+
+  };
+
+}
+
+#endif

# Begin bundle
IyBCYXphYXIgcmV2aXNpb24gYnVuZGxlIHY0CiMKQlpoOTFBWSZTWfjaqNUABrtfgEwQe////3/n
z+6////7YA29r5WttCNnVx3NUAjV0tbtXbYUXaba0MrYbWOgHCSRJhA0U8ym0TZTTEGIIaMammjN
QBoBoyCUQExNAIJiano0o/VD1PRGmg0NAaAADQHMJoyNDQyGEaGQ00aADEZMgGEAwCQohqaaqH5J
6NAp41T1NpB+VHojJp6j1Gg00A9TRppocwmjI0NDIYRoZDTRoAMRkyAYQDAJEghMEaRkJk1N6k8n
lDUyGpoeU8k009Q0yaaaGmlKpJNcxzZQjSPW0Hcw/7n3bKz8acvVG2b1+tqn8Sh/B2djl50oTzPq
xY0ldJRM1+2GCkRwuijRiiKik9c6ms/cRZXkG2sYZUIx5sKXDoUK1MXdTgZha20XMJN94cceJYTI
OqrRba3WfPjHLdiSAqxKuIXWU87qTIpVzr0QXXRfC5sOlhTRfc9fahJFUJHEXxJB0NIl5tMeEZEa
905ncykIbYdfAKmxfmWT6pYwz0H5Zn6cwleH3nUNNoGzXt0/FE4cuWnGeTGI9eTGHOTHL0o5Blpq
MTibikjtwsm5y40L5UpKcTWsnGHXZBB1RtTe7/DRDKM/PTZIsohSZm5MKdz9M711PgzsGzIMDUcu
u4PQMNydrQyNpE9bEW5p9IpdhD/DWE3zX11tFTyWEu6Q2/d0WQ15Pc1P4Y8rChDWEtXhTM5WZTCM
3S4zN5B+GFMRhZvep6mFxahBrX9FTSUJnFWWuTz1LB+1JMxkrnMG2PPbasQapnHmLB7mrvb5xXG+
GZkOgdTnRJhpa7VhttsGsUXbgPLuzPZZcGF293Y2+7LU3G6FEGpWFk4bZzGgx4yyQESY01jus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kGCl9z4fbbJOnOHkSzDyBX3gpl/mdvzKp
Ra4vR+c19bNGHEEyvAF7fp/XgCqCqjvxI9bL0wn79rcvRoeb0psfyQ2fltnJzZ9oEjpaDzphd8e4
7xzt4+WTr4AMBmvMDMMyTRNPm/4u5IpwoSHxtVGq

_______________________________________________
Mailing list: https://launchpad.net/~dolfin
Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
Unsubscribe : https://launchpad.net/~dolfin
More help   : https://help.launchpad.net/ListHelp


--
Anders

_______________________________________________
Mailing list: https://launchpad.net/~dolfin
Post to     : dolfin@xxxxxxxxxxxxxxxxxxx
Unsubscribe : https://launchpad.net/~dolfin
More help   : https://help.launchpad.net/ListHelp

--
Dr Garth N Wells
Department of Engineering
University of Cambridge
Trumpington Street
Cambridge CB2 1PZ
United Kingdom

tel.   +44 1223 3 32743
e-mail gnw20@xxxxxxxxx
http://www.eng.cam.ac.uk/~gnw20/



References