42 #include <boost/format.hpp>
53 "Writes a VTU file.");
77 cout <<
"OutputVtk: Writing file..." << endl;
81 string filename =
m_config[
"outfile"].as<
string>();
85 if(
m_f->m_session->GetComm()->GetSize() != 1)
87 int dot = filename.find_last_of(
'.');
88 string ext = filename.substr(dot,filename.length()-dot);
89 string start = filename.substr(0,dot);
90 path = start +
"_vtu";
92 boost::format pad(
"P%1$07d.vtu");
93 pad %
m_f->m_session->GetComm()->GetRank();
96 fs::path poutfile(filename.c_str());
97 fs::path specPath(path.c_str());
99 if(
m_f->m_comm->GetRank() == 0)
103 fs::create_directory(specPath);
105 catch (fs::filesystem_error& e)
107 ASSERTL0(
false,
"Filesystem error: " +
string(e.what()));
109 cout <<
"Writing files to directory: " << specPath << endl;
112 fs::path fulloutname = specPath / poutfile;
114 m_f->m_comm->Block();
118 fs::path specPath(filename.c_str());
119 cout <<
"Writing: " << specPath << endl;
124 ofstream outfile(filename.c_str());
125 m_f->m_exp[0]->WriteVtkHeader(outfile);
129 vector<string> fieldname;
132 dim =
m_f->m_exp[0]->GetExp(0)->GetCoordim();
135 if (
m_f->m_fielddef.size() == 0)
141 nfields =
m_f->m_fielddef[0]->m_fields.size();
143 m_f->m_session->LoadParameter(
"Strip_Z", nstrips, 1);
146 for(
int s = 0; s < nstrips; ++s)
149 for (i = 0; i <
m_f->m_exp[0]->GetNumElmts(); ++i)
151 m_f->m_exp[0]->WriteVtkPieceHeader(outfile,i,s);
154 for (j = 0; j < nfields; ++j)
156 m_f->m_exp[s*nfields+j]->WriteVtkPieceData(
157 outfile, i,
m_f->m_fielddef[0]->m_fields[j]);
159 m_f->m_exp[0]->WriteVtkPieceFooter(outfile, i);
164 for(i = 0; i < nfields; ++i)
166 fieldname.push_back(
m_f->m_fielddef[0]->m_fields[i]);
174 dim = fPts->GetDim();
176 if(fPts->GetNpoints() == 0)
182 switch(fPts->GetPtsType())
187 ASSERTL0(
false,
"VTK output needs settig up for PtsFile or Pts Line");
192 ASSERTL0(
false,
"VTK output needs settig up for PtsPlane");
208 ASSERTL0(
false,
"ptsType not supported yet.");
211 vector<Array<OneD, int> > ptsConn;
212 fPts->GetConnectivity(ptsConn);
214 nfields = fPts->GetNFields();
216 int nPts = fPts->GetNpoints();
218 for(i = 0; i < ptsConn.size(); ++i)
220 numBlocks += ptsConn[i].num_elements()/nvert;
224 outfile <<
" <Piece NumberOfPoints=\""
225 << nPts <<
"\" NumberOfCells=\""
226 << numBlocks <<
"\">" << endl;
227 outfile <<
" <Points>" << endl;
228 outfile <<
" <DataArray type=\"Float64\" "
229 <<
"NumberOfComponents=\""<<3<<
"\" format=\"ascii\">" << endl;
230 for(i = 0; i < nPts; ++i)
232 for(j = 0; j < dim; ++j)
234 outfile <<
" " << setprecision(8) << scientific
235 << fPts->GetPointVal(j, i) <<
" ";
237 for(j = dim; j < 3; ++j)
239 outfile <<
" 0.000000" ;
243 outfile <<
" </DataArray>" << endl;
244 outfile <<
" </Points>" << endl;
245 outfile <<
" <Cells>" << endl;
246 outfile <<
" <DataArray type=\"Int32\" "
247 <<
"Name=\"connectivity\" format=\"ascii\">" << endl;
252 for(i = 0; i < ptsConn.size();++i)
254 for(j = 0; j < ptsConn[i].num_elements(); ++j)
256 outfile << ptsConn[i][j] <<
" ";
257 if( (!(cnt % nvert)) && cnt )
259 outfile << std::endl;
265 outfile <<
" </DataArray>" << endl;
266 outfile <<
" <DataArray type=\"Int32\" "
267 <<
"Name=\"offsets\" format=\"ascii\">" << endl;
270 for (i = 0; i < numBlocks; ++i)
272 outfile << i*nvert+nvert <<
" ";
275 outfile <<
" </DataArray>" << endl;
276 outfile <<
" <DataArray type=\"UInt8\" "
277 <<
"Name=\"types\" format=\"ascii\">" << endl;
279 for (i = 0; i < numBlocks; ++i)
281 outfile << vtktype <<
" ";
284 outfile <<
" </DataArray>" << endl;
285 outfile <<
" </Cells>" << endl;
286 outfile <<
" <PointData>" << endl;
289 for(j = 0; j < nfields; ++j)
291 fieldname.push_back(fPts->GetFieldName(j));
292 outfile <<
" <DataArray type=\"Float64\" Name=\""
293 << fPts->GetFieldName(j) <<
"\">" << endl;
295 for(i = 0; i < fPts->GetNpoints(); ++i)
297 outfile << fPts->GetPointVal(dim+j, i) <<
" ";
300 outfile <<
" </DataArray>" << endl;
303 outfile <<
" </PointData>" << endl;
304 outfile <<
" </Piece>" << endl;
307 m_f->m_exp[0]->WriteVtkFooter(outfile);
308 cout <<
"Written file: " << filename << endl;
311 if(
m_f->m_comm->GetRank() == 0)
313 ASSERTL1(fieldname.size() == nfields,
"fieldname not the same size as nfields");
314 int nprocs =
m_f->m_comm->GetSize();
317 filename =
m_config[
"outfile"].as<
string>();
318 int dot = filename.find_last_of(
'.');
319 string body = filename.substr(0,dot);
320 filename = body +
".pvtu";
322 ofstream outfile(filename.c_str());
324 outfile <<
"<?xml version=\"1.0\"?>" << endl;
325 outfile <<
"<VTKFile type=\"PUnstructuredGrid\" version=\"0.1\" "
326 <<
"byte_order=\"LittleEndian\">" << endl;
327 outfile <<
"<PUnstructuredGrid GhostLevel=\"0\">" << endl;
328 outfile <<
"<PPoints> " << endl;
329 outfile <<
"<PDataArray type=\"Float64\" NumberOfComponents=\""
330 << 3 <<
"\"/> " << endl;
331 outfile <<
"</PPoints>" << endl;
332 outfile <<
"<PCells>" << endl;
333 outfile <<
"<PDataArray type=\"Int32\" Name=\"connectivity\" NumberOfComponents=\"1\"/>" << endl;
334 outfile <<
"<PDataArray type=\"Int32\" Name=\"offsets\" NumberOfComponents=\"1\"/>" << endl;
335 outfile <<
"<PDataArray type=\"UInt8\" Name=\"types\" NumberOfComponents=\"1\"/>" << endl;
336 outfile <<
"</PCells>" << endl;
337 outfile <<
"<PPointData Scalars=\"Material\">" << endl;
338 for(
int i = 0; i < nfields; ++i)
340 outfile <<
"<PDataArray type=\"Float64\" Name=\"" <<
341 fieldname[i] <<
"\"/>" << endl;
343 outfile <<
"</PPointData>" << endl;
345 for(
int i = 0; i < nprocs; ++i)
347 boost::format pad(
"P%1$07d.vtu");
349 outfile <<
"<Piece Source=\"" << path <<
"/" << pad.str() <<
"\"/>" <<endl;
351 outfile <<
"</PUnstructuredGrid>" << endl;
352 outfile <<
"</VTKFile>" << endl;
353 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.