42 #include <boost/format.hpp>
53 "Writes a VTU file.");
77 if(
m_f->m_comm->TreatAsRankZero())
79 cout <<
"OutputVtk: Writing file..." << endl;
84 string filename =
m_config[
"outfile"].as<
string>();
88 if(
m_f->m_session->GetComm()->GetSize() != 1)
90 int dot = filename.find_last_of(
'.');
91 string ext = filename.substr(dot,filename.length()-dot);
92 string start = filename.substr(0,dot);
93 path = start +
"_vtu";
95 boost::format pad(
"P%1$07d.vtu");
96 pad %
m_f->m_session->GetComm()->GetRank();
99 fs::path poutfile(filename.c_str());
100 fs::path specPath(path.c_str());
102 if(
m_f->m_comm->TreatAsRankZero())
106 fs::create_directory(specPath);
108 catch (fs::filesystem_error& e)
110 ASSERTL0(
false,
"Filesystem error: " +
string(e.what()));
112 cout <<
"Writing files to directory: " << specPath << endl;
115 fs::path fulloutname = specPath / poutfile;
117 m_f->m_comm->Block();
121 fs::path specPath(filename.c_str());
122 cout <<
"Writing: " << specPath << endl;
127 ofstream outfile(filename.c_str());
128 m_f->m_exp[0]->WriteVtkHeader(outfile);
132 vector<string> fieldname;
135 dim =
m_f->m_exp[0]->GetExp(0)->GetCoordim();
138 if (
m_f->m_fielddef.size() == 0)
144 nfields =
m_f->m_fielddef[0]->m_fields.size();
146 m_f->m_session->LoadParameter(
"Strip_Z", nstrips, 1);
149 for(
int s = 0; s < nstrips; ++s)
152 for (i = 0; i <
m_f->m_exp[0]->GetNumElmts(); ++i)
154 m_f->m_exp[0]->WriteVtkPieceHeader(outfile,i,s);
157 for (j = 0; j < nfields; ++j)
159 m_f->m_exp[s*nfields+j]->WriteVtkPieceData(
160 outfile, i,
m_f->m_fielddef[0]->m_fields[j]);
162 m_f->m_exp[0]->WriteVtkPieceFooter(outfile, i);
167 for(i = 0; i < nfields; ++i)
169 fieldname.push_back(
m_f->m_fielddef[0]->m_fields[i]);
177 dim = fPts->GetDim();
179 if(fPts->GetNpoints() == 0)
185 switch(fPts->GetPtsType())
190 ASSERTL0(
false,
"VTK output needs settig up for PtsFile or Pts Line");
195 ASSERTL0(
false,
"VTK output needs settig up for PtsPlane");
211 ASSERTL0(
false,
"ptsType not supported yet.");
214 vector<Array<OneD, int> > ptsConn;
215 fPts->GetConnectivity(ptsConn);
217 nfields = fPts->GetNFields();
219 int nPts = fPts->GetNpoints();
221 for(i = 0; i < ptsConn.size(); ++i)
223 numBlocks += ptsConn[i].num_elements()/nvert;
227 outfile <<
" <Piece NumberOfPoints=\""
228 << nPts <<
"\" NumberOfCells=\""
229 << numBlocks <<
"\">" << endl;
230 outfile <<
" <Points>" << endl;
231 outfile <<
" <DataArray type=\"Float64\" "
232 <<
"NumberOfComponents=\""<<3<<
"\" format=\"ascii\">" << endl;
233 for(i = 0; i < nPts; ++i)
235 for(j = 0; j < dim; ++j)
237 outfile <<
" " << setprecision(8) << scientific
238 << fPts->GetPointVal(j, i) <<
" ";
240 for(j = dim; j < 3; ++j)
242 outfile <<
" 0.000000" ;
246 outfile <<
" </DataArray>" << endl;
247 outfile <<
" </Points>" << endl;
248 outfile <<
" <Cells>" << endl;
249 outfile <<
" <DataArray type=\"Int32\" "
250 <<
"Name=\"connectivity\" format=\"ascii\">" << endl;
255 for(i = 0; i < ptsConn.size();++i)
257 for(j = 0; j < ptsConn[i].num_elements(); ++j)
259 outfile << ptsConn[i][j] <<
" ";
260 if( (!(cnt % nvert)) && cnt )
262 outfile << std::endl;
268 outfile <<
" </DataArray>" << endl;
269 outfile <<
" <DataArray type=\"Int32\" "
270 <<
"Name=\"offsets\" format=\"ascii\">" << endl;
273 for (i = 0; i < numBlocks; ++i)
275 outfile << i*nvert+nvert <<
" ";
278 outfile <<
" </DataArray>" << endl;
279 outfile <<
" <DataArray type=\"UInt8\" "
280 <<
"Name=\"types\" format=\"ascii\">" << endl;
282 for (i = 0; i < numBlocks; ++i)
284 outfile << vtktype <<
" ";
287 outfile <<
" </DataArray>" << endl;
288 outfile <<
" </Cells>" << endl;
289 outfile <<
" <PointData>" << endl;
292 for(j = 0; j < nfields; ++j)
294 fieldname.push_back(fPts->GetFieldName(j));
295 outfile <<
" <DataArray type=\"Float64\" Name=\""
296 << fPts->GetFieldName(j) <<
"\">" << endl;
298 for(i = 0; i < fPts->GetNpoints(); ++i)
300 outfile << fPts->GetPointVal(dim+j, i) <<
" ";
303 outfile <<
" </DataArray>" << endl;
306 outfile <<
" </PointData>" << endl;
307 outfile <<
" </Piece>" << endl;
310 m_f->m_exp[0]->WriteVtkFooter(outfile);
311 cout <<
"Written file: " << filename << endl;
314 if(
m_f->m_comm->GetRank() == 0)
316 ASSERTL1(fieldname.size() == nfields,
"fieldname not the same size as nfields");
317 int nprocs =
m_f->m_comm->GetSize();
320 filename =
m_config[
"outfile"].as<
string>();
321 int dot = filename.find_last_of(
'.');
322 string body = filename.substr(0,dot);
323 filename = body +
".pvtu";
325 ofstream outfile(filename.c_str());
327 outfile <<
"<?xml version=\"1.0\"?>" << endl;
328 outfile <<
"<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" "
329 <<
"byte_order=\"LittleEndian\">" << endl;
330 outfile <<
"<PUnstructuredGrid GhostLevel=\"0\">" << endl;
331 outfile <<
"<PPoints> " << endl;
332 outfile <<
"<PDataArray type=\"Float64\" NumberOfComponents=\""
333 << 3 <<
"\"/> " << endl;
334 outfile <<
"</PPoints>" << endl;
335 outfile <<
"<PCells>" << endl;
336 outfile <<
"<PDataArray type=\"Int32\" Name=\"connectivity\" NumberOfComponents=\"1\"/>" << endl;
337 outfile <<
"<PDataArray type=\"Int32\" Name=\"offsets\" NumberOfComponents=\"1\"/>" << endl;
338 outfile <<
"<PDataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\"/>" << endl;
339 outfile <<
"</PCells>" << endl;
340 outfile <<
"<PPointData Scalars=\"Material\">" << endl;
341 for(
int i = 0; i < nfields; ++i)
343 outfile <<
"<PDataArray type=\"Float64\" Name=\"" <<
344 fieldname[i] <<
"\"/>" << endl;
346 outfile <<
"</PPointData>" << endl;
348 for(
int i = 0; i < nprocs; ++i)
350 boost::format pad(
"P%1$07d.vtu");
352 outfile <<
"<Piece Source=\"" << path <<
"/" << pad.str() <<
"\"/>" <<endl;
354 outfile <<
"</PUnstructuredGrid>" << endl;
355 outfile <<
"</VTKFile>" << endl;
356 cout <<
"Written file: " << filename << endl;
#define ASSERTL0(condition, msg)
pair< ModuleType, string > ModuleKey
Abstract base class for output modules.
map< string, ConfigOption > m_config
List of configuration values.
FieldSharedPtr m_f
Field object.
boost::shared_ptr< PtsField > PtsFieldSharedPtr
virtual void Process()
Write mesh to output file.
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
boost::shared_ptr< Field > FieldSharedPtr
static PtsFieldSharedPtr NullPtsField
#define ASSERTL1(condition, msg)
Assert Level 1 – Debugging which is used whether in FULLDEBUG or DEBUG compilation mode...
ModuleFactory & GetModuleFactory()
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.