Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Code/Source/solver/BoundaryCondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@

#define n_debug_bc

BoundaryCondition::BoundaryCondition(const std::string& vtp_file_path, const std::vector<std::string>& array_names, const StringBoolMap& flags, const faceType& face)
BoundaryCondition::BoundaryCondition(const std::string& vtp_file_path, const std::vector<std::string>& array_names, const StringBoolMap& flags, const faceType& face, SimulationLogger& logger)
: face_(&face)
, global_num_nodes_(face.nNo)
, local_num_nodes_(0)
, array_names_(array_names)
, spatially_variable(true)
, vtp_file_path_(vtp_file_path)
, flags_(flags)
, logger_(&logger)
{
try {
global_data_ = read_data_from_vtp_file(vtp_file_path, array_names);
Expand All @@ -73,13 +74,14 @@ BoundaryCondition::BoundaryCondition(const std::string& vtp_file_path, const std
}
}

BoundaryCondition::BoundaryCondition(const StringDoubleMap& uniform_values, const StringBoolMap& flags, const faceType& face)
BoundaryCondition::BoundaryCondition(const StringDoubleMap& uniform_values, const StringBoolMap& flags, const faceType& face, SimulationLogger& logger)
: face_(&face)
, global_num_nodes_(face.nNo)
, local_num_nodes_(0)
, spatially_variable(false)
, vtp_file_path_("")
, flags_(flags)
, logger_(&logger)
{
try {
// Store array names, validate and store values
Expand Down Expand Up @@ -107,6 +109,7 @@ BoundaryCondition::BoundaryCondition(const BoundaryCondition& other)
, vtp_file_path_(other.vtp_file_path_)
, flags_(other.flags_)
, global_node_map_(other.global_node_map_)
, logger_(other.logger_)
{
if (other.vtp_data_) {
vtp_data_ = std::make_unique<VtkVtpData>(*other.vtp_data_);
Expand All @@ -126,6 +129,7 @@ void swap(BoundaryCondition& lhs, BoundaryCondition& rhs) noexcept {
swap(lhs.flags_, rhs.flags_);
swap(lhs.global_node_map_, rhs.global_node_map_);
swap(lhs.vtp_data_, rhs.vtp_data_);
swap(lhs.logger_, rhs.logger_);
}

BoundaryCondition& BoundaryCondition::operator=(BoundaryCondition other) {
Expand All @@ -145,6 +149,7 @@ BoundaryCondition::BoundaryCondition(BoundaryCondition&& other) noexcept
, flags_(std::move(other.flags_))
, global_node_map_(std::move(other.global_node_map_))
, vtp_data_(std::move(other.vtp_data_))
, logger_(other.logger_)
{
other.face_ = nullptr;
other.global_num_nodes_ = 0;
Expand Down Expand Up @@ -203,6 +208,11 @@ BoundaryCondition::StringArrayMap BoundaryCondition::read_data_from_vtp_file(con
#endif
}

logger_ -> log_message("[BoundaryCondition] Loaded from VTP file");
logger_ -> log_message("\t File path:", vtp_file_path);
logger_ -> log_message("\t Arrays:", array_names);
logger_ -> log_message("\t Face:", face_->name);

return result;
}

Expand Down
8 changes: 6 additions & 2 deletions Code/Source/solver/BoundaryCondition.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "CmMod.h"
#include "VtkData.h"
#include <string>
#include "SimulationLogger.h"
#include <memory>
#include <map>
#include <vector>
Expand Down Expand Up @@ -90,6 +91,7 @@ class BoundaryCondition {
std::string vtp_file_path_; ///< Path to VTP file (empty if uniform)
std::map<int, int> global_node_map_; ///< Maps global node IDs to local array indices
std::unique_ptr<VtkVtpData> vtp_data_; ///< VTP data object
SimulationLogger* logger_ = nullptr; ///< Logger for warnings/info

public:
/// @brief Tolerance for point matching in VTP files
Expand All @@ -102,13 +104,15 @@ class BoundaryCondition {
/// @param vtp_file_path Path to VTP file containing arrays
/// @param array_names Names of arrays to read from VTP file
/// @param face Face associated with the BC
/// @param logger Simulation logger used to write warnings
/// @throws std::runtime_error if file cannot be read or arrays are missing
BoundaryCondition(const std::string& vtp_file_path, const std::vector<std::string>& array_names, const StringBoolMap& flags, const faceType& face);
BoundaryCondition(const std::string& vtp_file_path, const std::vector<std::string>& array_names, const StringBoolMap& flags, const faceType& face, SimulationLogger& logger);

/// @brief Constructor for uniform values
/// @param uniform_values Map of array names to uniform values
/// @param face Face associated with the BC
BoundaryCondition(const StringDoubleMap& uniform_values, const StringBoolMap& flags, const faceType& face);
/// @param logger Simulation logger used to write warnings
BoundaryCondition(const StringDoubleMap& uniform_values, const StringBoolMap& flags, const faceType& face, SimulationLogger& logger);

/// @brief Copy constructor
BoundaryCondition(const BoundaryCondition& other);
Expand Down
8 changes: 4 additions & 4 deletions Code/Source/solver/RobinBoundaryCondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

#define n_debug_robin_bc

RobinBoundaryCondition::RobinBoundaryCondition(double uniform_stiffness, double uniform_damping, bool normal_only, const faceType& face)
: BoundaryCondition({{"Stiffness", uniform_stiffness}, {"Damping", uniform_damping}}, StringBoolMap{{"normal_direction_only", normal_only}}, face)
RobinBoundaryCondition::RobinBoundaryCondition(double uniform_stiffness, double uniform_damping, bool normal_only, const faceType& face, SimulationLogger& logger)
: BoundaryCondition({{"Stiffness", uniform_stiffness}, {"Damping", uniform_damping}}, StringBoolMap{{"normal_direction_only", normal_only}}, face, logger)
{
// Warning if both stiffness and damping are zero
if (uniform_stiffness == 0.0 && uniform_damping == 0.0) {
std::cout << "WARNING: Robin boundary condition has both stiffness and damping values set to zero. "
<< "This will result in effectively no boundary condition being applied." << std::endl;
logger_ -> log_message("WARNING [RobinBoundaryCondition] Both stiffness and damping values set to zero. "
"This will result in effectively no boundary condition being applied.");
}
}

8 changes: 5 additions & 3 deletions Code/Source/solver/RobinBoundaryCondition.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,22 @@ class RobinBoundaryCondition : public BoundaryCondition {
/// @param vtp_file_path Path to VTP file containing Stiffness and Damping point arrays
/// @param normal_only Flag to apply only along normal direction
/// @param face Face associated with the Robin BC
/// @param logger Simulation logger used to write warnings
/// @throws BoundaryConditionFileException if file cannot be read
/// @throws BoundaryConditionVtpArrayException if arrays are missing
/// @throws BoundaryConditionValidationException if values are invalid
RobinBoundaryCondition(const std::string& vtp_file_path, bool normal_only, const faceType& face)
: BoundaryCondition(vtp_file_path, std::vector<std::string>{"Stiffness", "Damping"}, StringBoolMap{{"normal_direction_only", normal_only}}, face) {}
RobinBoundaryCondition(const std::string& vtp_file_path, bool normal_only, const faceType& face, SimulationLogger& logger)
: BoundaryCondition(vtp_file_path, std::vector<std::string>{"Stiffness", "Damping"}, StringBoolMap{{"normal_direction_only", normal_only}}, face, logger) {}


/// @brief Constructor for uniform values
/// @param uniform_stiffness Uniform stiffness value for all nodes
/// @param uniform_damping Uniform damping value for all nodes
/// @param normal_only Flag to apply only along normal direction
/// @param face Face associated with the Robin BC
/// @param logger Simulation logger used to write warnings
/// @throws BoundaryConditionValidationException if values are invalid
RobinBoundaryCondition(double uniform_stiffness, double uniform_damping, bool normal_only, const faceType& face);
RobinBoundaryCondition(double uniform_stiffness, double uniform_damping, bool normal_only, const faceType& face, SimulationLogger& logger);

/// @brief Apply only along normal direction (getter)
/// @return true if BC should be applied only along normal direction
Expand Down
36 changes: 36 additions & 0 deletions Code/Source/solver/SimulationLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class SimulationLogger {
file_name_ = file_name;
}

bool is_initialized() const { return log_file_.is_open(); }

~SimulationLogger()
{
log_file_.close();
Expand All @@ -78,6 +80,27 @@ class SimulationLogger {
return *this;
}

// Special handling for vector<string>
SimulationLogger& operator<< (const std::vector<std::string>& values)
{
if (file_name_ == "") {
return *this;
}

bool first = true;
for (const auto& value : values) {
if (!first) {
log_file_ << ", ";
if (cout_write_) std::cout << ", ";
}
log_file_ << value;
if (cout_write_) std::cout << value;
first = false;
}

return *this;
}

SimulationLogger& operator<<(std::ostream&(*value)(std::ostream& o))
{
if (file_name_ == "") {
Expand All @@ -93,8 +116,21 @@ class SimulationLogger {
return *this;
};

/// @brief Log a message with automatic space separation
/// @param args Arguments to log
template<typename... Args>
SimulationLogger& log_message(const Args&... args) {
if (file_name_ == "") return *this;

bool first = true;
((first ? (first = false, (*this << args)) : (*this << ' ' << args)), ...);
*this << std::endl;
return *this;
}

private:
bool cout_write_ = false;
bool initialized_ = false;
std::string file_name_;
std::ofstream log_file_;
};
Expand Down
16 changes: 0 additions & 16 deletions Code/Source/solver/initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,22 +371,6 @@ void initialize(Simulation* simulation, Vector<double>& timeP)
dmsg.banner();
#endif

// Setup logging to history file.
if (!simulation->com_mod.cm.slv(simulation->cm_mod)) {
std::string hist_file_name;

if (chnl_mod.appPath != "") {
auto mkdir_arg = std::string("mkdir -p ") + chnl_mod.appPath;
std::system(mkdir_arg.c_str());
hist_file_name = chnl_mod.appPath + "/" + simulation->history_file_name;
} else {
hist_file_name = simulation->history_file_name;
}

bool output_to_cout = true;
simulation->logger.initialize(hist_file_name, output_to_cout);
}

#ifdef debug_initialize
dmsg << "Set time " << std::endl;
dmsg << "com_mod.timeP[0]: " << com_mod.timeP[0] << std::endl;
Expand Down
24 changes: 21 additions & 3 deletions Code/Source/solver/read_files.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,14 @@ void read_bc(Simulation* simulation, EquationParameters* eq_params, eqType& lEq,
if (bc_params->spatial_values_file_path.defined()) {
lBc.robin_bc = RobinBoundaryCondition(bc_params->spatial_values_file_path.value(),
bc_params->apply_along_normal_direction.value(),
com_mod.msh[lBc.iM].fa[lBc.iFa]);
com_mod.msh[lBc.iM].fa[lBc.iFa],
simulation->logger);
} else {
lBc.robin_bc = RobinBoundaryCondition(bc_params->stiffness.value(),
bc_params->damping.value(),
bc_params->apply_along_normal_direction.value(),
com_mod.msh[lBc.iM].fa[lBc.iFa]);
com_mod.msh[lBc.iM].fa[lBc.iFa],
simulation->logger);
}
}

Expand Down Expand Up @@ -1703,7 +1705,23 @@ void read_files(Simulation* simulation, const std::string& file_name)
com_mod.stFileFlag = gen_params.continue_previous_simulation.value();
}

// Set simulatioin and module member data from XML parameters.
// Setup logging to history file.
if (!simulation->com_mod.cm.slv(simulation->cm_mod)) {
std::string hist_file_name;

if (chnl_mod.appPath != "") {
auto mkdir_arg = std::string("mkdir -p ") + chnl_mod.appPath;
std::system(mkdir_arg.c_str());
hist_file_name = chnl_mod.appPath + "/" + simulation->history_file_name;
} else {
hist_file_name = simulation->history_file_name;
}

bool output_to_cout = true;
simulation->logger.initialize(hist_file_name, output_to_cout);
}

// Set simulation and module member data from XML parameters.
simulation->set_module_parameters();

// Read mesh and BCs data.
Expand Down
Loading