From d7e358c35978efe32babaa2cb51ad40707506d9b Mon Sep 17 00:00:00 2001 From: sjplimp Date: Tue, 4 Aug 2015 20:56:54 +0000 Subject: [PATCH] git-svn-id: svn://svn.icms.temple.edu/lammps-ro/trunk@13833 f3b2605a-c512-4ea7-a41b-209d697bcdaa --- tools/smd/Makefile | 7 ++ tools/smd/dump2vtk_tris.cpp | 235 ++++++++++++++++++++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 tools/smd/Makefile create mode 100644 tools/smd/dump2vtk_tris.cpp diff --git a/tools/smd/Makefile b/tools/smd/Makefile new file mode 100644 index 0000000000..618bd0ab39 --- /dev/null +++ b/tools/smd/Makefile @@ -0,0 +1,7 @@ +all: + clang++ -o dump2vtk_tris dump2vtk_tris.cpp -I/usr/include/vtk-5.8/ -lvtkCommon -lvtkIO -lvtkFiltering -lvtkRendering + + +clean: + rm -f *.o + diff --git a/tools/smd/dump2vtk_tris.cpp b/tools/smd/dump2vtk_tris.cpp new file mode 100644 index 0000000000..2486f8c4e3 --- /dev/null +++ b/tools/smd/dump2vtk_tris.cpp @@ -0,0 +1,235 @@ +/* + * convert a LAMMPS / Smooth-Mach Dynamics triangle output file into a VTK unstructured grid file. + * LAMMPS output file contains STL triangle vertices + * + * ASSUMPTIONS: + * + * A compute exists for and STL triangulated surface, which computes the triangl vertices with a command like this: + * compute F tri_group smd/triangle_vertices + * c_F[1-9] now hold the triangle vertices. + * + * LAMMPS dump file has the following entries per atom line: + * id type mol x y z c_F[1] c_F[2] c_F[3] c_F[4] c_F[5] c_F[6] c_F[7] c_F[8] c_F[9] + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace std; + +#define RENDER 0 // switch on / off rendering of snapshots + +void WriteVTU(std::string filename, int ntime, vtkSmartPointer unstructuredGrid) { + vtkSmartPointer writer = vtkSmartPointer::New(); + // create filename with the current timestep + std::ostringstream oss; + oss << ntime; + filename += "." + oss.str() + ".vtu"; + + writer->SetFileName(filename.c_str()); + #if VTK_MAJOR_VERSION <= 5 + writer->SetInput(unstructuredGrid); + #else + writer->SetInputData(unstructuredGrid); + #endif + writer->Write(); +} + +void WriteVTK(std::string filename, int ntime, vtkSmartPointer unstructuredGrid) { + vtkSmartPointer writer = vtkSmartPointer::New(); + // create filename with the current timestep + std::ostringstream oss; + oss << ntime; + filename += "_" + oss.str() + ".vtk"; + + writer->SetFileName(filename.c_str()); + #if VTK_MAJOR_VERSION <= 5 + writer->SetInput(unstructuredGrid); + #else + writer->SetInputData(unstructuredGrid); + #endif + writer->Write(); +} + +int main(int argc, char *argv[]) { + + string line; + vtkSmartPointer points; + vtkSmartPointer vertices; + vtkSmartPointer cellArray; + vtkIdType id_v1, id_v2, id_v3; + vtkSmartPointer tri = vtkSmartPointer::New(); // create an empty tri; + vtkSmartPointer unstructuredGrid; + + + unstructuredGrid = vtkSmartPointer::New(); + + // Parse command line arguments + if (argc != 3) { + std::cout << "Required arguments: InputFilename OutputFile_Basename" + << std::endl; + return EXIT_FAILURE; + } + + std::string lammps_dump = argv[1]; + std::string filename = argv[2]; + + ifstream myfile(lammps_dump.c_str()); + + int nlines_read = 0; + int count = -1; + int ntime; + int N; // number of atoms in this timestep + int id, type, mol; + double x, y, z, v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z; + if (myfile.is_open()) { + + while (getline(myfile, line)) { + std::size_t found = line.find("TIMESTEP"); + if (found != std::string::npos) { + cout << line << endl; + count = 0; + nlines_read = 0; + + // reinitialize VTK arrays so they are empty + points = vtkSmartPointer::New(); + vertices = vtkSmartPointer::New(); + cellArray = vtkSmartPointer::New(); + } + if (count >= 0) + count++; + + if (count == 2) { + istringstream convert(line); + convert >> ntime; + cout << "timestep is: " << ntime << endl; + } + + if (count == 4) { + istringstream convert(line); + convert >> N; + cout << "number of particles is: " << N << endl; + } + + if (count > 9) { + //cout << line << endl; + //const vector words = split(line, " "); + + std::istringstream is(line); + std::istream_iterator eos; + std::vector out(std::istream_iterator(is), eos); + + /* + * we expect the following entries in a data line: + * id type mol x y z v1x v1y v1z v2x v2y v2z v3x v3y v3z + */ + + id = out[0]; + type = out[1]; + mol = out[2]; + x = out[3]; // triangle center + y = out[4]; + z = out[5]; + v1x = out[6]; // triangle vertex 1 + v1y = out[7]; + v1z = out[8]; + v2x = out[9]; // triangle vertex 2 + v2y = out[10]; + v2z = out[11]; + v3x = out[12]; // triangle vertex 3 + v3y = out[13]; + v3z = out[14]; + + id_v1 = points->InsertNextPoint(v1x, v1y, v1z); // store tri vertices in points array and retain indices + id_v2 = points->InsertNextPoint(v2x, v2y, v2z); + id_v3 = points->InsertNextPoint(v3x, v3y, v3z); + + + + tri->GetPointIds()->SetId(0, id_v1); // fill triangle with retained indices + tri->GetPointIds()->SetId(1, id_v2); + tri->GetPointIds()->SetId(2, id_v3); + + cellArray->InsertNextCell(tri); // insert tri into array of cells + + nlines_read += 1; + + } + + if (count % (9 + N) == 0) { // we have reached the end of this snapshot + printf( + "----------------------------------------------------\n"); + printf("number of lines read from lammps dump file: %d\n", + nlines_read); + + + unstructuredGrid->SetPoints(points); + unstructuredGrid->SetCells(VTK_TRIANGLE, cellArray); + + WriteVTK(filename, ntime, unstructuredGrid); + + +// if (ntime > 100) { +// return 0; +// } + +#if RENDER == 1 + // Read and display file for verification that it was written correclty + vtkSmartPointer reader = + vtkSmartPointer::New(); + reader->SetFileName(filename.c_str()); + reader->Update(); + + vtkSmartPointer mapper = vtkSmartPointer< + vtkDataSetMapper>::New(); + mapper->SetInputConnection(reader->GetOutputPort()); + + vtkSmartPointer actor = + vtkSmartPointer::New(); + actor->SetMapper(mapper); + + vtkSmartPointer renderer = vtkSmartPointer< + vtkRenderer>::New(); + vtkSmartPointer renderWindow = vtkSmartPointer< + vtkRenderWindow>::New(); + renderWindow->AddRenderer(renderer); + vtkSmartPointer renderWindowInteractor = + vtkSmartPointer::New(); + renderWindowInteractor->SetRenderWindow(renderWindow); + + renderer->AddActor(actor); + renderer->SetBackground(.3, .6, .3); // Background color green + + renderWindow->Render(); + renderWindowInteractor->Start(); +#endif + } + + } + myfile.close(); + } else { + cout << "Unable to open file"; + } + + return EXIT_SUCCESS; +}