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 std::vector<T> getGridArray(int number_of_elements);
41};
42
48template <typename T>
54
60template <typename T>
61void GridOneDim<T>::incrementVector(std::vector<int>& vec)
62{
63 for (auto& val : vec)
64 {
65 val++;
66 }
67}
68
69template <typename T>
71{
72 bool depth_present{false};
73 bool length_present{false};
74 double cl_depth{};
75 double cl_length{};
76 try
77 {
78 int numerical_layers{this->sim->m_simulation_config.getIntParameters("numerical_layers")};
79 double max_depth{this->sim->m_simulation_config.getDoubleParameters("max_depth")};
80 cl_depth = max_depth / static_cast<double>(numerical_layers);
81 depth_present = true;
82 }
83 catch (const BadInput& e)
84 {
85 }
86 try
87 {
88 cl_length = this->sim->m_simulation_config.getDoubleParameters("cell_length");
89 length_present = true;
90 }
91 catch (const BadInput& e)
92 {
93 }
94
96 {
97 return cl_depth;
98 }
100 {
101 return cl_length;
102 }
103 else if (!length_present && !depth_present)
104 {
105 throw std::runtime_error(
106 "Could not find the entries 'cell_length' or 'max_depth' within the ini file. Please "
107 "supply one of these entries!");
108 }
109 else
110 {
111 const double epsilon{1e-8};
112 if (std::abs(cl_length - cl_depth) <= epsilon
113 || std::abs(cl_length - cl_depth)
114 <= epsilon * std::max(std::abs(cl_depth), std::abs(cl_length)))
115 {
116 return cl_length;
117 }
118 throw std::runtime_error(
119 "Both entries 'cell_length' and 'max_depth' are present within the ini file and result "
120 "in different lengths. Please only supply one of these entries or have them produce "
121 "the same result!");
122 }
123}
124
125template <typename T>
127{
128 try
129 {
130 CsvParser grid_data{this->sim->m_simulation_config.getStringParameters("grid")};
131 std::vector<T> depth{grid_data.getCsvContents()[0]};
132 std::vector<T> length_arr;
133 for (int i{1}; i < depth.size(); i++)
134 {
135 length_arr.push_back(depth[i] - depth[i - 1]);
136 }
137 // Overwrite the numerical_layers parameter with the one given by the grid file
138 if (this->sim->m_simulation_config.getIntParameters("numerical_layers") != depth.size())
139 {
140 std::cerr
141 << "[Warning] Missmatch between number of numerical layers within provided file "
142 "and given value in the ini! The value in the provided file will be used!\n";
143 this->sim->m_simulation_config.forceValueOverwrite("numerical_layers",
144 std::to_string(depth.size()));
145 }
146 return std::move(length_arr);
147 }
148 catch (const BadInput&)
149 {
150 std::vector<T> length_arr(number_of_elements, calculateCellLength());
151 length_arr[0] = length_arr[0] / 2;
152 return length_arr;
153 }
154}
155
168template <typename T>
170 const std::vector<int>& node_numbers, std::string_view boundary_condition_type,
171 const std::map<std::string_view, std::vector<int>>& boundary_points, int current_facet, T areas,
172 const std::string& face_prefix)
173{
174 std::vector<std::vector<int>> connected_points({{node_numbers[0], node_numbers[0] + 1}});
175 std::map<std::string_view, std::unique_ptr<GenericBoundaryCondition<T>>> boundary_condition;
179 this->all_cells[this->all_cells.size() - 1]->setBoundaryConditions(boundary_condition);
180}
181
189template <typename T>
191{
193 const std::string cell_type{this->sim->m_simulation_config.getStringParameters("cell_type")};
194 const std::string top_boundary_condition_type{
195 this->sim->m_simulation_config.getStringParameters("top_boundary_condition_type")};
196 const std::string bottom_boundary_condition_type{
197 this->sim->m_simulation_config.getStringParameters("bottom_boundary_condition_type")};
198 int number_of_elements{this->sim->m_simulation_config.getIntParameters("numerical_layers") - 1};
199 int nr_of_facets{static_cast<int>(this->m_poly_mesh->getNumberOfFacets())};
200 std::vector length_arr{getGridArray(number_of_elements)};
201
202 // This is ugly, please revise
203 std::valarray<double> cell_length(number_of_elements);
204 for (int i{0}; i < number_of_elements; i++)
205 {
207 }
208 this->sim->m_field_map.insert({"CellLength", cell_length});
209
210 this->grid_size =
211 nr_of_facets * this->sim->m_simulation_config.getIntParameters("numerical_layers");
212
213 // this->sim->set_matrix_size(nr_of_facets *
214 // this->sim->m_simulation_config.getIntParameters("numerical_layers"));
215 std::vector<T> areas{this->m_poly_mesh->getAreas()};
216 std::vector<int> node_numbers = {0, 1};
217 int num_cells{0};
218 std::vector<int> boundary_vector = {-1};
219 std::map<std::string_view, std::vector<int>> boundary_points_top{
221 std::map<std::string_view, std::vector<int>> boundary_points_bottom{
223 int current_facet = 0;
224
225 for (int i{0}; i < nr_of_facets * (number_of_elements); i++)
226 {
228 {
229 throw std::runtime_error(
230 static_cast<std::string>("Node number out of bounds in OneDimGrid. The node number "
231 "exceeds the requested number"));
232 }
233 props.element_length = length_arr[i % number_of_elements];
234 if (i % number_of_elements == 0)
235 {
237 props.element_area = areas[current_facet];
238 if (i > 0)
239 {
240 incrementVector(node_numbers); // The extra increase is needed to separate the
241 // facets in the matrix
242 }
244 this->all_cells.push_back({createCell<T>(cell_type, true, boundary_points_top,
245 node_numbers, this->sim, num_cells, props)});
248 }
249 else if (i % number_of_elements == number_of_elements - 1
251 {
253 this->all_cells.push_back({createCell<T>(cell_type, true, boundary_points_bottom,
254 node_numbers, this->sim, num_cells, props)});
256 current_facet, areas[current_facet], "bottom_");
257 }
258 else
259 {
260 // boundary_points.at(boundary_condition_type)[0] = node_numbers[0]; // This value is
261 // never used for internal elements
262 this->all_cells.push_back({createCell<T>(cell_type, false, boundary_points_top,
263 node_numbers, this->sim, num_cells, props)});
264 }
265
266 num_cells++;
267 incrementVector(node_numbers);
268 }
269 // std::cout << "Number interior cells: " << this->compute_cells.size() << ". Number boundary
270 // cells: " << this->boundary_cells.size() << "\n";
271}
272
273#endif
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
This class parses csv files. Parses the files as a vector of vectors of type double where each row in...
Definition CsvParser.h:17
Error class to throw when a module chain has circular or missing dependencies. Inherits from std::exc...
Definition ChainManager.h:28
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()
Definition GridOneDim.h:70
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:190
GridOneDim(SimulationClassBase< T > *sim_ptr, std::shared_ptr< ShapeBase< T > > mesh_ptr)
Constructor for a OneDimGrid object.
Definition GridOneDim.h:49