1
// Copyright (C) 2003-2007 Johan Hoffman and Anders Logg.
2
// Licensed under the GNU LGPL Version 2.1.
4
// Modified by Garth N. Wells 2005
6
// First added: 2003-05-06
7
// Last changed: 2007-05-02
9
#include <dolfin/dolfin_log.h>
10
#include <dolfin/Vector.h>
11
#include <dolfin/Matrix.h>
12
#include <dolfin/Mesh.h>
13
#include <dolfin/Function.h>
14
#include <dolfin/Sample.h>
15
#include <dolfin/Vertex.h>
16
#include <dolfin/Cell.h>
17
#include <dolfin/MFile.h>
19
using namespace dolfin;
21
//-----------------------------------------------------------------------------
22
MFile::MFile(const std::string filename) :
25
type = "Octave/MATLAB";
27
//-----------------------------------------------------------------------------
32
//-----------------------------------------------------------------------------
33
void MFile::operator<<(Vector& x)
35
error("Function output in Matlab/Octave format is broken. Need to access vector elements correctly.");
38
FILE *fp = fopen(filename.c_str(), "a");
41
fprintf(fp, "%s = [", x.name().c_str());
42
for (unsigned int i = 0; i < x.size(); i++)
44
// FIXME: This is a slow way to access PETSc vectors. Need a fast way
45
// which is consistent for different vector types.
47
fprintf(fp, " %.15g", temp);
54
message("Saved vector %s (%s) to file %s in Octave/MATLAB format.",
55
x.name().c_str(), x.label().c_str(), filename.c_str());
58
//-----------------------------------------------------------------------------
59
void MFile::operator<<(Mesh& mesh)
64
FILE *fp = fopen(filename.c_str(), "a");
66
// Create a list if we save the mesh a second time
69
fprintf(fp, "tmp = points;\n");
70
fprintf(fp, "clear points\n");
71
fprintf(fp, "points{1} = tmp;\n");
72
fprintf(fp, "clear tmp\n\n");
74
fprintf(fp, "tmp = cells;\n");
75
fprintf(fp, "clear cells\n");
76
fprintf(fp, "cells{1} = tmp;\n");
77
fprintf(fp, "clear tmp\n\n");
79
fprintf(fp, "tmp = edges;\n");
80
fprintf(fp, "clear edges\n");
81
fprintf(fp, "edges{1} = tmp;\n");
82
fprintf(fp, "clear tmp\n\n");
87
fprintf(fp,"points = [");
89
fprintf(fp,"points{%u} = [", counter + 1);
90
for (VertexIterator v(mesh); !v.end();)
95
if ( mesh.type().cellType() == CellType::triangle )
98
fprintf(fp,"%.15f %.15f]';\n", p.x(), p.y());
100
fprintf(fp,"%.15f %.15f\n", p.x(), p.y() );
104
fprintf(fp,"%.15f %.15f %.15f]';\n", p.x(), p.y(), p.z());
106
fprintf(fp,"%.15f %.15f %.15f\n", p.x(), p.y(), p.z());
113
fprintf(fp,"cells = [");
115
fprintf(fp,"cells{%u} = [", counter + 1);
116
for (CellIterator c(mesh); !c.end();)
118
for (VertexIterator v(*c); !v.end(); ++v)
119
fprintf(fp, "%u ", (v->index()) + 1 );
122
fprintf(fp, "]';\n");
128
// FIXME: Save all edges correctly
129
// Write edges (to make the pdeplot routines happy)
131
fprintf(fp,"edges = [1;2;0;0;0;0;0];\n\n");
133
fprintf(fp,"edges{%u} = [1;2;0;0;0;0;0];\n\n", counter + 1);
138
// // Increase the number of times we have saved the mesh
139
// // FIXME: Count number of meshes saved to this file, rather
140
// // than the number of times this specific mesh has been saved.
143
// Increase the number of meshes saved to this file
146
message(1, "Saved mesh %s (%s) to file %s in Octave/MATLAB format.",
147
mesh.name().c_str(), mesh.label().c_str(), filename.c_str());
149
//-----------------------------------------------------------------------------
150
void MFile::operator<<(Function& u)
152
error("Function output in Matlab/Octave format not implemented for new Function.");
154
// Write mesh the first time
159
FILE *fp = fopen(filename.c_str(), "a");
161
// Move old vector into list if we are saving a new value
164
fprintf(fp, "tmp = %s;\n", u.name().c_str());
165
fprintf(fp, "clear %s\n", u.name().c_str());
166
fprintf(fp, "%s{1} = tmp;\n", u.name().c_str());
167
fprintf(fp, "clear tmp\n\n");
173
fprintf(fp, "%s = [", u.name().c_str());
174
for (unsigned int i = 0; i < u.vectordim(); i++)
176
for (VertexIterator v(u.mesh()); !v.end(); ++v)
177
fprintf(fp, " %.15f", u(*v, i));
180
fprintf(fp, " ]';\n\n");
184
fprintf(fp, "%s{%u} = [", u.name().c_str(), counter1 + 1);
185
for (unsigned int i = 0; i < u.vectordim(); i++)
187
for (VertexIterator v(u.mesh()); !v.end(); ++v)
188
fprintf(fp, " %.15f", u(*v, i));
191
fprintf(fp, " ]';\n\n");
197
// Increase the number of times we have saved the function
200
cout << "Saved function " << u.name() << " (" << u.label()
201
<< ") to file " << filename << " in Octave/Matlab format." << endl;
204
//-----------------------------------------------------------------------------
205
void MFile::operator<<(Sample& sample)
208
FILE *fp = fopen(filename.c_str(), "a");
210
// Initialize data structures first time
213
fprintf(fp, "t = [];\n");
214
fprintf(fp, "%s = [];\n", sample.name().c_str());
215
fprintf(fp, "k = [];\n");
216
fprintf(fp, "r = [];\n");
221
fprintf(fp, "t = [t %.15e];\n", sample.t());
224
fprintf(fp, "tmp = [ ");
225
for (unsigned int i = 0; i < sample.size(); i++)
226
fprintf(fp, "%.15e ", sample.u(i));
228
fprintf(fp, "%s = [%s tmp'];\n",
229
sample.name().c_str(), sample.name().c_str());
230
//fprintf(fp, "clear tmp;\n");
233
fprintf(fp, "tmp = [ ");
234
for (unsigned int i = 0; i < sample.size(); i++)
235
fprintf(fp, "%.15e ", sample.k(i));
238
fprintf(fp, "k = [k tmp'];\n");
239
//fprintf(fp, "clear tmp;\n");
242
fprintf(fp, "tmp = [ ");
243
for (unsigned int i = 0; i < sample.size(); i++)
244
fprintf(fp, "%.15e ", sample.r(i));
247
fprintf(fp, "r = [r tmp'];\n");
248
fprintf(fp, "clear tmp;\n");
250
// Increase frame counter
256
//-----------------------------------------------------------------------------