70 const uint64_t cellId = _cellId - 1;
72 uint64_t currentCellCoordinate[3];
76 currentCellCoordinate[0] = cellId % cellStruct.
cell_bounds[0];
77 currentCellCoordinate[1] = ((cellId - currentCellCoordinate[0]) / cellStruct.
cell_bounds[0]) % cellStruct.
cell_bounds[1];
96 Real x_min, x_max, y_min, y_max, z_min, z_max, vx_min, vx_max, vy_min, vy_max,
vz_min, vz_max;
98 if( vlsvReader.readParameter(
"xmin", x_min ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
99 if( vlsvReader.readParameter(
"xmax", x_max ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
100 if( vlsvReader.readParameter(
"ymin", y_min ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
101 if( vlsvReader.readParameter(
"ymax", y_max ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
102 if( vlsvReader.readParameter(
"zmin", z_min ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
103 if( vlsvReader.readParameter(
"zmax", z_max ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
105 if( vlsvReader.readParameter(
"vxmin", vx_min ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
106 if( vlsvReader.readParameter(
"vxmax", vx_max ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
107 if( vlsvReader.readParameter(
"vymin", vy_min ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
108 if( vlsvReader.readParameter(
"vymax", vy_max ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
109 if( vlsvReader.readParameter(
"vzmin",
vz_min ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
110 if( vlsvReader.readParameter(
"vzmax", vz_max ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
115 const short int NumberOfCoordinates = 3;
116 uint64_t cell_bounds[NumberOfCoordinates];
117 uint64_t vcell_bounds[NumberOfCoordinates];
120 if( vlsvReader.readParameter(
"vxblocks_ini", vcell_bounds[0] ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
122 if( vlsvReader.readParameter(
"vyblocks_ini", vcell_bounds[1] ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
124 if( vlsvReader.readParameter(
"vzblocks_ini", vcell_bounds[2] ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
127 if( vlsvReader.readParameter(
"xcells_ini", cell_bounds[0] ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
129 if( vlsvReader.readParameter(
"ycells_ini", cell_bounds[1] ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
131 if( vlsvReader.readParameter(
"zcells_ini", cell_bounds[2] ) ==
false ) cerr <<
"FAILED TO READ PARAMETER AT " << __FILE__ <<
" " << __LINE__ << endl;
134 Real x_length = x_max - x_min;
135 Real y_length = y_max - y_min;
136 Real z_length = z_max - z_min;
138 Real vx_length = vx_max - vx_min;
139 Real vy_length = vy_max - vy_min;
142 for(
int i = 0;
i < NumberOfCoordinates; ++
i ) {
164 for(
int i = 0;
i < 3; ++
i ) {
166 cerr <<
"ERROR, ZERO CELL LENGTH OR CELL_BOUNDS AT " << __FILE__ <<
" " << __LINE__ << endl;
321 vlsv::datatype::type dataType;
322 uint64_t arraySize,vectorSize,dataSize;
323 list<pair<string, string> > xmlAttributes;
324 xmlAttributes.push_back(make_pair(
"name", varName));
325 xmlAttributes.push_back(make_pair(
"mesh", meshName));
326 if (vlsvReader.getArrayInfo(
"VARIABLE", xmlAttributes, arraySize, vectorSize, dataType, dataSize) ==
false)
return false;
329 char* buffer =
new char[arraySize*vectorSize*dataSize];
330 const short unsigned int startingIndex = 0;
331 if (vlsvReader.readArray(
"VARIABLE", xmlAttributes, startingIndex, arraySize, buffer ) ==
false) success =
false;
332 if (success ==
false) {
333 cerr <<
"FAILED TO READ VARIABLE AT " << __FILE__ <<
" " << __LINE__ << endl;
340 char** components =
new char*[vectorSize];
341 for (uint64_t
i=0;
i<vectorSize; ++
i) {
342 components[
i] =
new char[arraySize*dataSize];
343 for (uint64_t
j=0;
j<arraySize; ++
j)
for (uint64_t
k=0;
k<dataSize; ++
k)
344 components[
i][
j*dataSize+
k] = buffer[
j*vectorSize*dataSize +
i*dataSize +
k];
350 vector<string> varNames(vectorSize);
351 vector<char*> varNamePtrs(vectorSize);
352 for (uint64_t
i=0;
i<vectorSize; ++
i) {
354 ss << varName << (
i+1);
355 varNames[
i] = ss.str();
356 varNamePtrs[
i] =
const_cast<char*
>(varNames[
i].c_str());
360 if (DBPutUcdvar(
fileptr,varName.c_str(),meshName.c_str(),vectorSize,&(varNamePtrs[0]),components,arraySize,NULL,0,
SiloType(dataType,dataSize),DB_ZONECENT,NULL) < 0) success =
false;
362 for (uint64_t
i=0;
i<vectorSize; ++
i) {
363 delete [] components[
i];
365 delete [] components;
375 const float EPS = 1.0e-7;
381 map<NodeCrd<Real>,uint64_t,
NodeComp> nodes;
384 vector<uint64_t> cellIds;
385 if( vlsvReader.
getCellIds( cellIds ) ==
false ) {
386 cerr <<
"Failed to read cell ids at " << __FILE__ <<
" " << __LINE__ << endl;
400 for( vector<uint64_t>::const_iterator it = cellIds.begin(); it != cellIds.end(); ++it, ++
i ) {
407 const uint64_t cellId = *it;
408 array<Real, 3> coordinates;
413 Real X0 = coordinates[0];
415 Real Y0 = coordinates[1];
417 Real Z0 = coordinates[2];
421 if (fabs(X0) <
EPS) X0 = 0.0;
422 if (fabs(X1) <
EPS) X1 = 0.0;
423 if (fabs(Y0) <
EPS) Y0 = 0.0;
424 if (fabs(Y1) <
EPS) Y1 = 0.0;
425 if (fabs(Z0) <
EPS) Z0 = 0.0;
426 if (fabs(Z1) <
EPS) Z1 = 0.0;
437 if (success ==
false) {
438 cerr <<
"ERROR reading array COORDS" << endl;
443 uint64_t counter = 0;
444 Real* xcrds =
new Real[nodes.size()];
445 Real* ycrds =
new Real[nodes.size()];
446 Real* zcrds =
new Real[nodes.size()];
447 for (map<
NodeCrd<Real>,uint64_t>::iterator it=nodes.begin(); it!=nodes.end(); ++it) {
448 it->second = counter;
449 xcrds[counter] = it->first.x;
450 ycrds[counter] = it->first.y;
451 zcrds[counter] = it->first.z;
459 int* nodelist =
new int[8*cellIds.size()];
461 for( vector<uint64_t>::const_iterator it = cellIds.begin(); it != cellIds.end(); ++it, ++
i ) {
466 const uint64_t cellId = *it;
467 array<Real, 3> coordinates;
472 Real X0 = coordinates[0];
474 Real Y0 = coordinates[1];
476 Real Z0 = coordinates[2];
480 if (fabs(X0) <
EPS) X0 = 0.0;
481 if (fabs(X1) <
EPS) X1 = 0.0;
482 if (fabs(Y0) <
EPS) Y0 = 0.0;
483 if (fabs(Y1) <
EPS) Y1 = 0.0;
484 if (fabs(Z0) <
EPS) Z0 = 0.0;
485 if (fabs(Z1) <
EPS) Z1 = 0.0;
489 map<NodeCrd<Real>,uint64_t,
NodeComp>::const_iterator it2;
490 it2 = nodes.find(
NodeCrd<Real>(X0,Y0,Z0));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+0] = it2->second;
491 it2 = nodes.find(
NodeCrd<Real>(X1,Y0,Z0));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+1] = it2->second;
492 it2 = nodes.find(
NodeCrd<Real>(X1,Y1,Z0));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+2] = it2->second;
493 it2 = nodes.find(
NodeCrd<Real>(X0,Y1,Z0));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+3] = it2->second;
494 it2 = nodes.find(
NodeCrd<Real>(X0,Y0,Z1));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+4] = it2->second;
495 it2 = nodes.find(
NodeCrd<Real>(X1,Y0,Z1));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+5] = it2->second;
496 it2 = nodes.find(
NodeCrd<Real>(X1,Y1,Z1));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+6] = it2->second;
497 it2 = nodes.find(
NodeCrd<Real>(X0,Y1,Z1));
if (it2 == nodes.end()) success =
false; nodelist[
i*8+7] = it2->second;
501 if (success ==
false) {
502 cerr <<
"Failed to find node(s)" << endl;
506 const int N_dims = 3;
507 const int N_nodes = nodes.size();
508 const int N_zones = cellIds.size();
509 int shapeTypes[] = {DB_ZONETYPE_HEX};
510 int shapeSizes[] = {8};
511 int shapeCnt[] = {N_zones};
512 const int N_shapes = 1;
520 const string zoneListName = meshName +
"Zones";
521 if (DBPutZonelist2(
fileptr,zoneListName.c_str(),N_zones,N_dims,nodelist,8*cellIds.size(),0,0,0,shapeTypes,shapeSizes,shapeCnt,N_shapes,NULL) < 0) success =
false;
524 if (DBPutUcdmesh(
fileptr,meshName.c_str(),N_dims,NULL,coords,N_nodes,N_zones,zoneListName.c_str(),NULL,
SiloType(datatype::type::FLOAT,
sizeof(
Real)),NULL) < 0) success =
false;
533 string cellIDlabel =
"Cell ID";
534 DBoptlist* optList = DBMakeOptlist(1);
535 DBAddOption(optList,DBOPT_LABEL,
const_cast<char*
>(cellIDlabel.c_str()));
537 if (DBPutUcdvar1(
fileptr,
"CellID",meshName.c_str(),
reinterpret_cast<char*
>(cellIds.data()),cellIds.size(),NULL,0,
SiloType(datatype::type::UINT,
sizeof(uint64_t)),DB_ZONECENT,NULL) < 0) success =
false;
539 DBFreeOptlist(optList);
542 list<string> variables;
544 for (list<string>::const_iterator it=variables.begin(); it!=variables.end(); ++it) {
559 if (vlsvReader.open(fname) ==
false) {
560 cerr <<
"Failed to open '" << fname <<
"'" << endl;
566 size_t found=fname.find_last_of(
"/\\");
568 string fileout = fname.substr(found+1);
569 size_t pos = fileout.rfind(
".vlsv");
570 if (pos != string::npos) fileout.replace(pos,5,
".silo");
572 fileptr = DBCreate(fileout.c_str(),DB_CLOBBER,DB_LOCAL,
"Vlasov data file",DB_PDB);
573 if (
fileptr == NULL)
return false;
576 list<string> meshNames;
577 if (vlsvReader.getMeshNames(meshNames) ==
false) {
581 for (list<string>::const_iterator it=meshNames.begin(); it!=meshNames.end(); ++it) {
592int main(
int argn,
char* args[]) {
594 MPI_Init(&argn, &args);
595 MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
596 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
598 if (rank == 0 && argn < 2) {
600 cout <<
"USAGE: ./vlsv2vtk <input file mask(s)>" << endl;
601 cout <<
"Each VLSV in the current directory is compared against the given file mask(s)," << endl;
602 cout <<
"and if match is found, that file is converted into SILO format." << endl;
608 vector<string> masks, fileList;
609 for (
int i=1;
i<argn; ++
i) masks.push_back(args[
i]);
613 const string suffix =
".vlsv";
614 int filesFound = 0, filesConverted = 0;
615 for (
size_t mask=0; mask<masks.size(); ++mask) {
616 size_t found=masks[mask].find_last_of(
"/\\");
617 string directory=
".";
618 if(found != string::npos)
619 directory = masks[mask].substr(0,found);
620 const string maskName = masks[mask].substr(found+1);
622 if(rank == 0) {cout <<
"Comparing mask '" << maskName <<
"' in folder '" << directory <<
"'" << endl;}
623 DIR* dir = opendir(directory.c_str());
624 if (dir == NULL)
continue;
626 struct dirent* entry = readdir(dir);
627 while (entry != NULL) {
628 const string entryName = entry->d_name;
630 if (entryName.find(maskName) == string::npos || entryName.find(suffix) == string::npos) {
631 entry = readdir(dir);
635 fileList.push_back(directory);
636 fileList.back().append(
"/");
637 fileList.back().append(entryName);
639 entry = readdir(dir);
642 if (rank == 0 && filesFound == 0) cout <<
"\t no matches found" << endl;
645 for(
size_t entryName = 0; entryName < fileList.size(); entryName++) {
646 if(entryName%ntasks == (uint)rank) {
647 cout <<
"\tProc " << rank <<
" converting '" << fileList[entryName] <<
"'" << endl;
653 int totalFilesConverted =0;
654 MPI_Reduce(&filesConverted, &totalFilesConverted, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
655 if (rank == 0 && totalFilesConverted == 0) cout <<
"\t no files converted" << endl;