svMultiPhysics
Loading...
Searching...
No Matches
BoundaryCondition.h
1// SPDX-FileCopyrightText: Copyright (c) Stanford University, The Regents of the University of California, and others.
2// SPDX-License-Identifier: BSD-3-Clause
3
4#ifndef BOUNDARY_CONDITION_H
5#define BOUNDARY_CONDITION_H
6
7#include "Array.h"
8#include "Vector.h"
9#include "CmMod.h"
10#include "VtkData.h"
11#include <string>
12#include "SimulationLogger.h"
13#include <memory>
14#include <map>
15#include <vector>
16#include <stdexcept>
17#include <utility>
18
19// Forward declarations. These are needed because including ComMod.h causes a
20// circular header dependency.
21class faceType;
22class ComMod;
23
24/// @brief Base class for boundary conditions with spatially variable arrays
25///
26/// This class provides common functionality for boundary conditions that need to
27/// read and manage arrays of values from VTP files or uniform values. It handles
28/// distribution of data across processes and provides efficient access to values.
29///
30/// This class is intended to be subclassed by specific boundary condition types.
31///
32/// Development note: this class is intended to eventually be an object-oriented
33/// replacement of the existing bcType, although it is not yet complete.
34///
35/// Example usage:
36/// ```cpp
37/// class MyBoundaryCondition : public BoundaryCondition {
38/// public:
39/// MyBoundaryCondition(const std::string& vtp_file_path, const std::vector<std::string>& array_names, const faceType& face)
40/// : BoundaryCondition(vtp_file_path, array_names, face) {}
41///
42/// MyBoundaryCondition(const std::map<std::string, double>& uniform_values, const faceType& face)
43/// : BoundaryCondition(uniform_values, face) {}
44///
45/// // Add BC-specific functionality here
46/// };
47/// ```
49protected:
50 /// @brief Type alias for map of array names to array data
51 using StringArrayMap = std::map<std::string, Array<double>>;
52 using StringBoolMap = std::map<std::string, bool>;
53 using StringDoubleMap = std::map<std::string, double>;
54
55 /// @brief Data members for BC
56 const faceType* face_ = nullptr; ///< Face associated with the BC (not owned by BoundaryCondition)
57 int global_num_nodes_ = 0; ///< Global number of nodes on the face
58 int local_num_nodes_ = 0; ///< Local number of nodes on this processor
59 std::vector<std::string> array_names_; ///< Names of arrays to read from VTP file
60 StringArrayMap local_data_; ///< Local array values for each node on this processor
61 StringArrayMap global_data_; ///< Global array values (only populated on master)
62 StringBoolMap flags_; ///< Named boolean flags for BC behavior
63 bool spatially_variable = false; ///< Flag indicating if data is from VTP file
64 std::string vtp_file_path_; ///< Path to VTP file (empty if uniform)
65 std::map<int, int> global_node_map_; ///< Maps global node IDs to local array indices
66 std::unique_ptr<VtkVtpData> vtp_data_; ///< VTP data object
67 const SimulationLogger* logger_ = nullptr; ///< Logger for warnings/info (not owned by BoundaryCondition)
68
69public:
70 /// @brief Tolerance for point matching in VTP files
71 static constexpr double POINT_MATCH_TOLERANCE = 1e-12;
72
73 /// @brief Default constructor - creates an empty BC
74 BoundaryCondition() = default;
75
76 /// @brief Constructor - reads data from VTP file
77 /// @param vtp_file_path Path to VTP file containing arrays
78 /// @param array_names Names of arrays to read from VTP file
79 /// @param face Face associated with the BC
80 /// @param logger Simulation logger used to write warnings
81 /// @throws std::runtime_error if file cannot be read or arrays are missing
82 BoundaryCondition(const std::string& vtp_file_path, const std::vector<std::string>& array_names, const StringBoolMap& flags, const faceType& face, SimulationLogger& logger);
83
84 /// @brief Constructor for uniform values
85 /// @param uniform_values Map of array names to uniform values
86 /// @param face Face associated with the BC
87 /// @param logger Simulation logger used to write warnings
88 BoundaryCondition(const StringDoubleMap& uniform_values, const StringBoolMap& flags, const faceType& face, SimulationLogger& logger);
89
90 /// @brief Copy constructor
92
93 /// @brief Unified assignment operator (handles both copy and move)
95
96 /// @brief Move constructor
97 BoundaryCondition(BoundaryCondition&& other) noexcept;
98
99 /// @brief Virtual destructor
100 virtual ~BoundaryCondition() noexcept = default;
101
102 /// @brief Swap function for copy-and-swap idiom (friend function)
103 friend void swap(BoundaryCondition& lhs, BoundaryCondition& rhs) noexcept;
104
105
106 /// @brief Get value for a specific array and node
107 /// @param array_name Name of the array
108 /// @param node_id Node index on the face
109 /// @return Value for the array at the specified node
110 /// @throws std::runtime_error if array_name is not found
111 double get_value(const std::string& array_name, int node_id) const;
112
113 /// @brief Get a boolean flag by name
114 /// @param name Name of the flag
115 /// @return Value of the flag
116 /// @throws std::runtime_error if flag is not found
117 bool get_flag(const std::string& name) const;
118
119 /// @brief Get a string representation of the flags
120 /// @return String representation of the flags
121 std::string flags_to_string() const;
122
123 /// @brief Get global number of nodes
124 /// @return Global number of nodes on the face
125 int get_global_num_nodes() const noexcept {
126 return global_num_nodes_;
127 }
128
129 /// @brief Get local number of nodes
130 /// @return Local number of nodes on the face on this processor
131 int get_local_num_nodes() const noexcept {
132 return local_num_nodes_;
133 }
134
135 /// @brief Get local array index for a global node ID
136 /// @param global_node_id The global node ID defined on the face
137 /// @return Local array index for data arrays
138 /// @throws std::runtime_error if global_node_id is not found in the map
139 int get_local_index(int global_node_id) const;
140
141 /// @brief Check if data is loaded from VTP file
142 /// @return true if loaded from VTP, false if using uniform values
143 bool is_from_vtp() const noexcept {
144 return spatially_variable;
145 }
146
147 /// @brief Get the VTP file path (empty if using uniform values)
148 /// @return VTP file path
149 const std::string& get_vtp_path() const noexcept {
150 return vtp_file_path_;
151 }
152
153 /// @brief Check if this BC has been properly initialized with data
154 /// @return true if BC has data (either global or local arrays are populated)
155 bool is_initialized() const noexcept {
156 return !global_data_.empty() || !local_data_.empty();
157 }
158
159 /// @brief Distribute BC data from the master process to the slave processes
160 /// @param com_mod Reference to ComMod object for global coordinates
161 /// @param cm_mod Reference to CmMod object for MPI communication
162 /// @param cm Reference to cmType object for MPI communication
163 /// @param face Face associated with the BC
164 void distribute(const ComMod& com_mod, const CmMod& cm_mod, const cmType& cm, const faceType& face);
165
166protected:
167 /// @brief Read data from VTP file
168 /// @param vtp_file_path Path to VTP file
169 /// @param array_names Names of arrays to read
170 /// @return Map of array names to array data
171 StringArrayMap read_data_from_vtp_file(const std::string& vtp_file_path, const std::vector<std::string>& array_names);
172
173 /// @brief Find index of a point in the VTP points array
174 /// @param x X coordinate
175 /// @param y Y coordinate
176 /// @param z Z coordinate
177 /// @param vtp_points VTP points array
178 /// @param mesh_scale_factor Scale factor applied to mesh coordinates
179 /// @return Index of the matching point in the VTP array
180 /// @throws std::runtime_error if no matching point is found
181 int find_vtp_point_index(double x, double y, double z, const Array<double>& vtp_points, double mesh_scale_factor) const;
182
183 /// @brief Hook for derived classes to validate array values
184 /// @param array_name Name of the array being validated
185 /// @param value Value to validate
186 /// @throws std::runtime_error if validation fails
187 virtual void validate_array_value(const std::string& array_name, double value) const {}
188
189 // ---- distribute helpers ----
190 void distribute_metadata(const CmMod& cm_mod, const cmType& cm, bool is_slave);
191 void distribute_spatially_variable(const ComMod& com_mod, const CmMod& cm_mod, const cmType& cm, bool is_slave);
192 void distribute_uniform(const CmMod& cm_mod, const cmType& cm, bool is_slave);
193 void distribute_flags(const CmMod& cm_mod, const cmType& cm, bool is_slave);
194
195};
196
197/// @brief Base exception class for BC errors
198///
199/// These exceptions indicate fatal errors that should terminate the solver.
200/// They should not be caught and handled gracefully - the solver should exit.
201class BoundaryConditionBaseException : public std::exception {
202public:
203 /// @brief Constructor
204 /// @param msg Error message
205 explicit BoundaryConditionBaseException(const std::string& msg) : message_(msg) {}
206
207 /// @brief Get error message
208 /// @return Error message
209 const char* what() const noexcept override {
210 return message_.c_str();
211 }
212
213private:
214 std::string message_;
215};
216
217/// @brief Exception thrown when VTP file cannot be read or is invalid
219public:
220 /// @brief Constructor
221 /// @param file Path to VTP file
222 explicit BoundaryConditionFileException(const std::string& file)
223 : BoundaryConditionBaseException("Failed to open or read the VTP file '" + file + "'") {}
224};
225
226/// @brief Exception thrown when node count mismatch between VTP and face
228public:
229 /// @brief Constructor
230 /// @param vtp_file VTP file path
231 /// @param face_name Face name
232 explicit BoundaryConditionNodeCountException(const std::string& vtp_file, const std::string& face_name)
233 : BoundaryConditionBaseException("Number of nodes in VTP file '" + vtp_file +
234 "' does not match number of nodes on face '" + face_name + "'") {}
235};
236
237/// @brief Exception thrown when array validation fails
239public:
240 /// @brief Constructor
241 /// @param array_name Name of array that failed validation
242 /// @param value Value that failed validation
243 explicit BoundaryConditionValidationException(const std::string& array_name, double value)
244 : BoundaryConditionBaseException("Invalid value " + std::to_string(value) +
245 " for array '" + array_name + "'") {}
246};
247
248/// @brief Exception thrown when a requested flag is not defined
250public:
251 explicit BoundaryConditionFlagException(const std::string& flag_name)
252 : BoundaryConditionBaseException("BoundaryCondition flag not found: '" + flag_name + "'") {}
253};
254
255/// @brief Exception thrown when a requested array is not found
257public:
258 explicit BoundaryConditionArrayException(const std::string& array_name)
259 : BoundaryConditionBaseException("BoundaryCondition array not found: '" + array_name + "'") {}
260};
261
262/// @brief Exception thrown when BoundaryCondition is not properly initialized
264public:
266 : BoundaryConditionBaseException("BoundaryCondition not properly initialized - no data available") {}
267};
268
269/// @brief Exception thrown when a node ID is out of range
271public:
272 explicit BoundaryConditionNodeIdException(int node_id, int max_node_id)
273 : BoundaryConditionBaseException("Node ID " + std::to_string(node_id) +
274 " is out of range [0, " + std::to_string(max_node_id - 1) + "]") {}
275};
276
277/// @brief Exception thrown when a global node ID is not found in the global-to-local map
279public:
280 explicit BoundaryConditionGlobalNodeIdException(int global_node_id)
281 : BoundaryConditionBaseException("Global node ID " + std::to_string(global_node_id) +
282 " not found in global-to-local map") {}
283};
284
285/// @brief Exception thrown when a VTP file doesn't contain a required array
287public:
288 explicit BoundaryConditionVtpArrayException(const std::string& vtp_file, const std::string& array_name)
289 : BoundaryConditionBaseException("VTP file '" + vtp_file + "' does not contain '" + array_name + "' point array") {}
290};
291
292/// @brief Exception thrown when a VTP array has incorrect dimensions
294public:
295 explicit BoundaryConditionVtpArrayDimensionException(const std::string& vtp_file, const std::string& array_name,
296 int expected_rows, int expected_cols, int actual_rows, int actual_cols)
297 : BoundaryConditionBaseException("'" + array_name + "' array in VTP file '" + vtp_file +
298 "' has incorrect dimensions. Expected " + std::to_string(expected_rows) +
299 " x " + std::to_string(expected_cols) + ", got " + std::to_string(actual_rows) +
300 " x " + std::to_string(actual_cols)) {}
301};
302
303/// @brief Exception thrown when face_ is nullptr during distribute
305public:
307 : BoundaryConditionBaseException("face_ is nullptr during distribute") {}
308};
309
310/// @brief Exception thrown when a point cannot be found in VTP file
312public:
313 explicit BoundaryConditionPointNotFoundException(double x, double y, double z)
314 : BoundaryConditionBaseException("Could not find matching point in VTP file for node at position (" +
315 std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")") {}
316};
317
318#endif // BOUNDARY_CONDITION_H
Exception thrown when a requested array is not found.
Definition BoundaryCondition.h:256
Base exception class for BC errors.
Definition BoundaryCondition.h:201
BoundaryConditionBaseException(const std::string &msg)
Constructor.
Definition BoundaryCondition.h:205
const char * what() const noexcept override
Get error message.
Definition BoundaryCondition.h:209
Exception thrown when VTP file cannot be read or is invalid.
Definition BoundaryCondition.h:218
BoundaryConditionFileException(const std::string &file)
Constructor.
Definition BoundaryCondition.h:222
Exception thrown when a requested flag is not defined.
Definition BoundaryCondition.h:249
Exception thrown when a global node ID is not found in the global-to-local map.
Definition BoundaryCondition.h:278
Base class for boundary conditions with spatially variable arrays.
Definition BoundaryCondition.h:48
StringBoolMap flags_
Named boolean flags for BC behavior.
Definition BoundaryCondition.h:62
virtual ~BoundaryCondition() noexcept=default
Virtual destructor.
std::unique_ptr< VtkVtpData > vtp_data_
VTP data object.
Definition BoundaryCondition.h:66
bool is_from_vtp() const noexcept
Check if data is loaded from VTP file.
Definition BoundaryCondition.h:143
int local_num_nodes_
Local number of nodes on this processor.
Definition BoundaryCondition.h:58
BoundaryCondition()=default
Default constructor - creates an empty BC.
BoundaryCondition & operator=(BoundaryCondition other)
Unified assignment operator (handles both copy and move)
Definition BoundaryCondition.cpp:108
StringArrayMap read_data_from_vtp_file(const std::string &vtp_file_path, const std::vector< std::string > &array_names)
Read data from VTP file.
Definition BoundaryCondition.cpp:134
const faceType * face_
Data members for BC.
Definition BoundaryCondition.h:56
std::map< int, int > global_node_map_
Maps global node IDs to local array indices.
Definition BoundaryCondition.h:65
double get_value(const std::string &array_name, int node_id) const
Get value for a specific array and node.
Definition BoundaryCondition.cpp:192
void distribute(const ComMod &com_mod, const CmMod &cm_mod, const cmType &cm, const faceType &face)
Distribute BC data from the master process to the slave processes.
Definition BoundaryCondition.cpp:237
bool get_flag(const std::string &name) const
Get a boolean flag by name.
Definition BoundaryCondition.cpp:211
static constexpr double POINT_MATCH_TOLERANCE
Tolerance for point matching in VTP files.
Definition BoundaryCondition.h:71
virtual void validate_array_value(const std::string &array_name, double value) const
Hook for derived classes to validate array values.
Definition BoundaryCondition.h:187
std::vector< std::string > array_names_
Names of arrays to read from VTP file.
Definition BoundaryCondition.h:59
bool spatially_variable
Flag indicating if data is from VTP file.
Definition BoundaryCondition.h:63
int global_num_nodes_
Global number of nodes on the face.
Definition BoundaryCondition.h:57
std::string flags_to_string() const
Get a string representation of the flags.
Definition BoundaryCondition.cpp:499
int get_local_index(int global_node_id) const
Get local array index for a global node ID.
Definition BoundaryCondition.cpp:224
int get_global_num_nodes() const noexcept
Get global number of nodes.
Definition BoundaryCondition.h:125
std::string vtp_file_path_
Path to VTP file (empty if uniform)
Definition BoundaryCondition.h:64
StringArrayMap global_data_
Global array values (only populated on master)
Definition BoundaryCondition.h:61
StringArrayMap local_data_
Local array values for each node on this processor.
Definition BoundaryCondition.h:60
int get_local_num_nodes() const noexcept
Get local number of nodes.
Definition BoundaryCondition.h:131
friend void swap(BoundaryCondition &lhs, BoundaryCondition &rhs) noexcept
Swap function for copy-and-swap idiom (friend function)
Definition BoundaryCondition.cpp:92
const SimulationLogger * logger_
Logger for warnings/info (not owned by BoundaryCondition)
Definition BoundaryCondition.h:67
int find_vtp_point_index(double x, double y, double z, const Array< double > &vtp_points, double mesh_scale_factor) const
Find index of a point in the VTP points array.
Definition BoundaryCondition.cpp:468
const std::string & get_vtp_path() const noexcept
Get the VTP file path (empty if using uniform values)
Definition BoundaryCondition.h:149
std::map< std::string, Array< double > > StringArrayMap
Type alias for map of array names to array data.
Definition BoundaryCondition.h:51
bool is_initialized() const noexcept
Check if this BC has been properly initialized with data.
Definition BoundaryCondition.h:155
Exception thrown when node count mismatch between VTP and face.
Definition BoundaryCondition.h:227
BoundaryConditionNodeCountException(const std::string &vtp_file, const std::string &face_name)
Constructor.
Definition BoundaryCondition.h:232
Exception thrown when a node ID is out of range.
Definition BoundaryCondition.h:270
Exception thrown when BoundaryCondition is not properly initialized.
Definition BoundaryCondition.h:263
Exception thrown when face_ is nullptr during distribute.
Definition BoundaryCondition.h:304
Exception thrown when a point cannot be found in VTP file.
Definition BoundaryCondition.h:311
Exception thrown when array validation fails.
Definition BoundaryCondition.h:238
BoundaryConditionValidationException(const std::string &array_name, double value)
Constructor.
Definition BoundaryCondition.h:243
Exception thrown when a VTP array has incorrect dimensions.
Definition BoundaryCondition.h:293
Exception thrown when a VTP file doesn't contain a required array.
Definition BoundaryCondition.h:286
The CmMod class duplicates the data structures in the Fortran CMMOD module defined in COMU....
Definition CmMod.h:35
The ComMod class duplicates the data structures in the Fortran COMMOD module defined in MOD....
Definition ComMod.h:1514
The SimulationLogger class is used to write information to a text file and optionally to cout.
Definition SimulationLogger.h:13
The cmType class stores data and defines methods used for mpi communication.
Definition CmMod.h:55
The face type containing mesh at boundary.
Definition ComMod.h:511