PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
reader.cpp
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2026 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#include "reader.h"
12#include "util/io.h"
13#include <stdexcept>
14#include "mshReader.h"
15#include "vtkReader.h"
16#include <csv/csv.h>
17
18void rw::reader::readCsvFile(const std::string &filename, size_t dim,
19 std::vector<util::Point> *nodes,
20 std::vector<double> *volumes) {
21 nodes->clear();
22 volumes->clear();
23 if (dim == 1) {
24 io::CSVReader<3> in(filename);
25 in.read_header(io::ignore_extra_column, "id", "x", "volume");
26
27 double x;
28 double volume;
29 int id;
30 while (in.read_row(id, x, volume)) {
31 volumes->emplace_back(volume);
32 nodes->emplace_back(util::Point(x, 0., 0.));
33 }
34 }
35
36 if (dim == 2) {
37 io::CSVReader<4> in(filename);
38 in.read_header(io::ignore_extra_column, "id", "x", "y", "volume");
39
40 double x, y, volume;
41 int id;
42 while (in.read_row(id, x, y, volume)) {
43 volumes->emplace_back(volume);
44 nodes->emplace_back(util::Point(x, y, 0.));
45 }
46 }
47
48 if (dim == 3) {
49 io::CSVReader<5> in(filename);
50 in.read_header(io::ignore_extra_column, "id", "x", "y", "z", "volume");
51
52 double x, y, z, volume;
53 int id;
54 while (in.read_row(id, x, y, z, volume)) {
55 volumes->emplace_back(volume);
56 nodes->emplace_back(util::Point(x, y, z));
57 }
58 }
59}
60
61void rw::reader::readParticleCsvFile(const std::string &filename, size_t dim,
62 std::vector<util::Point> *nodes,
63 std::vector<double> *rads,
64 std::vector<size_t> *zones) {
65
66 nodes->clear();
67 zones->clear();
68 rads->clear();
69
70 io::CSVReader<5> in(filename);
71 in.read_header(io::ignore_extra_column, "i", "x", "y", "z", "r");
72
73 double x, y, z, r;
74 int id;
75 while (in.read_row(id, x, y, z, r)) {
76 rads->emplace_back(r);
77 nodes->emplace_back(util::Point(x, y, z));
78 zones->emplace_back(id);
79 }
80}
81
82void rw::reader::readParticleCsvFile(const std::string &filename, size_t dim,
83 std::vector<util::Point> *nodes,
84 std::vector<double> *rads,
85 const size_t &zone) {
86
87 nodes->clear();
88 rads->clear();
89
90 io::CSVReader<5> in(filename);
91 in.read_header(io::ignore_extra_column, "i", "x", "y", "z", "r");
92
93 double x, y, z, r;
94 int id;
95 while (in.read_row(id, x, y, z, r)) {
96 if (id == zone) {
97 rads->emplace_back(r);
98 nodes->emplace_back(util::Point(x, y, z));
99 }
100 }
101}
102
103void rw::reader::readParticleWithOrientCsvFile(const std::string &filename, size_t dim,
104 std::vector<util::Point> *nodes,
105 std::vector<double> *rads,
106 std::vector<double> *orients,
107 const size_t &zone) {
108
109 nodes->clear();
110 rads->clear();
111 orients->clear();
112
113 io::CSVReader<6> in(filename);
114 in.read_header(io::ignore_extra_column, "i", "x", "y", "z", "r", "o");
115
116 double x, y, z, r, o;
117 int id;
118 while (in.read_row(id, x, y, z, r, o)) {
119 if (id == zone) {
120 rads->emplace_back(r);
121 nodes->emplace_back(util::Point(x, y, z));
122 orients->emplace_back(o);
123 }
124 }
125}
126
127void rw::reader::readVtuFile(const std::string &filename, size_t dim,
128 std::vector<util::Point> *nodes,
129 size_t &element_type, size_t &num_elem,
130 std::vector<size_t> *enc,
131 std::vector<std::vector<size_t>> *nec,
132 std::vector<double> *volumes, bool is_fd) {
133 // call vtk reader
134 auto rdr = rw::reader::VtkReader(filename);
135 rdr.readMesh(dim, nodes, element_type, num_elem, enc, nec, volumes, is_fd);
136 rdr.close();
137}
138
139void rw::reader::readVtuFileNodes(const std::string &filename, size_t dim,
140 std::vector<util::Point> *nodes,
141 bool ref_config) {
142 // call vtk reader
143 auto rdr = rw::reader::VtkReader(filename);
144
145 // below will read the current position of nodes
146 rdr.readNodes(nodes);
147
148 // need to subtract the displacement to get reference configuration of nodes
149 if (ref_config) {
150 std::vector<util::Point> u;
151 if (rdr.readPointData("Displacement", &u)) {
152 throw std::runtime_error(
154 << "Error: Did not find displacement in the vtu file."
155 << std::endl);
156 }
157
158 if (u.size() != nodes->size()) {
159 throw std::runtime_error(
161 << "Error: Displacement data and node data size do not match."
162 << std::endl);
163 }
164
165 for (size_t i = 0; i < u.size(); i++)
166 (*nodes)[i] -= u[i];
167 }
168
169 rdr.close();
170}
171
172void rw::reader::readVtuFileCells(const std::string &filename, size_t dim,
173 size_t &element_type, size_t &num_elem,
174 std::vector<size_t> *enc,
175 std::vector<std::vector<size_t>> *nec) {
176 // call vtk reader
177 auto rdr = rw::reader::VtkReader(filename);
178
179 // below will read the current position of nodes
180 rdr.readCells(dim, element_type, num_elem, enc, nec);
181
182 rdr.close();
183}
184
185void rw::reader::readVtuFileRestart(const std::string &filename,
186 std::vector<util::Point> *u,
187 std::vector<util::Point> *v,
188 const std::vector<util::Point> *X) {
189 // call vtk reader
190 auto rdr = rw::reader::VtkReader(filename);
191 // if displacement is not in input file, use reference coordinate to get
192 // displacement
193 if (!rdr.readPointData("Displacement", u)) {
194 std::vector<util::Point> y;
195 rdr.readNodes(&y);
196 if (y.size() != X->size()) {
197 throw std::runtime_error(
199 << "Error: Number of nodes in input file = " << filename
200 << " and number nodes in data X are not same.\n");
201 }
202
203 u->resize(y.size());
204 for (size_t i = 0; i < y.size(); i++)
205 (*u)[i] = y[i] - (*X)[i];
206 }
207
208 // get velocity
209 rdr.readPointData("Velocity", v);
210 rdr.close();
211}
212
213bool rw::reader::readVtuFilePointData(const std::string &filename,
214 const std::string &tag,
215 std::vector<uint8_t> *data) {
216 // call vtk reader
217 auto rdr = rw::reader::VtkReader(filename);
218 // read data
219 auto st = rdr.readPointData(tag, data);
220 rdr.close();
221 return st;
222}
223
224bool rw::reader::readVtuFilePointData(const std::string &filename,
225 const std::string &tag,
226 std::vector<size_t> *data) {
227 // call vtk reader
228 auto rdr = rw::reader::VtkReader(filename);
229 // read data
230 auto st = rdr.readPointData(tag, data);
231 rdr.close();
232 return st;
233}
234
235bool rw::reader::readVtuFilePointData(const std::string &filename,
236 const std::string &tag,
237 std::vector<int> *data) {
238 // call vtk reader
239 auto rdr = rw::reader::VtkReader(filename);
240 // read data
241 auto st = rdr.readPointData(tag, data);
242 rdr.close();
243 return st;
244}
245
246bool rw::reader::readVtuFilePointData(const std::string &filename,
247 const std::string &tag,
248 std::vector<float> *data) {
249 // call vtk reader
250 auto rdr = rw::reader::VtkReader(filename);
251 // read data
252 auto st = rdr.readPointData(tag, data);
253 rdr.close();
254 return st;
255}
256
257bool rw::reader::readVtuFilePointData(const std::string &filename,
258 const std::string &tag,
259 std::vector<double> *data) {
260 // call vtk reader
261 auto rdr = rw::reader::VtkReader(filename);
262 // read data
263 auto st = rdr.readPointData(tag, data);
264 rdr.close();
265 return st;
266}
267
268bool rw::reader::readVtuFilePointData(const std::string &filename,
269 const std::string &tag,
270 std::vector<util::Point> *data) {
271 // call vtk reader
272 auto rdr = rw::reader::VtkReader(filename);
273 // read data
274 auto st = rdr.readPointData(tag, data);
275 rdr.close();
276 return st;
277}
278
279bool rw::reader::readVtuFilePointData(const std::string &filename,
280 const std::string &tag,
281 std::vector<util::SymMatrix3> *data) {
282 // call vtk reader
283 auto rdr = rw::reader::VtkReader(filename);
284 // read data
285 auto st = rdr.readPointData(tag, data);
286 rdr.close();
287 return st;
288}
289
290bool rw::reader::readVtuFilePointData(const std::string &filename,
291 const std::string &tag,
292 std::vector<util::Matrix3> *data) {
293 // call vtk reader
294 auto rdr = rw::reader::VtkReader(filename);
295 // read data
296 auto st = rdr.readPointData(tag, data);
297 rdr.close();
298 return st;
299}
300
301bool rw::reader::readVtuFileCellData(const std::string &filename,
302 const std::string &tag,
303 std::vector<float> *data) {
304 // call vtk reader
305 auto rdr = rw::reader::VtkReader(filename);
306 // read data
307 auto st = rdr.readCellData(tag, data);
308 rdr.close();
309 return st;
310}
311
312bool rw::reader::readVtuFileCellData(const std::string &filename,
313 const std::string &tag,
314 std::vector<double> *data) {
315 // call vtk reader
316 auto rdr = rw::reader::VtkReader(filename);
317 // read data
318 auto st = rdr.readCellData(tag, data);
319 rdr.close();
320 return st;
321}
322
323bool rw::reader::readVtuFileCellData(const std::string &filename,
324 const std::string &tag,
325 std::vector<util::Point> *data) {
326 // call vtk reader
327 auto rdr = rw::reader::VtkReader(filename);
328 // read data
329 auto st = rdr.readCellData(tag, data);
330 rdr.close();
331 return st;
332}
333
334bool rw::reader::readVtuFileCellData(const std::string &filename,
335 const std::string &tag,
336 std::vector<util::SymMatrix3> *data) {
337 // call vtk reader
338 auto rdr = rw::reader::VtkReader(filename);
339 // read data
340 auto st = rdr.readCellData(tag, data);
341 rdr.close();
342 return st;
343}
344
345bool rw::reader::readVtuFileCellData(const std::string &filename,
346 const std::string &tag,
347 std::vector<util::Matrix3> *data) {
348 // call vtk reader
349 auto rdr = rw::reader::VtkReader(filename);
350 // read data
351 auto st = rdr.readCellData(tag, data);
352 rdr.close();
353 return st;
354}
355
356void rw::reader::readMshFile(const std::string &filename, size_t dim,
357 std::vector<util::Point> *nodes,
358 size_t &element_type, size_t &num_elem,
359 std::vector<size_t> *enc,
360 std::vector<std::vector<size_t>> *nec,
361 std::vector<double> *volumes, bool is_fd) {
362 // call msh reader
363 auto rdr = rw::reader::MshReader(filename);
364 rdr.readMesh(dim, nodes, element_type, num_elem, enc, nec, volumes, is_fd);
365 rdr.close();
366}
367
368void rw::reader::readMshFileRestart(const std::string &filename,
369 std::vector<util::Point> *u,
370 std::vector<util::Point> *v,
371 const std::vector<util::Point> *X) {
372 // call msh reader
373 auto rdr = rw::reader::MshReader(filename);
374 // if displacement is not in input file, use reference coordinate to get
375 // displacement
376 if (!rdr.readPointData("Displacement", u)) {
377 std::vector<util::Point> y;
378 rdr.readNodes(&y);
379 if (y.size() != X->size()) {
380 throw std::runtime_error(
382 << "Error: Number of nodes in input file = " << filename
383 << " and number nodes in data X are not same.\n");
384 }
385
386 u->resize(y.size());
387 for (size_t i = 0; i < y.size(); i++)
388 (*u)[i] = y[i] - (*X)[i];
389 }
390
391 // get velocity
392 rdr.readPointData("Velocity", v);
393 rdr.close();
394}
395
396bool rw::reader::readMshFilePointData(const std::string &filename,
397 const std::string &tag,
398 std::vector<double> *data) {
399 // call msh reader
400 auto rdr = rw::reader::MshReader(filename);
401 // get velocity
402 auto st = rdr.readPointData(tag, data);
403 rdr.close();
404 return st;
405}
406
407void rw::reader::readMshFileCells(const std::string &filename, size_t dim,
408 size_t &element_type, size_t &num_elem,
409 std::vector<size_t> *enc,
410 std::vector<std::vector<size_t>> *nec) {
411 // call msh reader
412 auto rdr = rw::reader::MshReader(filename);
413 rdr.readCells(dim, element_type, num_elem, enc, nec);
414 rdr.close();
415}
A class to read Gmsh (msh) mesh files.
Definition mshReader.h:28
A class to read VTK (.vtu) mesh files.
Definition vtkReader.h:32
Collects a message with stream syntax for use in an exception.
Definition io.h:52
Definition contact.h:20
bool readMshFilePointData(const std::string &filename, const std::string &tag, std::vector< double > *data)
Reads data of specified tag from the vtu file.
Definition reader.cpp:396
void readMshFileRestart(const std::string &filename, std::vector< util::Point > *u, std::vector< util::Point > *v, const std::vector< util::Point > *X=nullptr)
Reads mesh data into node file and element file.
Definition reader.cpp:368
void readVtuFileRestart(const std::string &filename, std::vector< util::Point > *u, std::vector< util::Point > *v, const std::vector< util::Point > *X=nullptr)
Reads mesh data into node file and element file.
Definition reader.cpp:185
void readVtuFileCells(const std::string &filename, size_t dim, size_t &element_type, size_t &num_elem, std::vector< size_t > *enc, std::vector< std::vector< size_t > > *nec)
Reads cell data, i.e. element-node connectivity and node-element connectivity.
Definition reader.cpp:172
bool readVtuFileCellData(const std::string &filename, const std::string &tag, std::vector< float > *data)
Reads data of specified tag from the vtu file.
Definition reader.cpp:301
void readParticleWithOrientCsvFile(const std::string &filename, size_t dim, std::vector< util::Point > *nodes, std::vector< double > *rads, std::vector< double > *orients, const size_t &zone)
Reads particles center location, radius, and zone id. In this case, file also provides initial orient...
Definition reader.cpp:103
void readParticleCsvFile(const std::string &filename, size_t dim, std::vector< util::Point > *nodes, std::vector< double > *rads, std::vector< size_t > *zones)
Reads particles center location, radius, and zone id.
Definition reader.cpp:61
void readVtuFile(const std::string &filename, size_t dim, std::vector< util::Point > *nodes, size_t &element_type, size_t &num_elem, std::vector< size_t > *enc, std::vector< std::vector< size_t > > *nec, std::vector< double > *volumes, bool is_fd=false)
Reads mesh data into node file and element file.
Definition reader.cpp:127
void readMshFileCells(const std::string &filename, size_t dim, size_t &element_type, size_t &num_elem, std::vector< size_t > *enc, std::vector< std::vector< size_t > > *nec)
Reads cell data, i.e. element-node connectivity and node-element connectivity.
Definition reader.cpp:407
bool readVtuFilePointData(const std::string &filename, const std::string &tag, std::vector< uint8_t > *data)
Reads data of specified tag from the vtu file.
Definition reader.cpp:213
void readVtuFileNodes(const std::string &filename, size_t dim, std::vector< util::Point > *nodes, bool ref_config=false)
Reads nodal coordinates.
Definition reader.cpp:139
void readCsvFile(const std::string &filename, size_t dim, std::vector< util::Point > *nodes, std::vector< double > *volumes)
Reads mesh data into node file and element file.
Definition reader.cpp:18
void readMshFile(const std::string &filename, size_t dim, std::vector< util::Point > *nodes, size_t &element_type, size_t &num_elem, std::vector< size_t > *enc, std::vector< std::vector< size_t > > *nec, std::vector< double > *volumes, bool is_fd=false)
Reads mesh data into node file and element file.
Definition reader.cpp:356
A structure to represent 3d vectors.
Definition point.h:30