Skip to content

Commit ee43996

Browse files
Remove defined_ flag, just check if arrays are allocated, add new exception classes
1 parent 626788c commit ee43996

File tree

5 files changed

+92
-39
lines changed

5 files changed

+92
-39
lines changed

Code/Source/solver/BoundaryCondition.cpp

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ BoundaryCondition::BoundaryCondition(const std::string& vtp_file_path, const std
4848
, spatially_variable(true)
4949
, vtp_file_path_(vtp_file_path)
5050
, flags_(flags)
51-
, defined_(false) // Start as undefined, set to true only if successful
5251
{
5352
try {
5453
global_data_ = read_data_from_vtp_file(vtp_file_path, array_names);
@@ -68,11 +67,8 @@ BoundaryCondition::BoundaryCondition(const std::string& vtp_file_path, const std
6867
for (int i = 0; i < global_num_nodes_; i++) {
6968
global_node_map_[face_->gN(i)] = i;
7069
}
71-
72-
defined_ = true; // Mark as successfully defined
7370
} catch (const std::exception& e) {
74-
// Constructor failed - object remains in undefined state
75-
// defined_ remains false, resources are automatically cleaned up
71+
// Constructor failed - resources are automatically cleaned up
7672
throw; // Re-throw the exception
7773
}
7874
}
@@ -84,7 +80,6 @@ BoundaryCondition::BoundaryCondition(const StringDoubleMap& uniform_values, cons
8480
, spatially_variable(false)
8581
, vtp_file_path_("")
8682
, flags_(flags)
87-
, defined_(false) // Start as undefined, set to true only if successful
8883
{
8984
try {
9085
// Store array names, validate and store values
@@ -95,11 +90,8 @@ BoundaryCondition::BoundaryCondition(const StringDoubleMap& uniform_values, cons
9590
local_data_[name] = Array<double>(1, 1);
9691
local_data_[name](0, 0) = value;
9792
}
98-
99-
defined_ = true; // Mark as successfully defined
10093
} catch (const std::exception& e) {
101-
// Constructor failed - object remains in undefined state
102-
// defined_ remains false, resources are automatically cleaned up
94+
// Constructor failed - resources are automatically cleaned up
10395
throw; // Re-throw the exception
10496
}
10597
}
@@ -115,7 +107,6 @@ BoundaryCondition::BoundaryCondition(const BoundaryCondition& other)
115107
, vtp_file_path_(other.vtp_file_path_)
116108
, flags_(other.flags_)
117109
, global_node_map_(other.global_node_map_)
118-
, defined_(other.defined_)
119110
{
120111
if (other.vtp_data_) {
121112
vtp_data_ = std::make_unique<VtkVtpData>(*other.vtp_data_);
@@ -135,7 +126,6 @@ void swap(BoundaryCondition& lhs, BoundaryCondition& rhs) noexcept {
135126
swap(lhs.flags_, rhs.flags_);
136127
swap(lhs.global_node_map_, rhs.global_node_map_);
137128
swap(lhs.vtp_data_, rhs.vtp_data_);
138-
swap(lhs.defined_, rhs.defined_);
139129
}
140130

141131
BoundaryCondition& BoundaryCondition::operator=(BoundaryCondition other) {
@@ -155,13 +145,11 @@ BoundaryCondition::BoundaryCondition(BoundaryCondition&& other) noexcept
155145
, flags_(std::move(other.flags_))
156146
, global_node_map_(std::move(other.global_node_map_))
157147
, vtp_data_(std::move(other.vtp_data_))
158-
, defined_(other.defined_)
159148
{
160149
other.face_ = nullptr;
161150
other.global_num_nodes_ = 0;
162151
other.local_num_nodes_ = 0;
163152
other.spatially_variable = false;
164-
other.defined_ = false;
165153
}
166154

167155

@@ -195,16 +183,15 @@ BoundaryCondition::StringArrayMap BoundaryCondition::read_data_from_vtp_file(con
195183
StringArrayMap result;
196184
for (const auto& array_name : array_names) {
197185
if (!vtp_data_->has_point_data(array_name)) {
198-
throw std::runtime_error("VTP file '" + vtp_file_path + "' does not contain '" + array_name + "' point array.");
186+
throw BoundaryConditionVtpArrayException(vtp_file_path, array_name);
199187
}
200188

201189
auto array_data = vtp_data_->get_point_data(array_name);
202190

203191
if (array_data.nrows() != global_num_nodes_ || array_data.ncols() != 1) {
204-
throw std::runtime_error("'" + array_name + "' array in VTP file '" + vtp_file_path +
205-
"' has incorrect dimensions. Expected " + std::to_string(global_num_nodes_) +
206-
" x 1, got " + std::to_string(array_data.nrows()) + " x " +
207-
std::to_string(array_data.ncols()) + ".");
192+
throw BoundaryConditionVtpArrayDimensionException(vtp_file_path, array_name,
193+
global_num_nodes_, 1,
194+
array_data.nrows(), array_data.ncols());
208195
}
209196

210197
// Store array in result map
@@ -223,12 +210,11 @@ double BoundaryCondition::get_value(const std::string& array_name, int node_id)
223210
{
224211
auto it = local_data_.find(array_name);
225212
if (it == local_data_.end()) {
226-
throw std::runtime_error("Array '" + array_name + "' not found.");
213+
throw BoundaryConditionArrayException(array_name);
227214
}
228215

229216
if (node_id < 0 || node_id >= global_num_nodes_) {
230-
throw std::runtime_error("Node ID " + std::to_string(node_id) +
231-
" is out of range [0, " + std::to_string(global_num_nodes_ - 1) + "].");
217+
throw BoundaryConditionNodeIdException(node_id, global_num_nodes_);
232218
}
233219

234220
// Return value
@@ -257,8 +243,7 @@ int BoundaryCondition::get_local_index(int global_node_id) const
257243
if (spatially_variable) {
258244
auto it = global_node_map_.find(global_node_id);
259245
if (it == global_node_map_.end()) {
260-
throw std::runtime_error("Global node ID " + std::to_string(global_node_id) +
261-
" not found in global-to-local map.");
246+
throw BoundaryConditionGlobalNodeIdException(global_node_id);
262247
}
263248
return it->second;
264249
} else {
@@ -292,7 +277,6 @@ void BoundaryCondition::distribute(const ComMod& com_mod, const CmMod& cm_mod, c
292277
distribute_uniform(cm_mod, cm, is_slave);
293278
}
294279
distribute_flags(cm_mod, cm, is_slave);
295-
defined_ = true;
296280

297281
#ifdef debug_distribute
298282
dmsg << "Finished distributing BC data" << std::endl;
@@ -338,7 +322,7 @@ void BoundaryCondition::distribute_spatially_variable(const ComMod& com_mod, con
338322
#endif
339323

340324
if (face_ == nullptr) {
341-
throw std::runtime_error("face_ is nullptr during distribute");
325+
throw BoundaryConditionNullFaceException();
342326
}
343327
// Each processor collects the global node IDs and nodal positions of its
344328
// associated face portion
@@ -513,9 +497,7 @@ int BoundaryCondition::find_vtp_point_index(double x, double y, double z,
513497
}
514498
}
515499

516-
throw std::runtime_error("Could not find matching point in VTP file for node at position (" +
517-
std::to_string(x) + ", " + std::to_string(y) + ", " +
518-
std::to_string(z) + ")");
500+
throw BoundaryConditionPointNotFoundException(x, y, z);
519501
}
520502

521503
std::string BoundaryCondition::flags_to_string() const {

Code/Source/solver/BoundaryCondition.h

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ class BoundaryCondition {
9090
std::string vtp_file_path_; ///< Path to VTP file (empty if uniform)
9191
std::map<int, int> global_node_map_; ///< Maps global node IDs to local array indices
9292
std::unique_ptr<VtkVtpData> vtp_data_; ///< VTP data object
93-
bool defined_ = false; ///< Whether this BC has been properly initialized
9493

9594
public:
9695
/// @brief Tolerance for point matching in VTP files
@@ -174,10 +173,10 @@ class BoundaryCondition {
174173
return vtp_file_path_;
175174
}
176175

177-
/// @brief Check if this BC is properly defined
178-
/// @return true if BC has been initialized with either VTP data or uniform values
179-
bool is_defined() const noexcept {
180-
return defined_;
176+
/// @brief Check if this BC has been properly initialized with data
177+
/// @return true if BC has data (either global or local arrays are populated)
178+
bool is_initialized() const noexcept {
179+
return !global_data_.empty() || !local_data_.empty();
181180
}
182181

183182
/// @brief Distribute BC data from the master process to the slave processes
@@ -275,4 +274,67 @@ class BoundaryConditionFlagException : public BoundaryConditionBaseException {
275274
: BoundaryConditionBaseException("BoundaryCondition flag not found: '" + flag_name + "'") {}
276275
};
277276

277+
/// @brief Exception thrown when a requested array is not found
278+
class BoundaryConditionArrayException : public BoundaryConditionBaseException {
279+
public:
280+
explicit BoundaryConditionArrayException(const std::string& array_name)
281+
: BoundaryConditionBaseException("BoundaryCondition array not found: '" + array_name + "'") {}
282+
};
283+
284+
/// @brief Exception thrown when BoundaryCondition is not properly initialized
285+
class BoundaryConditionNotInitializedException : public BoundaryConditionBaseException {
286+
public:
287+
explicit BoundaryConditionNotInitializedException()
288+
: BoundaryConditionBaseException("BoundaryCondition not properly initialized - no data available") {}
289+
};
290+
291+
/// @brief Exception thrown when a node ID is out of range
292+
class BoundaryConditionNodeIdException : public BoundaryConditionBaseException {
293+
public:
294+
explicit BoundaryConditionNodeIdException(int node_id, int max_node_id)
295+
: BoundaryConditionBaseException("Node ID " + std::to_string(node_id) +
296+
" is out of range [0, " + std::to_string(max_node_id - 1) + "]") {}
297+
};
298+
299+
/// @brief Exception thrown when a global node ID is not found in the global-to-local map
300+
class BoundaryConditionGlobalNodeIdException : public BoundaryConditionBaseException {
301+
public:
302+
explicit BoundaryConditionGlobalNodeIdException(int global_node_id)
303+
: BoundaryConditionBaseException("Global node ID " + std::to_string(global_node_id) +
304+
" not found in global-to-local map") {}
305+
};
306+
307+
/// @brief Exception thrown when a VTP file doesn't contain a required array
308+
class BoundaryConditionVtpArrayException : public BoundaryConditionBaseException {
309+
public:
310+
explicit BoundaryConditionVtpArrayException(const std::string& vtp_file, const std::string& array_name)
311+
: BoundaryConditionBaseException("VTP file '" + vtp_file + "' does not contain '" + array_name + "' point array") {}
312+
};
313+
314+
/// @brief Exception thrown when a VTP array has incorrect dimensions
315+
class BoundaryConditionVtpArrayDimensionException : public BoundaryConditionBaseException {
316+
public:
317+
explicit BoundaryConditionVtpArrayDimensionException(const std::string& vtp_file, const std::string& array_name,
318+
int expected_rows, int expected_cols, int actual_rows, int actual_cols)
319+
: BoundaryConditionBaseException("'" + array_name + "' array in VTP file '" + vtp_file +
320+
"' has incorrect dimensions. Expected " + std::to_string(expected_rows) +
321+
" x " + std::to_string(expected_cols) + ", got " + std::to_string(actual_rows) +
322+
" x " + std::to_string(actual_cols)) {}
323+
};
324+
325+
/// @brief Exception thrown when face_ is nullptr during distribute
326+
class BoundaryConditionNullFaceException : public BoundaryConditionBaseException {
327+
public:
328+
explicit BoundaryConditionNullFaceException()
329+
: BoundaryConditionBaseException("face_ is nullptr during distribute") {}
330+
};
331+
332+
/// @brief Exception thrown when a point cannot be found in VTP file
333+
class BoundaryConditionPointNotFoundException : public BoundaryConditionBaseException {
334+
public:
335+
explicit BoundaryConditionPointNotFoundException(double x, double y, double z)
336+
: BoundaryConditionBaseException("Could not find matching point in VTP file for node at position (" +
337+
std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")") {}
338+
};
339+
278340
#endif // BOUNDARY_CONDITION_H

Code/Source/solver/RobinBoundaryCondition.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ class RobinBoundaryCondition : public BoundaryCondition {
7070
/// @param vtp_file_path Path to VTP file containing Stiffness and Damping point arrays
7171
/// @param normal_only Flag to apply only along normal direction
7272
/// @param face Face associated with the Robin BC
73-
/// @throws std::runtime_error if file cannot be read or arrays are missing
73+
/// @throws BoundaryConditionFileException if file cannot be read
74+
/// @throws BoundaryConditionVtpArrayException if arrays are missing
75+
/// @throws BoundaryConditionValidationException if values are invalid
7476
RobinBoundaryCondition(const std::string& vtp_file_path, bool normal_only, const faceType& face)
7577
: BoundaryCondition(vtp_file_path, std::vector<std::string>{"Stiffness", "Damping"}, StringBoolMap{{"normal_direction_only", normal_only}}, face) {}
7678

@@ -80,22 +82,29 @@ class RobinBoundaryCondition : public BoundaryCondition {
8082
/// @param uniform_damping Uniform damping value for all nodes
8183
/// @param normal_only Flag to apply only along normal direction
8284
/// @param face Face associated with the Robin BC
85+
/// @throws BoundaryConditionValidationException if values are invalid
8386
RobinBoundaryCondition(double uniform_stiffness, double uniform_damping, bool normal_only, const faceType& face)
8487
: BoundaryCondition({{"Stiffness", uniform_stiffness}, {"Damping", uniform_damping}}, StringBoolMap{{"normal_direction_only", normal_only}}, face) {}
8588

8689
/// @brief Apply only along normal direction (getter)
90+
/// @return true if BC should be applied only along normal direction
91+
/// @throws BoundaryConditionFlagException if "normal_direction_only" flag not found
8792
bool normal_direction_only() const { return this->get_flag("normal_direction_only"); }
8893

8994
/// @brief Get stiffness value for a specific node (convenience method)
9095
/// @param node_id Node index on the face
9196
/// @return Stiffness value for the node
97+
/// @throws BoundaryConditionArrayException if "Stiffness" array not found
98+
/// @throws BoundaryConditionNodeIdException if node_id is out of range
9299
double get_stiffness(int node_id) const {
93100
return get_value("Stiffness", node_id);
94101
}
95102

96103
/// @brief Get damping value for a specific node (convenience method)
97104
/// @param node_id Node index on the face
98105
/// @return Damping value for the node
106+
/// @throws BoundaryConditionArrayException if "Damping" array not found
107+
/// @throws BoundaryConditionNodeIdException if node_id is out of range
99108
double get_damping(int node_id) const {
100109
return get_value("Damping", node_id);
101110
}
@@ -109,7 +118,7 @@ class RobinBoundaryCondition : public BoundaryCondition {
109118
/// @brief Validate array values for Robin BC
110119
/// @param array_name Name of the array being validated
111120
/// @param value Value to validate
112-
/// @throws std::runtime_error if validation fails
121+
/// @throws BoundaryConditionValidationException if validation fails
113122
void validate_array_value(const std::string& array_name, double value) const override {
114123
if (value < 0.0) {
115124
throw BoundaryConditionValidationException(array_name, value);

Code/Source/solver/distribute.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,8 @@ void dist_bc(ComMod& com_mod, const CmMod& cm_mod, const cmType& cm, bcType& lBc
802802

803803
// Communicating Robin BC
804804
//
805-
bool has_robin_bc = lBc.robin_bc.is_defined();
806-
cm.bcast(cm_mod, &has_robin_bc);
805+
bool has_robin_bc = lBc.robin_bc.is_initialized();
806+
cm.bcast(cm_mod, &has_robin_bc); // Master process broadcasts the flag to all processes
807807

808808
if (has_robin_bc) {
809809
lBc.robin_bc.distribute(com_mod, cm_mod, cm, com_mod.msh[lBc.iM].fa[lBc.iFa]);

Code/Source/solver/set_bc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ void set_bc_neu_l(ComMod& com_mod, const CmMod& cm_mod, const bcType& lBc, const
14701470
}
14711471
}
14721472
// Now treat Robin BC (stiffness and damping) here
1473-
if (lBc.robin_bc.is_defined()) {
1473+
if (lBc.robin_bc.is_initialized()) {
14741474
set_bc_rbnl(com_mod, lFa, lBc.robin_bc, Yg, Dg);
14751475
}
14761476
}

0 commit comments

Comments
 (0)