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& e)
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& e)
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 throw std::runtime_error(
115 "Could not find the entries 'cell_length' or 'max_depth' within the ini file. Please "
116 "supply one of these entries!");
117 }
118 else
119 {
120 const double epsilon{1e-8};
121 if (std::abs(cl_length - cl_depth) <= epsilon
122 || std::abs(cl_length - cl_depth)
123 <= epsilon * std::max(std::abs(cl_depth), std::abs(cl_length)))
124 {
125 return cl_length;
126 }
127 throw std::runtime_error(
128 "Both entries 'cell_length' and 'max_depth' are present within the ini file and result "
129 "in different lengths. Please only supply one of these entries or have them produce "
130 "the same result!");
131 }
132}
133
141template <typename T>
143{
144 try
145 {
146 CsvParser grid_data{this->sim->m_simulation_config.getStringParameters("grid")};
147 std::vector<T> depth{grid_data.getCsvContents()[0]};
148 std::vector<T> length_arr;
149 for (int i{1}; i < depth.size(); i++)
150 {
151 length_arr.push_back(depth[i] - depth[i - 1]);
152 }
153 // Overwrite the numerical_layers parameter with the one given by the grid file
154 if (this->sim->m_simulation_config.getIntParameters("numerical_layers") != depth.size())
155 {
156 std::cerr
157 << "[Warning] Missmatch between number of numerical layers within provided file "
158 "and given value in the ini! The value in the provided file will be used!\n";
159 this->sim->m_simulation_config.forceValueOverwrite("numerical_layers",
160 std::to_string(depth.size()));
161 }
162 return std::move(length_arr);
163 }
164 catch (const BadInput&)
165 {
166 std::vector<T> length_arr(number_of_elements, calculateCellLength());
167 length_arr[0] = length_arr[0] / 2;
168 return length_arr;
169 }
170}
171
184template <typename T>
186 const std::vector<int>& node_numbers, std::string_view boundary_condition_type,
187 const std::map<std::string_view, std::vector<int>>& boundary_points, int current_facet, T areas,
188 const std::string& face_prefix)
189{
190 std::vector<std::vector<int>> connected_points({{node_numbers[0], node_numbers[0] + 1}});
191 std::map<std::string_view, std::unique_ptr<GenericBoundaryCondition<T>>> boundary_condition;
195 this->all_cells[this->all_cells.size() - 1]->setBoundaryConditions(boundary_condition);
196}
197
204template <typename T>
206{
207#ifdef MPI_PRESENT
208 return this->sim->m_simulation_config.getIntParameters("number_of_facets");
209#else
210 return static_cast<int>(this->m_poly_mesh->getNumberOfFacets());
211#endif
212}
213
221template <typename T>
223{
225 const std::string cell_type{this->sim->m_simulation_config.getStringParameters("cell_type")};
226 const std::string top_boundary_condition_type{
227 this->sim->m_simulation_config.getStringParameters("top_boundary_condition_type")};
228 const std::string bottom_boundary_condition_type{
229 this->sim->m_simulation_config.getStringParameters("bottom_boundary_condition_type")};
230 int number_of_elements{this->sim->m_simulation_config.getIntParameters("numerical_layers") - 1};
231 int nr_of_facets{getNumberOfFacets()};
232 std::vector length_arr{getGridArray(number_of_elements)};
233
234 // This is ugly, please revise
235 std::valarray<double> cell_length(number_of_elements);
236 for (int i{0}; i < number_of_elements; i++)
237 {
239 }
240 this->sim->m_field_map.insert({"CellLength", cell_length});
241
242 this->grid_size =
243 nr_of_facets * this->sim->m_simulation_config.getIntParameters("numerical_layers");
244
245 // this->sim->set_matrix_size(nr_of_facets *
246 // this->sim->m_simulation_config.getIntParameters("numerical_layers"));
247 std::vector<T> areas{this->m_poly_mesh->getAreas()};
248 std::vector<int> node_numbers = {0, 1};
249 int num_cells{0};
250 std::vector<int> boundary_vector = {-1};
251 std::map<std::string_view, std::vector<int>> boundary_points_top{
253 std::map<std::string_view, std::vector<int>> boundary_points_bottom{
255 int current_facet = 0;
256
257 for (int i{0}; i < nr_of_facets * (number_of_elements); i++)
258 {
260 {
261 throw std::runtime_error(
262 static_cast<std::string>("Node number out of bounds in OneDimGrid. The node number "
263 "exceeds the requested number"));
264 }
265 props.element_length = length_arr[i % number_of_elements];
266 if (i % number_of_elements == 0)
267 {
269 props.element_area = areas[current_facet];
270 if (i > 0)
271 {
272 incrementVector(node_numbers); // The extra increase is needed to separate the
273 // facets in the matrix
274 }
276 this->all_cells.push_back({createCell<T>(cell_type, true, boundary_points_top,
277 node_numbers, this->sim, num_cells, props)});
280 }
281 else if (i % number_of_elements == number_of_elements - 1
283 {
285 this->all_cells.push_back({createCell<T>(cell_type, true, boundary_points_bottom,
286 node_numbers, this->sim, num_cells, props)});
288 current_facet, areas[current_facet], "bottom_");
289 }
290 else
291 {
292 // boundary_points.at(boundary_condition_type)[0] = node_numbers[0]; // This value is
293 // never used for internal elements
294 this->all_cells.push_back({createCell<T>(cell_type, false, boundary_points_top,
295 node_numbers, this->sim, num_cells, props)});
296 }
297
298 num_cells++;
299 incrementVector(node_numbers);
300 }
301 // std::cout << "Number interior cells: " << this->compute_cells.size() << ". Number boundary
302 // cells: " << this->boundary_cells.size() << "\n";
303}
304
305#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:35
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:92
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:118
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:222
GridOneDim(SimulationClassBase< T > *sim_ptr, std::shared_ptr< ShapeBase< T > > mesh_ptr)
Constructor for a OneDimGrid object.
Definition GridOneDim.h:50