6
// AUTHOR : Cedric Ody (not an expert in c++)
7
// E-MAIL : cedric.listes@gmail.com
8
// from the work of Sala Lorenzo (Dxwriter)
10
#include "mode_open.hpp"
17
//using namespace Fem2D;
26
int imesh;//!<index of the mesh
28
std::vector<double> vecistant;
32
std::vector<Mesh3*> _vecmesh;
33
//std::vector<tsinfo> _vecofts;
34
std::string _nameoffile;
36
/*! This string contains the name of data file with \\ where there's a \ in the path*/
37
std::string _nameofdatafile;
39
//!files containing the data and the timeseries
40
std::ofstream _ofdata;
43
VtkWriter() { std::cout << "Constructor of VtkWriter" << endl; }
44
void openfiles(const std::string& s)
47
std::string tmp=s+".vtu";
49
_ofdata.open(tmp.c_str(), std::ios_base::out);
51
for(int i=0;i<tmp.length();++i)
54
_nameofdatafile.append(1,'\\');
55
_nameofdatafile.append(1,tmp.at(i));
59
void addmesh(Mesh3* mesh)
62
_vecmesh.push_back(mesh);
63
_ofdata.flags(std::ios_base::scientific);
64
_ofdata.precision(15);
66
_ofdata << "<?xml version=\"1.0\"?>" << std::endl;
67
_ofdata << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">" << std::endl;
68
_ofdata << "<UnstructuredGrid>" << std::endl;
69
_ofdata << "<Piece NumberOfPoints=\"" << Th.nv << "\" NumberOfCells=\"" << Th.nt << "\">" << std::endl;
70
_ofdata << "<Points>" << std::endl;
71
_ofdata << "<DataArray type=\"Float32\" Name=\"Position\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl;
72
for(int k=0;k<Th.nv;++k) _ofdata << Th(k).x<<" "<<Th(k).y<< " "<<Th(k).z<<std::endl;
73
_ofdata << "</DataArray>" << std::endl;
74
_ofdata << "</Points>" << std::endl;
75
_ofdata << "<Cells>" << std::endl;
76
_ofdata << "<DataArray type=\"Int32\" Name=\"connectivity\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
77
for(int i=0;i<Th.nt;++i)
78
for (int j=0; j <4; j++) _ofdata << Th(i,j) << " " ;
80
_ofdata << "</DataArray>" << std::endl;
81
_ofdata << "<DataArray type=\"Int32\" Name=\"offsets\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
82
for(int i=0;i<Th.nt;++i) _ofdata << 4+4*(i) << " ";
84
_ofdata << "</DataArray>" << std::endl;
85
_ofdata << "<DataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
86
for(int i=0;i<Th.nt;++i) _ofdata << 10 << " ";
88
_ofdata << "</DataArray>" << std::endl;
89
_ofdata << "</Cells>" << std::endl;
90
_ofdata << "<PointData >" << endl;
94
void addscalar(const string& nameoffield, Mesh3* mesh, const KN<double>&val)
96
_ofdata.flags(std::ios_base::scientific);
97
_ofdata.precision(15);
99
_ofdata << "<DataArray type=\"Float32\" Name=\""<<nameoffield<<"\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
100
for(int i=0;i<val.size();++i) _ofdata<<checkprecision(val[i])<<std::endl;
101
_ofdata << "</DataArray>" << std::endl;
106
double checkprecision(double val)
109
if ( val >= 0. ) tmp=max(0.,val);
110
if ( val < 0. ) tmp=min(0.,val);
115
void addvector(const string& nameoffield, Mesh3* mesh, const KN<double>&val,
116
const KN<double>&val2,const KN<double>&val3 )
118
_ofdata.flags(std::ios_base::scientific);
119
_ofdata.precision(15);
121
_ofdata << "<DataArray type=\"Float32\" Name=\""<<nameoffield<<"\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl;
122
for(int i=0;i<val.size();++i) _ofdata<<checkprecision(val[i])<< " " << checkprecision(val2[i]) << " " << checkprecision(val3[i]) << std::endl;
123
_ofdata << "</DataArray>" << std::endl;
128
/*!Get the mesh associated with the series nameofts*/
129
Mesh3* getmeshts(const string& nameofts)
136
new(this)VtkWriter();
141
if(_ofdata.is_open())
143
_ofdata << "</PointData>" << endl;
144
_ofdata << "<CellData>" << endl;
145
_ofdata << "</CellData>" << endl;
146
_ofdata << "</Piece>" << endl;
147
_ofdata << "</UnstructuredGrid>" << endl;
148
_ofdata << "</VTKFile>" << endl;
154
class Vtkwritesol_Op : public E_F0mps
159
Expression ename;//!<name of time series or field
160
Expression et;//!<time
161
long what; // 1 scalar, 2 vector, 3 symtensor
162
long nbfloat; // 1 scalar, n vector (3D), n symtensor(3D)
163
Expression evct,evct2,evct3;
166
Vtkwritesol_Op(const basicAC_F0 & args) : what(0), nbfloat(0)
173
//There's no named parameter
177
CompileError("Vtkwritesol accepts only 4 parameters");
179
if (BCastTo<VtkWriter *>(args[0])) edx = CastTo<VtkWriter *>(args[0]);
180
if (BCastTo<string *>(args[1])) ename = CastTo<string *>(args[1]);
182
if ( args[2].left()==atype<double>() )
186
evct=to<double>( args[2] );
188
else if ( args[2].left()==atype<double *>() )
192
evct=to<double>( args[2] );
194
else if ( BCastTo<pfer>(args[2]) )
198
evct=to<double>( args[2] );
200
else if ( args[2].left()==atype<E_Array>() )
202
std::cout << "Until now only scalar solution" << std::endl;
205
const E_Array * a0 = dynamic_cast<const E_Array *>( args[i].LeftValue() );
208
if( a0->size() == 1){
212
evct = to<double>( (*a0)[0]);
216
if( a0->size() == ddim){
220
evct = to<double>( (*a0)[0]);
221
evct2 = to<double>( (*a0)[1]);
222
evct3 = to<double>( (*a0)[2]);
225
cout << "Passed Until now only scalar solution" << std::endl;
229
CompileError("savesol in 2D: Sorry no way to save this kind of data");
233
static ArrayOfaType typeargs() { return ArrayOfaType(atype<VtkWriter *>(), atype<string *>(), true); }// all type
234
static E_F0 * f(const basicAC_F0 & args) { return new Vtkwritesol_Op(args);}
235
AnyType operator()(Stack stack) const ;
239
AnyType Vtkwritesol_Op::operator()(Stack stack) const
241
MeshPoint *mp(MeshPointStack(stack)) , mps=*mp;
242
VtkWriter &dx=*(GetAny<VtkWriter *>((*edx)(stack)));
243
string &name=*(GetAny<string *>((*ename)(stack)));
244
//double t=GetAny<double>((*et)(stack));
245
Mesh3 &Th=*(dx.getmeshts(name));
253
KN<double> valsol(nbsol);
255
KN<int> takemesh(nbsol);
257
MeshPoint *mp3(MeshPointStack(stack));
258
for (int it=0;it<nt;it++)
260
for(int iv=0;iv<4;iv++)
263
mp3->setP(&Th,it,iv);
264
valsol[i] = valsol[i] + GetAny< double >((*evct)(stack));
268
for(int i=0; i<nbsol; i++)
270
valsol[i] /= takemesh[i];
273
//Writes valsol on the file file
275
dx.addscalar(name,&Th,valsol);
279
KN<double> valsol2(nbsol);
281
KN<int> takemesh(nbsol);
283
MeshPoint *mp3(MeshPointStack(stack));
284
for (int it=0;it<nt;it++)
286
for(int iv=0;iv<4;iv++)
289
mp3->setP(&Th,it,iv);
290
valsol2[i] = valsol2[i] + GetAny< double >((*evct2)(stack));
294
for(int i=0; i<nbsol; i++)
296
valsol2[i] /= takemesh[i];
300
KN<double> valsol3(nbsol);
302
KN<int> takemesh(nbsol);
304
MeshPoint *mp3(MeshPointStack(stack));
305
for (int it=0;it<nt;it++)
307
for(int iv=0;iv<4;iv++)
310
mp3->setP(&Th,it,iv);
311
valsol3[i] = valsol3[i] + GetAny< double >((*evct3)(stack));
315
for(int i=0; i<nbsol; i++)
317
valsol3[i] /= takemesh[i];
320
//Writes valsol on the file file
321
dx.addvector(name,&Th,valsol,valsol2,valsol3);
333
// le vrai constructeur est la
334
VtkWriter* init_VtkWriter(VtkWriter * const &a, string * const & s)
336
std::cout << "start init_VtkWriter" << std::endl;
339
std::cout << "end init_VtkWriter" << std::endl;
343
void* call_addmesh( VtkWriter * const & mt, Mesh3* const & pTh) { mt->addmesh(pTh);}
345
// Add the function name to the freefem++ table
356
Dcl_Type<VtkWriter*>(InitP<VtkWriter>,Destroy<VtkWriter>); // declare deux nouveau type pour freefem++ un pointeur et
358
zzzfff->Add("VtkWriter",atype<VtkWriter*>()); // ajoute le type myType a freefem++
359
// constructeur d'un type myType dans freefem
360
TheOperators->Add("<-", new OneOperator2_<VtkWriter*, VtkWriter* ,string*>(&init_VtkWriter));
361
Global.Add("Vtkaddmesh","(",new OneOperator2_<void *, VtkWriter*, Mesh3*>(call_addmesh));
362
Global.Add("Vtkaddscalar","(",new OneOperatorCode< Vtkwritesol_Op> );
6
// AUTHOR : Cedric Ody (not an expert in c++)
7
// E-MAIL : cedric.listes@gmail.com
8
// from the work of Sala Lorenzo (Dxwriter)
10
#include "mode_open.hpp"
17
//using namespace Fem2D;
26
int imesh;//!<index of the mesh
28
std::vector<double> vecistant;
32
std::vector<Mesh3*> _vecmesh;
33
//std::vector<tsinfo> _vecofts;
34
std::string _nameoffile;
36
/*! This string contains the name of data file with \\ where there's a \ in the path*/
37
std::string _nameofdatafile;
39
//!files containing the data and the timeseries
40
std::ofstream _ofdata;
43
VtkWriter() { std::cout << "Constructor of VtkWriter" << endl; }
44
void openfiles(const std::string& s)
47
std::string tmp=s+".vtu";
49
_ofdata.open(tmp.c_str(), std::ios_base::out);
51
for(int i=0;i<tmp.length();++i)
54
_nameofdatafile.append(1,'\\');
55
_nameofdatafile.append(1,tmp.at(i));
59
void addmesh(Mesh3* mesh)
62
_vecmesh.push_back(mesh);
63
_ofdata.flags(std::ios_base::scientific);
64
_ofdata.precision(15);
66
_ofdata << "<?xml version=\"1.0\"?>" << std::endl;
67
_ofdata << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">" << std::endl;
68
_ofdata << "<UnstructuredGrid>" << std::endl;
69
_ofdata << "<Piece NumberOfPoints=\"" << Th.nv << "\" NumberOfCells=\"" << Th.nt << "\">" << std::endl;
70
_ofdata << "<Points>" << std::endl;
71
_ofdata << "<DataArray type=\"Float32\" Name=\"Position\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl;
72
for(int k=0;k<Th.nv;++k) _ofdata << Th(k).x<<" "<<Th(k).y<< " "<<Th(k).z<<std::endl;
73
_ofdata << "</DataArray>" << std::endl;
74
_ofdata << "</Points>" << std::endl;
75
_ofdata << "<Cells>" << std::endl;
76
_ofdata << "<DataArray type=\"Int32\" Name=\"connectivity\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
77
for(int i=0;i<Th.nt;++i)
78
for (int j=0; j <4; j++) _ofdata << Th(i,j) << " " ;
80
_ofdata << "</DataArray>" << std::endl;
81
_ofdata << "<DataArray type=\"Int32\" Name=\"offsets\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
82
for(int i=0;i<Th.nt;++i) _ofdata << 4+4*(i) << " ";
84
_ofdata << "</DataArray>" << std::endl;
85
_ofdata << "<DataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
86
for(int i=0;i<Th.nt;++i) _ofdata << 10 << " ";
88
_ofdata << "</DataArray>" << std::endl;
89
_ofdata << "</Cells>" << std::endl;
90
_ofdata << "<PointData >" << endl;
94
void addscalar(const string& nameoffield, Mesh3* mesh, const KN<double>&val)
96
_ofdata.flags(std::ios_base::scientific);
97
_ofdata.precision(15);
99
_ofdata << "<DataArray type=\"Float32\" Name=\""<<nameoffield<<"\" NumberOfComponents=\"1\" format=\"ascii\">" << std::endl;
100
for(int i=0;i<val.size();++i) _ofdata<<checkprecision(val[i])<<std::endl;
101
_ofdata << "</DataArray>" << std::endl;
106
double checkprecision(double val)
109
if ( val >= 0. ) tmp=max(0.,val);
110
if ( val < 0. ) tmp=min(0.,val);
115
void addvector(const string& nameoffield, Mesh3* mesh, const KN<double>&val,
116
const KN<double>&val2,const KN<double>&val3 )
118
_ofdata.flags(std::ios_base::scientific);
119
_ofdata.precision(15);
121
_ofdata << "<DataArray type=\"Float32\" Name=\""<<nameoffield<<"\" NumberOfComponents=\"3\" format=\"ascii\">" << std::endl;
122
for(int i=0;i<val.size();++i) _ofdata<<checkprecision(val[i])<< " " << checkprecision(val2[i]) << " " << checkprecision(val3[i]) << std::endl;
123
_ofdata << "</DataArray>" << std::endl;
128
/*!Get the mesh associated with the series nameofts*/
129
Mesh3* getmeshts(const string& nameofts)
136
new(this)VtkWriter();
141
if(_ofdata.is_open())
143
_ofdata << "</PointData>" << endl;
144
_ofdata << "<CellData>" << endl;
145
_ofdata << "</CellData>" << endl;
146
_ofdata << "</Piece>" << endl;
147
_ofdata << "</UnstructuredGrid>" << endl;
148
_ofdata << "</VTKFile>" << endl;
154
class Vtkwritesol_Op : public E_F0mps
159
Expression ename;//!<name of time series or field
160
Expression et;//!<time
161
long what; // 1 scalar, 2 vector, 3 symtensor
162
long nbfloat; // 1 scalar, n vector (3D), n symtensor(3D)
163
Expression evct,evct2,evct3;
166
Vtkwritesol_Op(const basicAC_F0 & args) : what(0), nbfloat(0)
173
//There's no named parameter
177
CompileError("Vtkwritesol accepts only 4 parameters");
179
if (BCastTo<VtkWriter *>(args[0])) edx = CastTo<VtkWriter *>(args[0]);
180
if (BCastTo<string *>(args[1])) ename = CastTo<string *>(args[1]);
182
if ( args[2].left()==atype<double>() )
186
evct=to<double>( args[2] );
188
else if ( args[2].left()==atype<double *>() )
192
evct=to<double>( args[2] );
194
else if ( BCastTo<pfer>(args[2]) )
198
evct=to<double>( args[2] );
200
else if ( args[2].left()==atype<E_Array>() )
202
std::cout << "Until now only scalar solution" << std::endl;
205
const E_Array * a0 = dynamic_cast<const E_Array *>( args[i].LeftValue() );
208
if( a0->size() == 1){
212
evct = to<double>( (*a0)[0]);
216
if( a0->size() == ddim){
220
evct = to<double>( (*a0)[0]);
221
evct2 = to<double>( (*a0)[1]);
222
evct3 = to<double>( (*a0)[2]);
225
cout << "Passed Until now only scalar solution" << std::endl;
229
CompileError("savesol in 2D: Sorry no way to save this kind of data");
233
static ArrayOfaType typeargs() { return ArrayOfaType(atype<VtkWriter *>(), atype<string *>(), true); }// all type
234
static E_F0 * f(const basicAC_F0 & args) { return new Vtkwritesol_Op(args);}
235
AnyType operator()(Stack stack) const ;
239
AnyType Vtkwritesol_Op::operator()(Stack stack) const
241
MeshPoint *mp(MeshPointStack(stack)) , mps=*mp;
242
VtkWriter &dx=*(GetAny<VtkWriter *>((*edx)(stack)));
243
string &name=*(GetAny<string *>((*ename)(stack)));
244
//double t=GetAny<double>((*et)(stack));
245
Mesh3 &Th=*(dx.getmeshts(name));
253
KN<double> valsol(nbsol);
255
KN<int> takemesh(nbsol);
257
MeshPoint *mp3(MeshPointStack(stack));
258
for (int it=0;it<nt;it++)
260
for(int iv=0;iv<4;iv++)
263
mp3->setP(&Th,it,iv);
264
valsol[i] = valsol[i] + GetAny< double >((*evct)(stack));
268
for(int i=0; i<nbsol; i++)
270
valsol[i] /= takemesh[i];
273
//Writes valsol on the file file
275
dx.addscalar(name,&Th,valsol);
279
KN<double> valsol2(nbsol);
281
KN<int> takemesh(nbsol);
283
MeshPoint *mp3(MeshPointStack(stack));
284
for (int it=0;it<nt;it++)
286
for(int iv=0;iv<4;iv++)
289
mp3->setP(&Th,it,iv);
290
valsol2[i] = valsol2[i] + GetAny< double >((*evct2)(stack));
294
for(int i=0; i<nbsol; i++)
296
valsol2[i] /= takemesh[i];
300
KN<double> valsol3(nbsol);
302
KN<int> takemesh(nbsol);
304
MeshPoint *mp3(MeshPointStack(stack));
305
for (int it=0;it<nt;it++)
307
for(int iv=0;iv<4;iv++)
310
mp3->setP(&Th,it,iv);
311
valsol3[i] = valsol3[i] + GetAny< double >((*evct3)(stack));
315
for(int i=0; i<nbsol; i++)
317
valsol3[i] /= takemesh[i];
320
//Writes valsol on the file file
321
dx.addvector(name,&Th,valsol,valsol2,valsol3);
333
// le vrai constructeur est la
334
VtkWriter* init_VtkWriter(VtkWriter * const &a, string * const & s)
336
std::cout << "start init_VtkWriter" << std::endl;
339
std::cout << "end init_VtkWriter" << std::endl;
343
void* call_addmesh( VtkWriter * const & mt, Mesh3* const & pTh) {
348
// Add the function name to the freefem++ table
359
Dcl_Type<VtkWriter*>(InitP<VtkWriter>,Destroy<VtkWriter>); // declare deux nouveau type pour freefem++ un pointeur et
361
zzzfff->Add("VtkWriter",atype<VtkWriter*>()); // ajoute le type myType a freefem++
362
// constructeur d'un type myType dans freefem
363
TheOperators->Add("<-", new OneOperator2_<VtkWriter*, VtkWriter* ,string*>(&init_VtkWriter));
364
Global.Add("Vtkaddmesh","(",new OneOperator2_<void *, VtkWriter*, Mesh3*>(call_addmesh));
365
Global.Add("Vtkaddscalar","(",new OneOperatorCode< Vtkwritesol_Op> );