← Back to team overview

dolfin team mailing list archive

Fwd: [Branch ~dolfin-core/dolfin/main] Rev 4781: Add GenericFile<< (const std::pair<const Function*, double>)

 

This doesn't work from Python since we need some SWIG magic.

Johan Hake: Can you take a look at adding a typemap for

   std::pair<const Function*, double>

I guess that's what we need?

Garth


-------- Original Message --------
Subject: [Branch ~dolfin-core/dolfin/main] Rev 4781: Add GenericFile<< (const std::pair<const Function*, double>)
Date: Wed, 02 Jun 2010 12:18:12 -0000
From: noreply@xxxxxxxxxxxxx
Reply-To: noreply@xxxxxxxxxxxxx
To: Garth Wells <gnw20@xxxxxxxxx>

------------------------------------------------------------
revno: 4781
committer: Garth N. Wells <gnw20@xxxxxxxxx>
branch nick: dolfin-all
timestamp: Wed 2010-06-02 12:31:31 +0100
message:
  Add GenericFile<< (const std::pair<const Function*, double>)

The double is the time step which is written to the .pvd file. This is particularly useful when using variable time steps.
modified:
  demo/pde/advection-diffusion/cpp/main.cpp
  dolfin/io/File.cpp
  dolfin/io/File.h
  dolfin/io/GenericFile.cpp
  dolfin/io/GenericFile.h
  dolfin/io/VTKFile.cpp
  dolfin/io/VTKFile.h


--
lp:dolfin
https://code.launchpad.net/~dolfin-core/dolfin/main

Your team DOLFIN Core Team is subscribed to branch lp:dolfin.
To unsubscribe from this branch go to https://code.launchpad.net/~dolfin-core/dolfin/main/+edit-subscription

=== modified file 'demo/pde/advection-diffusion/cpp/main.cpp'
--- demo/pde/advection-diffusion/cpp/main.cpp	2010-01-31 16:33:46 +0000
+++ demo/pde/advection-diffusion/cpp/main.cpp	2010-06-02 11:31:31 +0000
@@ -87,7 +87,8 @@
     lu.solve(A, u.vector(), b);
 
     // Save solution in VTK format
-    file << u;
+    //file << u;
+    file << std::make_pair(&u, t);
 
     // Move to next interval
     p = t / T;

=== modified file 'dolfin/io/File.cpp'
--- dolfin/io/File.cpp	2010-02-10 18:13:32 +0000
+++ dolfin/io/File.cpp	2010-06-02 11:31:31 +0000
@@ -113,6 +113,13 @@
   *file << u;
 }
 //-----------------------------------------------------------------------------
+void File::operator<<(const std::pair<const Function*, double> u)
+{
+  u.first->gather();
+  file->write();
+  *file << u;
+}
+//-----------------------------------------------------------------------------
 bool File::exists(std::string filename)
 {
   std::ifstream file(filename.c_str());

=== modified file 'dolfin/io/File.h'
--- dolfin/io/File.h	2010-02-10 18:13:32 +0000
+++ dolfin/io/File.h	2010-06-02 11:31:31 +0000
@@ -14,6 +14,7 @@
 
 #include <ostream>
 #include <string>
+#include <utility>
 #include "GenericFile.h"
 
 namespace dolfin
@@ -55,6 +56,9 @@
     /// Write Function to file
     void operator<<(const Function& u);
 
+    /// Write Function to file (with time)
+    void operator<<(const std::pair<const Function*, double> u);
+
     /// Write to file
     template<class T> void operator<<(const T& t)
     {

=== modified file 'dolfin/io/GenericFile.cpp'
--- dolfin/io/GenericFile.cpp	2010-02-10 19:28:38 +0000
+++ dolfin/io/GenericFile.cpp	2010-06-02 11:31:31 +0000
@@ -178,6 +178,11 @@
   write_not_impl("Function");
 }
 //-----------------------------------------------------------------------------
+void GenericFile::operator<< (const std::pair<const Function*, double> u)
+{
+  write_not_impl("std::pair<Function*, double> Function");
+}
+//-----------------------------------------------------------------------------
 void GenericFile::operator<< (const Sample& sample)
 {
   write_not_impl("Sample");

=== modified file 'dolfin/io/GenericFile.h'
--- dolfin/io/GenericFile.h	2010-02-10 19:28:38 +0000
+++ dolfin/io/GenericFile.h	2010-06-02 11:31:31 +0000
@@ -11,6 +11,7 @@
 
 #include <map>
 #include <string>
+#include <utility>
 #include <vector>
 #include "dolfin/common/types.h"
 
@@ -66,6 +67,10 @@
     virtual void operator<< (const MeshFunction<double>& meshfunction);
     virtual void operator<< (const MeshFunction<bool>& meshfunction);
     virtual void operator<< (const Function& u);
+
+    // Output function with time
+    virtual void operator<< (const std::pair<const Function*, double> u);
+
     virtual void operator<< (const Sample& sample);
     virtual void operator<< (const Parameters& parameters);
     virtual void operator<< (const FunctionPlotData& data);

=== modified file 'dolfin/io/VTKFile.cpp'
--- dolfin/io/VTKFile.cpp	2010-05-24 18:26:30 +0000
+++ dolfin/io/VTKFile.cpp	2010-06-02 11:31:31 +0000
@@ -67,7 +67,7 @@
   }
 
   // Finalise and write pvd files
-  finalize(vtu_filename);
+  finalize(vtu_filename, counter);
 
   info(TRACE, "Saved mesh %s (%s) to file %s in VTK format.",
           mesh.name().c_str(), mesh.label().c_str(), filename.c_str());
@@ -95,6 +95,16 @@
 //----------------------------------------------------------------------------
 void VTKFile::operator<<(const Function& u)
 {
+  write(u, counter);
+}
+//----------------------------------------------------------------------------
+void VTKFile::operator<<(const std::pair<const Function*, double> u)
+{
+  write(*(u.first), u.second);
+}
+//----------------------------------------------------------------------------
+void VTKFile::write(const Function& u, double time)
+{
   const Mesh& mesh = u.function_space().mesh();
 
   // Get vtu file name and intialise
@@ -115,7 +125,7 @@
   }
 
   // Finalise and write pvd files
-  finalize(vtu_filename);
+  finalize(vtu_filename, time);
 
   info(TRACE, "Saved function %s (%s) to file %s in VTK format.",
           u.name().c_str(), u.label().c_str(), filename.c_str());
@@ -141,7 +151,7 @@
   return vtu_filename;
 }
 //----------------------------------------------------------------------------
-void VTKFile::finalize(std::string vtu_filename)
+void VTKFile::finalize(std::string vtu_filename, double time)
 {
   // Close headers
   vtk_header_close(vtu_filename);
@@ -156,11 +166,11 @@
       pvtu_header_close(pvtu_filename);
 
       // Write pvd file (parallel)
-      pvd_file_write(counter, pvtu_filename);
+      pvd_file_write(counter, counter, pvtu_filename);
     }
   }
   else
-    pvd_file_write(counter, vtu_filename);
+    pvd_file_write(counter, time, vtu_filename);
 
   // Increase the number of times we have saved the object
   counter++;
@@ -617,11 +627,11 @@
   fp << "</CellData> " << std::endl;
 }
 //----------------------------------------------------------------------------
-void VTKFile::pvd_file_write(uint num, std::string _filename)
+void VTKFile::pvd_file_write(uint step, double time, std::string _filename)
 {
   std::fstream pvd_file;
 
-  if( num == 0)
+  if (step == 0)
   {
     // Open pvd file
     pvd_file.open(filename.c_str(), std::ios::out|std::ios::trunc);
@@ -642,7 +652,7 @@
   std::string fname = strip_path(_filename);
 
   // Data file name
-  pvd_file << "<DataSet timestep=\"" << num << "\" part=\"0\"" << " file=\"" <<  fname <<  "\"/>" << std::endl;
+  pvd_file << "<DataSet timestep=\"" << time << "\" part=\"0\"" << " file=\"" <<  fname <<  "\"/>" << std::endl;
   mark = pvd_file.tellp();
 
   // Close headers
@@ -886,7 +896,7 @@
   fp.close();
 
   // Write pvd files
-  finalize(vtu_filename);
+  finalize(vtu_filename, counter);
 
   cout << "saved mesh function " << counter << " times." << endl;
 

=== modified file 'dolfin/io/VTKFile.h'
--- dolfin/io/VTKFile.h	2010-03-14 10:18:32 +0000
+++ dolfin/io/VTKFile.h	2010-06-02 11:31:31 +0000
@@ -12,6 +12,7 @@
 
 #include <fstream>
 #include <string>
+#include <utility>
 #include <vector>
 #include "GenericFile.h"
 
@@ -31,12 +32,15 @@
     void operator<< (const MeshFunction<int>& meshfunction);
     void operator<< (const MeshFunction<double>& meshfunction);
     void operator<< (const Function& u);
+    void operator<< (const std::pair<const Function*, double> u);
 
   protected:
 
+    void write(const Function& u, double time);
+
     std::string init(const Mesh& mesh) const;
 
-    void finalize(std::string vtu_filename);
+    void finalize(std::string vtu_filename, double time);
 
     void mesh_write(const Mesh& mesh, std::string file) const;
 
@@ -47,7 +51,7 @@
 
     void write_cell_data(const Function& u, std::string file) const;
 
-    void pvd_file_write(uint u, std::string file);
+    void pvd_file_write(uint step, double time, std::string file);
 
     void pvtu_mesh_write(std::string pvtu_filename, std::string vtu_filename) const;