PeriDEM 0.3.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
vtkParticleReader.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 "vtkParticleReader.h"
12#include <stdexcept>
13#include "util/feElementDefs.h"
14#include "util/io.h"
15#include "mesh/mesh.h"
16#include "util/vecMethods.h"
17#include "data/modelData.h"
19
20#include <cstdint>
21
22#include <vtkAbstractArray.h>
23#include <vtkCellArray.h>
24#include <vtkCellData.h>
25#include <vtkDoubleArray.h>
26#include <vtkIdList.h>
27#include <vtkIntArray.h>
28#include <vtkPointData.h>
29#include <vtkPoints.h>
30#include <vtkUnsignedCharArray.h>
31#include <vtkUnsignedIntArray.h>
32
34
35 auto f = util::io::checkAndCreateNewFilename(filename, "vtu");
36
37 d_reader_p = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
38 d_reader_p->SetFileName(const_cast<char *>(f.c_str()));
39 d_reader_p->Update();
40}
41
43 data::ModelData *model) {
44
45 if (model->d_x.size() == 0)
46 return;
47
48 //
49 // For nodes, we read following data
50 // 1. nodes current configuration
51 // 2. nodes displacement (if not then compute from current and reference
52 // config)
53 // 3. nodes velocity (if not then produce error)
54 //
55 d_grid_p = d_reader_p->GetOutput();
56 vtkIdType num_nodes = d_grid_p->GetNumberOfPoints();
57
58 if (num_nodes != model->d_x.size()) {
59 throw std::runtime_error(
61 << "Error: Number of points data = " << num_nodes <<
62 " in file does not match the number of points = "
63 << model->d_x.size() << ". Aborting reading.\n");
64 }
65
66 // read current configuration
67 for (size_t i = 0; i < num_nodes; i++) {
68 vtkIdType id = i;
69
70 double x[3];
71 d_grid_p->GetPoint(id, x);
72
73 util::Point i_node = util::Point(x[0], x[1], x[2]);
74 model->setX(i, i_node);
75 }
76
77 // read point field data
78 vtkPointData *p_field = d_grid_p->GetPointData();
79
80 std::vector<std::string> tags = {"Displacement", "Velocity"};
81 for (auto tag: tags) {
82
83 if (p_field->HasArray(tag.c_str())) {
84 vtkDataArray *array = p_field->GetArray(tag.c_str());
85
86 auto data_a = vtkSmartPointer<vtkDoubleArray>::New();
87 data_a->SetNumberOfComponents(3);
88 data_a->Allocate(3, 1); // allocate memory
89
90 for (size_t i = 0; i < array->GetNumberOfTuples(); i++) {
91 array->GetTuples(i, i, data_a);
92 auto xi = util::Point(data_a->GetValue(0), data_a->GetValue(1),
93 data_a->GetValue(2));
94 if (tag == "Displacement")
95 model->setU(i, xi);
96 else if (tag == "Velocity")
97 model->setV(i, xi);
98 }
99 } else {
100
101 if (tag == "Velocity") {
102 throw std::runtime_error(
104 << "Error: Restart file does not have <Velocity> data. "
105 "Aborting reading.\n");
106 } else if (tag == "Displacement") {
107 // compute from current and reference configuration
108 for (size_t i=0; i<model->d_x.size(); i++)
109 model->setU(i, model->getX(i) - model->getXRef(i));
110 }
111 }
112 }
113}
114
115
117 // delete d_reader_p;
118 // delete d_grid_p;
119}
A class to store model data.
Definition modelData.h:50
std::vector< util::Point > d_x
Current positions of the nodes.
Definition modelData.h:745
void setU(size_t i, const util::Point &u)
Set displacement of the node.
Definition modelData.h:342
util::Point & getX(size_t i)
Get current coordinate of the node.
Definition modelData.h:285
util::Point & getXRef(size_t i)
Get reference coordinate of the node.
Definition modelData.h:238
void setV(size_t i, const util::Point &v)
Set velocity of the node.
Definition modelData.h:389
void setX(size_t i, const util::Point &x)
Set current coordinate of the node.
Definition modelData.h:295
void readNodes(data::ModelData *model)
Writes the nodes to the file.
VtkParticleReader(const std::string &filename)
Constructor.
void close()
Closes the file and store it to the hard disk.
vtkSmartPointer< vtkXMLUnstructuredGridReader > d_reader_p
XML unstructured grid writer.
Collects a message with stream syntax for use in an exception.
Definition io.h:52
std::string checkAndCreateNewFilename(std::string const &filename, std::string filename_ext)
Check for extension and if possible create new filename from a given filename and a given extension.
Definition io.h:366
A structure to represent 3d vectors.
Definition point.h:30