Skip to content

Commit

Permalink
Merge pull request #497 from ut-issl/feature/fix-small-thermal-dynamics
Browse files Browse the repository at this point in the history
Small refactor of thermal dynamics
  • Loading branch information
200km authored Oct 5, 2023
2 parents 0e58e9c + 0d6d0cb commit 9c21a16
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 136 deletions.
15 changes: 12 additions & 3 deletions src/dynamics/thermal/heater.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "heater.hpp"

#include <cassert>
#include <cmath>

using namespace std;
Expand Down Expand Up @@ -30,6 +29,16 @@ void Heater::PrintParam(void) {
}

void Heater::AssertHeaterParams(void) {
assert(heater_id_ >= 1); // Heater ID must be larger than 1
assert(power_rating_W_ >= 0); // Power rating must be larger than 0
// Heater ID must be larger than 1
if (heater_id_ < 1) {
std::cerr << "[WARNING] heater: heater ID is smaller than 1. " << std::endl;
heater_id_ += 1;
std::cerr << "The heater ID is set as " << heater_id_ << std::endl;
}
// Power rating must be larger than 0
if (power_rating_W_ < 0.0) {
std::cerr << "[WARNING] heater: power rating is smaller than 0.0 [W]. " << std::endl;
std::cerr << "The value is set as 0.0." << std::endl;
power_rating_W_ = 0.0;
}
}
14 changes: 7 additions & 7 deletions src/dynamics/thermal/heater.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ enum class HeaterStatus {
* @brief class for heater hardware
*/
class Heater {
protected:
unsigned int heater_id_; // heater id (Use values over 1)
double power_rating_W_; // Power Rating (100% Duty) [W]

HeaterStatus heater_status_; // Power Status of Heater
double power_output_W_; // Power Output of Heater [W]

public:
/**
* @fn Heater
Expand Down Expand Up @@ -84,6 +77,13 @@ class Heater {
*/
void PrintParam(void);

protected:
unsigned int heater_id_; // heater id (Use values over 1)
double power_rating_W_; // Power Rating (100% Duty) [W]

HeaterStatus heater_status_; // Power Status of Heater
double power_output_W_; // Power Output of Heater [W]

/**
* @fn AssertHeaterParams
* @brief Check Heater Parameters
Expand Down
10 changes: 8 additions & 2 deletions src/dynamics/thermal/heater_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "heater_controller.hpp"

#include <cassert>
#include <cmath>

using namespace std;
Expand All @@ -25,4 +24,11 @@ void HeaterController::ControlHeater(Heater* p_heater, double temperature_degC)
}
}

void HeaterController::AssertHeaterControllerParams() { assert(upper_threshold_degC_ > lower_threshold_degC_); }
void HeaterController::AssertHeaterControllerParams() {
if (upper_threshold_degC_ < lower_threshold_degC_) {
std::cerr << "[WARNING] heater_controller: the upper threshold is smaller than the lower threshold. " << std::endl;
double auto_set_upper_threshold_degC = lower_threshold_degC_ + 10.0;
std::cerr << "The the upper threshold set as" << auto_set_upper_threshold_degC << std::endl;
upper_threshold_degC_ = auto_set_upper_threshold_degC;
}
}
16 changes: 8 additions & 8 deletions src/dynamics/thermal/heater_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@
#include "heater.hpp"

class HeaterController {
protected:
double lower_threshold_degC_; // Lower Threshold of Heater Control [degC]
double upper_threshold_degC_; // Upper Threshold of Heater Control [degC]

public:
/**
* @fn HeaterController
Expand All @@ -35,22 +31,22 @@ class HeaterController {
// Getter
/**
* @fn GetLowerThreshold_degC
* @brief Return Lower Thershold [degC]
* @brief Return Lower Threshold [degC]
*/
inline double GetLowerThreshold_degC(void) const { return lower_threshold_degC_; }
/**
* @fn GetUpperThreshold_degC
* @brief Return Upper Thershold [degC]
* @brief Return Upper Threshold [degC]
*/
inline double GetUpperThreshold_degC(void) const { return upper_threshold_degC_; }
/**
* @fn GetLowerThreshold_K
* @brief Return Lower Thershold [K]
* @brief Return Lower Threshold [K]
*/
inline double GetLowerThreshold_K(void) const { return degC2K(lower_threshold_degC_); }
/**
* @fn GetUpperThreshold_K
* @brief Return Upper Thershold [K]
* @brief Return Upper Threshold [K]
*/
inline double GetUpperThreshold_K(void) const { return degC2K(upper_threshold_degC_); }

Expand All @@ -74,6 +70,10 @@ class HeaterController {
*/
void ControlHeater(Heater* p_heater, double temperature_degC);

protected:
double lower_threshold_degC_; // Lower Threshold of Heater Control [degC]
double upper_threshold_degC_; // Upper Threshold of Heater Control [degC]

/**
* @fn AssertHeaterControllerParams
* @brief Check Parameters of HeaterController
Expand Down
30 changes: 15 additions & 15 deletions src/dynamics/thermal/heatload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,6 @@
* @brief Class for calculating heatload value for Node class object at elapsed time
*/
class Heatload {
protected:
double elapsed_time_s_; // Elapsed time [s]
unsigned int node_id_; // Node ID to apply heatload
std::vector<double> time_table_s_; // Times that internal heatload values are defined [s]
std::vector<double> internal_heatload_table_W_; // Defined internal heatload values [W]

unsigned int elapsed_time_idx_; // index of time_table_s_ that is closest to elapsed_time_s_
double solar_heatload_W_; // Heatload from solar flux [W]
double internal_heatload_W_; // Heatload from internal dissipation [W]
double heater_heatload_W_; // Heatload from heater [W]
double total_heatload_W_; // Total heatload [W]

double time_table_period_s_; // Value of last element of time_table_s_, which represents the period of the heatload table [s]
double residual_elapsed_time_s_; // Residual of dividing elapsed_time_s_ by time_table_period_s_ [s]

public:
/**
* @fn Heatload
Expand Down Expand Up @@ -100,6 +85,21 @@ class Heatload {
*/
inline void SetHeaterHeatload_W(double heater_heatload_W) { heater_heatload_W_ = heater_heatload_W; }

protected:
double elapsed_time_s_; // Elapsed time [s]
unsigned int node_id_; // Node ID to apply heatload
std::vector<double> time_table_s_; // Times that internal heatload values are defined [s]
std::vector<double> internal_heatload_table_W_; // Defined internal heatload values [W]

unsigned int elapsed_time_idx_; // index of time_table_s_ that is closest to elapsed_time_s_
double solar_heatload_W_; // Heatload from solar flux [W]
double internal_heatload_W_; // Heatload from internal dissipation [W]
double heater_heatload_W_; // Heatload from heater [W]
double total_heatload_W_; // Total heatload [W]

double time_table_period_s_; // Value of last element of time_table_s_, which represents the period of the heatload table [s]
double residual_elapsed_time_s_; // Residual of dividing elapsed_time_s_ by time_table_period_s_ [s]

/**
* @fn AssertHeatloadParams
* @brief Check if Heatload Parameters are Correct
Expand Down
10 changes: 5 additions & 5 deletions src/dynamics/thermal/initialize_heater.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ Heater InitHeater(const std::vector<std::string>& heater_str) {
size_t heater_str_size_defined = 4; // Correct size of heater_str
assert(heater_str.size() == heater_str_size_defined); // Check if size of heater_str is correct

int heater_id = 0;
size_t heater_id = 0;
double power_rating_W = 0; // [W]

// Index to read from heater_str for each parameter
int index_heater_id = 0;
int index_power_rating = 1;
size_t index_heater_id = 0;
size_t index_power_rating = 1;

heater_id = stoi(heater_str[index_heater_id]);
power_rating_W = stod(heater_str[index_power_rating]);
Expand All @@ -46,8 +46,8 @@ HeaterController InitHeaterController(const std::vector<std::string>& heater_str
assert(heater_str.size() == heater_str_size_defined); // Check if size of heater_str is correct

// Index to read from heater_str for each parameter
int index_lower_threshold = 2;
int index_upper_threshold = 3;
size_t index_lower_threshold = 2;
size_t index_upper_threshold = 3;

double lower_threshold_degC = 0; // [degC]
double upper_threshold_degC = 0; // [degC]
Expand Down
2 changes: 1 addition & 1 deletion src/dynamics/thermal/initialize_heatload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Heatload InitHeatload(const std::vector<std::string>& time_str, const std::vecto
int node_id = stoi(internal_heatload_str[0]); // First data of internal_heatload_str is node id

// read table
for (unsigned int i = 0; i < time_str.size() - 1; ++i) {
for (size_t i = 0; i < time_str.size() - 1; ++i) {
time_table_s[i] = stod(time_str[i + 1]);
internal_heatload_table_W[i] = stod(internal_heatload_str[i + 1]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/dynamics/thermal/initialize_heatload.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* @fn InitHeatload
* @brief Intialize Heatload object from csv file
* @brief Initialize Heatload object from csv file
* @param[in] time_str: str representing time table, read from csv file
* @param[in] internal_heatload_str: str representing internal heatload table, read from csv file
* @return Heatload
Expand Down
28 changes: 14 additions & 14 deletions src/dynamics/thermal/initialize_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,25 @@ Node InitNode(const std::vector<std::string>& node_str) {
size_t node_str_size_defined = 11; // Correct size of node_str
assert(node_str.size() == node_str_size_defined); // Check if size of node_str is correct

int node_id = 0; // node number
size_t node_id = 0; // node number
std::string node_label = "temp"; // node name
int node_type_int = 0; // node type
int heater_id = 0; // heater node index
size_t node_type_int = 0; // node type
size_t heater_id = 0; // heater node index
double temperature_K = 0; // [K]
double capacity_J_K = 0; // [J/K]
double alpha = 0; // []
double area_m2 = 0; // [m^2]

// Index to read from node_str for each parameter
int index_node_id = 0;
int index_node_label = 1;
int index_node_type = 2;
int index_heater_id = 3;
int index_capacity = 4;
int index_alpha = 5;
int index_area = 6;
int index_normal_v_b_head = 7;
int index_temperature = 10;
size_t index_node_id = 0;
size_t index_node_label = 1;
size_t index_node_type = 2;
size_t index_heater_id = 3;
size_t index_capacity = 4;
size_t index_alpha = 5;
size_t index_area = 6;
size_t index_normal_v_b_head = 7;
size_t index_temperature = 10;

node_id = stoi(node_str[index_node_id]);
node_label = node_str[index_node_label];
Expand All @@ -70,14 +70,14 @@ Node InitNode(const std::vector<std::string>& node_str) {
alpha = stod(node_str[index_alpha]);
area_m2 = stod(node_str[index_area]);
libra::Vector<3> normal_v_b;
for (int i = 0; i < 3; i++) {
for (size_t i = 0; i < 3; i++) {
normal_v_b[i] = stod(node_str[index_normal_v_b_head + i]);
}

// Normalize Norm Vector (Except for Boundary and Arithmetic Nodes)
if (node_type_int == 0) {
double norm = normal_v_b.CalcNorm();
for (int i = 0; i < 3; i++) {
for (size_t i = 0; i < 3; i++) {
normal_v_b[i] = normal_v_b[i] / norm;
}
}
Expand Down
20 changes: 6 additions & 14 deletions src/dynamics/thermal/initialize_temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "initialize_heatload.hpp"
#include "initialize_node.hpp"

/* Import node properties, heatload_list and Cij/Rij Datas by reading CSV File (node.csv,
/* Import node properties, heatload_list and Cij/Rij Data by reading CSV File (node.csv,
heatload.csv, cij.csv, rij.csv) Detailed process of reading node properties from CSV File, and
CSV file formats of node properties is written in Init_Node.cpp
Expand Down Expand Up @@ -47,14 +47,6 @@ First row is time data
Rij units: m^2 (Equivalent to Ai * Bij * eps_i * eps_j)
*/

/* Parameters
mainIni : main Ini file(simBase.ini)
file_path : directory of Thermal Input files(read from SimBase.ini)
propstep : time step interval for temperature propagation integration(read
from SimBase.ini) mainstepSec: time step interval for Main Routin integration
(= temperature.end_time_)
*/

using std::string;
using std::vector;

Expand Down Expand Up @@ -100,7 +92,7 @@ Temperature* InitTemperature(const std::string file_name, const double rk_prop_s
IniAccess conf_heatload(filepath_heatload);
conf_heatload.ReadCsvString(heatload_str_list, 100);
/*since we don't know the number of node_list yet, set node_num=100 temporary.
Recall that Nodes_num are given to this function only to reseve memory*/
Recall that Nodes_num are given to this function only to reserve memory*/

heatload_num = heatload_str_list.size() - 1;
auto times_itr = heatload_str_list.begin(); // First Row is Time Data
Expand All @@ -113,7 +105,7 @@ Temperature* InitTemperature(const std::string file_name, const double rk_prop_s
IniAccess conf_node(filepath_node);
conf_node.ReadCsvString(node_str_list, 100);
/*since we don't know the number of node_list yet, set node_num=100 temporary.
Recall that Nodes_num are given to this function only to reseve memory*/
Recall that Nodes_num are given to this function only to reserve memory*/

node_num = node_str_list.size() - 1; // First Row is for Header(not data)
node_list.reserve(node_num); // reserve memory
Expand All @@ -128,10 +120,10 @@ Temperature* InitTemperature(const std::string file_name, const double rk_prop_s
IniAccess conf_heater(filepath_heater);
conf_heater.ReadCsvString(heater_str_list, 100);
/*since we don't know the number of heater_list yet, set heater_num=100 temporary.
Recall that heater_num are given to this function only to reseve memory*/
Recall that heater_num are given to this function only to reserve memory*/

heater_num = heater_str_list.size() - 1; // First Row is for Header(not data)
heater_list.reserve(heater_num); // reserve memory
heater_num = heater_str_list.size() - 1; // First Row is for Header(not data)
heater_list.reserve(heater_num); // reserve memory
heater_controller_list.reserve(heater_num);
for (auto itr = heater_str_list.begin() + 1; itr != heater_str_list.end(); ++itr) { // first row is for labels
heater_list.push_back(InitHeater(*itr));
Expand Down
35 changes: 26 additions & 9 deletions src/dynamics/thermal/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

#include "node.hpp"

#include <cassert>
#include <cmath>
#include <environment/global/physical_constants.hpp>

using namespace std;
using namespace libra;

Node::Node(const int node_id, const string node_name, const NodeType node_type, const int heater_id, const double temperature_ini_K,
Node::Node(const size_t node_id, const string node_name, const NodeType node_type, const size_t heater_id, const double temperature_ini_K,
const double capacity_J_K, const double alpha, const double area_m2, libra::Vector<3> normal_vector_b)
: node_id_(node_id),
node_name_(node_name),
Expand Down Expand Up @@ -57,17 +56,35 @@ void Node::PrintParam(void) {
cout << " node type : " << node_type_str << endl;
cout << " heater id : " << heater_id_ << endl;
cout << " Normal Vector: ";
for (int i = 0; i < 3; i++) {
for (size_t i = 0; i < 3; i++) {
cout << normal_vector_b_[i] << " ";
}
cout << endl;
}

void Node::AssertNodeParams(void) {
assert(node_id_ >= 0); // Node ID should be larger than 0
assert(heater_id_ >= 0); // Heater ID should be larger than 0
assert(temperature_K_ >= environment::absolute_zero_degC); // Temperature must be larger than zero kelvin
assert(capacity_J_K_ >= 0); // Capacity must be larger than 0, use 0 when node is boundary or arithmetic
assert(0 <= alpha_ && alpha_ <= 1); // alpha must be between 0 and 1
assert(area_m2_ >= 0); // Area must be larger than 0
// Temperature must be larger than zero kelvin
if (temperature_K_ < 0.0) {
std::cerr << "[WARNING] node: temperature is less than zero [K]." << std::endl;
std::cerr << "The value is set as 0.0." << std::endl;
temperature_K_ = 0.0;
}
// Capacity must be larger than 0, use 0 when node is boundary or arithmetic
if (capacity_J_K_ < 0.0) {
std::cerr << "[WARNING] node: capacity is less than zero [J/K]." << std::endl;
std::cerr << "The value is set as 0.0." << std::endl;
capacity_J_K_ = 0.0;
}
// alpha must be between 0 and 1
if (alpha_ < 0.0 || alpha_ > 1.0) {
std::cerr << "[WARNING] node: alpha is over the range [0, 1]." << std::endl;
std::cerr << "The value is set as 0.0." << std::endl;
alpha_ = 0.0;
}
// Area must be larger than 0
if (area_m2_ < 0.0) {
std::cerr << "[WARNING] node: area is less than zero [m2]." << std::endl;
std::cerr << "The value is set as 0.0." << std::endl;
area_m2_ = 0.0;
}
}
Loading

0 comments on commit 9c21a16

Please sign in to comment.