MoCSI API Reference
Loading...
Searching...
No Matches
GridOneDim.h
Go to the documentation of this file.
1#ifndef GRID_ONE_DIM_H
2#define GRID_ONE_DIM_H
3
4#include <algorithm>
5#include <memory>
6#include <string>
7#include <vector>
8
10#include "CellRegistry.h"
11#include "CsvParser.h"
13#include "GenericElement.h"
14#include "GridBase.h"
15#include "IniParser.h"
16#include "ShapeBase.h"
17#include "SimulationClassBase.h"
18
23template <typename T>
24class GridOneDim : public GridBase<T>
25{
26 public:
27 void createGrid() override;
28
30
32 ~GridOneDim() = default;
33
34 private:
35 void setupBoundaryCell(const std::vector<int>& node_numbers,
36 std::string_view boundary_condition_type,
37 const std::map<std::string_view, std::vector<int>>& boundary_points,
38 int current_facet, T areas, const std::string& face_prefix);
39 void incrementVector(std::vector<int>& vec);
40 int getNumberOfFacets() const;
41 std::vector<T> getGridArray(int number_of_elements);
42};
43
49template <typename T>
55
61template <typename T>
62void GridOneDim<T>::incrementVector(std::vector<int>& vec)
63{
64 for (auto& val : vec)
65 {
66 val++;
67 }
68}
69
78template <typename T>
80{
81 bool depth_present{false};
82 bool length_present{false};
83 double cl_depth{};
84 double cl_length{};
85 try
86 {
87 int numerical_layers{this->sim->m_simulation_config.getIntParameters("numerical_layers")};
88 double max_depth{this->sim->m_simulation_config.getDoubleParameters("max_depth")};
89 cl_depth = max_depth / static_cast<double>(numerical_layers);
90 depth_present = true;
91 }
92 catch (const BadInput&)
93 {
94 }
95 try
96 {
97 cl_length = this->sim->m_simulation_config.getDoubleParameters("cell_length");
98 length_present = true;
99 }
100 catch (const BadInput&)
101 {
102 }
103
104 if (depth_present && !length_present && cl_depth >= 0)
105 {
106 return cl_depth;
107 }
108 if (length_present && !depth_present && cl_length >= 0)
109 {
110 return cl_length;
111 }
112 else if (!length_present && !depth_present)
113 {
114 std::cerr << "[GridOneDim]: Could not find the entries 'cell_length' or 'max_depth' within "
115 "the ini file. Please "
116 "supply one of these entries!\n";
117 throw std::runtime_error(
118 static_cast<std::string>("[GridOneDim]: Could not find the entries 'cell_length' or "
119 "'max_depth' within the ini file. Please "
120 "supply one of these entries!"));
121 }
122 else
123 {
124 const double epsilon{1e-8};
125 if (std::abs(cl_length - cl_depth) <= epsilon
126 || std::abs(cl_length - cl_depth)
127 <= epsilon * std::max(std::abs(cl_depth), std::abs(cl_length)))
128 {
129 return cl_length;
130 }
131 std::cerr
132 << "[GridOneDim]: Both entries 'cell_length' and 'max_depth' are present within the "
133 "ini file and result "
134 "in different lengths. Please only supply one of these entries or have them produce "
135 "the same result!\n";
136 throw std::runtime_error(static_cast<std::string>(
137 "[GridOneDim]: Both entries 'cell_length' and 'max_depth' are present within the ini "
138 "file and result "
139 "in different lengths. Please only supply one of these entries or have them produce "
140 "the same result!"));
141 }
142}
143
151template <typename T>
153{
154 try
155 {
156 CsvParser grid_data{this->sim->m_simulation_config.getStringParameters("grid")};
157 std::vector<T> depth{grid_data.getCsvContents()[0]};
158 std::vector<T> length_arr;
159 for (int i{1}; i < depth.size(); i++)
160 {
161 length_arr.push_back(depth[i] - depth[i - 1]);
162 }
163 // Overwrite the numerical_layers parameter with the one given by the grid file
164 if (this->sim->m_simulation_config.getIntParameters("numerical_layers") != depth.size())
165 {
166 std::cerr
167 << "[GridOneDim]: Warning - Missmatch between number of numerical layers within "
168 "provided file "
169 "and given value in the ini! The value in the provided file will be used!\n";
170 this->sim->m_simulation_config.forceValueOverwrite("numerical_layers",
171 std::to_string(depth.size()));
172 }
173 return std::move(length_arr);
174 }
175 catch (const BadInput&)
176 {
177 std::vector<T> length_arr(number_of_elements, calculateCellLength());
178 length_arr[0] = length_arr[0] / 2;
179 return length_arr;
180 }
181}
182
195template <typename T>
197 const std::vector<int>& node_numbers, std::string_view boundary_condition_type,
198 const std::map<std::string_view, std::vector<int>>& boundary_points, int current_facet, T areas,
199 const std::string& face_prefix)
200{
201 std::vector<std::vector<int>> connected_points({{node_numbers[0], node_numbers[0] + 1}});
202 std::map<std::string_view, std::unique_ptr<GenericBoundaryCondition<T>>> boundary_condition;
206 this->all_cells[this->all_cells.size() - 1]->setBoundaryConditions(boundary_condition);
207}
208
215template <typename T>
217{
218#ifdef MPI_PRESENT
219 try
220 {
221 return this->sim->m_simulation_config.getIntParameters("number_of_facets");
222 }
223 catch (const BadInput& e)
224 {
225 std::cerr << "[GridOneDim]: " << e.what() << '\n';
226 throw BadInput(static_cast<std::string>("[GridOneDim]: ")
227 + static_cast<std::string>(e.what()));
228 }
229#else
230 return static_cast<int>(this->m_poly_mesh->getNumberOfFacets());
231#endif
232}
233
241template <typename T>
243{
246 std::vector<T> length_arr;
247 std::valarray<T> cell_length;
249 try
250 {
251 cell_type = this->sim->m_simulation_config.getStringParameters("cell_type");
253 this->sim->m_simulation_config.getStringParameters("top_boundary_condition_type");
255 this->sim->m_simulation_config.getStringParameters("bottom_boundary_condition_type");
257 this->sim->m_simulation_config.getIntParameters("numerical_layers") - 1;
258 nr_of_facets = getNumberOfFacets();
259 length_arr = getGridArray(number_of_elements);
260
261 // This is ugly, please revise
263 for (int i{0}; i < number_of_elements; i++)
264 {
266 }
267 this->sim->m_field_map.insert({"CellLength", cell_length});
268
269 this->grid_size =
270 nr_of_facets * this->sim->m_simulation_config.getIntParameters("numerical_layers");
271 }
272 catch (const BadInput& e)
273 {
274 std::cerr << "[GridOneDim]: " << e.what() << '\n';
275 throw BadInput(static_cast<std::string>("[GridOneDim]: ")
276 + static_cast<std::string>(e.what()));
277 }
278
279 // this->sim->set_matrix_size(nr_of_facets *
280 // this->sim->m_simulation_config.getIntParameters("numerical_layers"));
281 std::vector<T> areas{this->m_poly_mesh->getAreas()};
282 std::vector<int> node_numbers = {0, 1};
283 int num_cells{0};
284 std::vector<int> boundary_vector = {-1};
285 std::map<std::string_view, std::vector<int>> boundary_points_top{
287 std::map<std::string_view, std::vector<int>> boundary_points_bottom{
289 int current_facet = 0;
290
291 for (int i{0}; i < nr_of_facets * (number_of_elements); i++)
292 {
294 {
295 std::cerr << "[GridOneDim]: Node number out of bounds in OneDimGrid. The node number "
296 "exceeds the requested number.\n";
297 throw std::runtime_error(static_cast<std::string>(
298 "[GridOneDim]: Node number out of bounds in OneDimGrid. The node number "
299 "exceeds the requested number."));
300 }
301 props.element_length = length_arr[i % number_of_elements];
302 if (i % number_of_elements == 0)
303 {
305 props.element_area = areas[current_facet];
306 if (i > 0)
307 {
308 incrementVector(node_numbers); // The extra increase is needed to separate the
309 // facets in the matrix
310 }
312 this->all_cells.push_back({createCell<T>(cell_type, true, boundary_points_top,
313 node_numbers, this->sim, num_cells, props)});
316 }
317 else if (i % number_of_elements == number_of_elements - 1
319 {
321 this->all_cells.push_back({createCell<T>(cell_type, true, boundary_points_bottom,
322 node_numbers, this->sim, num_cells, props)});
324 current_facet, areas[current_facet], "bottom_");
325 }
326 else
327 {
328 // boundary_points.at(boundary_condition_type)[0] = node_numbers[0]; // This value is
329 // never used for internal elements
330 this->all_cells.push_back({createCell<T>(cell_type, false, boundary_points_top,
331 node_numbers, this->sim, num_cells, props)});
332 }
333
334 num_cells++;
335 incrementVector(node_numbers);
336 }
337 // std::cout << "Number interior cells: " << this->compute_cells.size() << ". Number boundary
338 // cells: " << this->boundary_cells.size() << "\n";
339}
340
341#endif
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:94
This class parses csv files. Parses the files as a vector of vectors of type double where each row in...
Definition CsvParser.h:17
std::pair< int, int > size() const
Function that returns the size of the matrix as a pair in (rows, columns) format.
Definition GenericMatrix.h:120
Definition GridBase.h:17
Concrete implementation of a 1D grid on each facet of a 3D shape model (hereafter: pseudo 3D).
Definition GridOneDim.h:25
~GridOneDim()=default
T calculateCellLength()
Calculates the uniform cell length from the specified simulation config parameters.
Definition GridOneDim.h:79
void createGrid() override
Creation function for a pseudo 3D grid, assuming a shape model is given. True 1D geometries can be co...
Definition GridOneDim.h:242
GridOneDim(SimulationClassBase< T > *sim_ptr, std::shared_ptr< ShapeBase< T > > mesh_ptr)
Constructor for a OneDimGrid object.
Definition GridOneDim.h:50