MoCSI API Reference
Loading...
Searching...
No Matches
BCSinusoidalTemperature.h
Go to the documentation of this file.
1#ifndef BOUNDARY_CONDITION_SINUSOIDAL_TEMPERATURE_H
2#define BOUNDARY_CONDITION_SINUSOIDAL_TEMPERATURE_H
3
4#include <cmath>
5#include <memory>
6#include <numbers>
7#include <string>
8#include <valarray>
9#include <vector>
10
12#include "SimulationClassBase.h"
13
17template <typename T>
19{
20 private:
21 T m_temperature_amplitude{};
22 T m_base_temperature{};
23 T m_period{};
24 T m_dt{};
25 int m_numerical_layer{};
26 std::vector<std::reference_wrapper<T>> m_capacitance_extra_val{};
27 std::vector<std::reference_wrapper<T>> m_stiffness_extra_val{};
28 std::vector<std::reference_wrapper<T>> m_forcing_extra_val{};
29
30 public:
31 void writeBoundaryCondition() override;
32 void getReferenceToElements(GenericMatrix<T>& capacitance_matrix,
33 GenericMatrix<T>& stiffness_matrix,
34 std::valarray<T>& forcing_vector) override;
35
36 BCSinusoidalTemperature(SimulationClassBase<T>* sim, std::vector<int> boundary_points,
37 std::vector<std::vector<int>> connected_points, int facet_nr,
38 T area, std::string face_prefix);
39 ~BCSinusoidalTemperature() override = default;
40};
41
53template <typename T>
55 std::vector<int> boundary_points,
56 std::vector<std::vector<int>> connected_points,
57 int facet_nr, T area, std::string face_prefix)
58 : GenericBoundaryCondition<T>{sim, boundary_points, connected_points, facet_nr, face_prefix},
59 m_base_temperature(
60 this->m_sim->m_simulation_config.getDoubleParameters("initial_temperature")),
61 m_numerical_layer(this->m_sim->m_simulation_config.getIntParameters("numerical_layers"))
62{
63 try
64 {
65 m_temperature_amplitude = this->m_sim->m_simulation_config.getDoubleParameters(
66 this->m_face_indicator_prefix + "temperature_amplitude");
67 m_period = this->m_sim->m_simulation_config.getDoubleParameters(
68 this->m_face_indicator_prefix + "period");
69 }
70 catch (const BadInput& e)
71 {
72 throw std::runtime_error(
73 "[BCSinusoidalTemperature] Couldn't find amplitude and/or period value in simulation "
74 "config under: "
75 + this->m_face_indicator_prefix + "temperature_amplitude / "
76 + this->m_face_indicator_prefix + "period.");
77 }
78 m_dt = this->m_sim->m_simulation_config.getDoubleParameters("time_delta");
79}
80
87template <typename T>
89 GenericMatrix<T>& stiffness_matrix,
90 std::valarray<T>& forcing_vector)
91{
92 for (int i{0}; i < this->m_boundary_points.size(); i++)
93 {
94 std::vector<std::reference_wrapper<T>> temp_cap_mat{};
95 std::vector<std::reference_wrapper<T>> temp_stiff_mat{};
96 for (int j{0}; j < this->m_connected_points[i].size(); j++)
97 {
98 temp_cap_mat.emplace_back(
99 capacitance_matrix(this->m_boundary_points[i], this->m_connected_points[i][j]));
100 temp_stiff_mat.emplace_back(
101 stiffness_matrix(this->m_boundary_points[i], this->m_connected_points[i][j]));
102 }
103 this->m_capacitance_matrix_elements.emplace_back(temp_cap_mat);
104 this->m_stiffness_matrix_elements.emplace_back(temp_stiff_mat);
105 // These extra values are needed for dirichlet boundary conditions in the FEM scheme
106 this->m_forcing_vector_elements.emplace_back(forcing_vector[this->m_boundary_points[i]]);
107 m_capacitance_extra_val.emplace_back(
108 capacitance_matrix(this->m_boundary_points[i] + 1, this->m_boundary_points[i]));
109 m_stiffness_extra_val.emplace_back(
110 stiffness_matrix(this->m_boundary_points[i] + 1, this->m_boundary_points[i]));
111 m_forcing_extra_val.emplace_back(forcing_vector[this->m_boundary_points[i] + 1]);
112 }
113}
114
118template <typename T>
120{
121 for (int i{0}; i < this->m_boundary_points.size(); i++)
122 {
123 for (auto& val : this->m_capacitance_matrix_elements[i])
124 {
125 val *= 0.0;
126 }
127 for (auto& val : this->m_stiffness_matrix_elements[i])
128 {
129 val *= 0.0;
130 }
131 // Set almost everything to 0, so that the top equation in the matrix looks like T_n+1 * 1 =
132 // T_new.
133 this->m_stiffness_matrix_elements[i][0] += 1.0;
134 this->m_forcing_vector_elements[i] *= 0.0;
135 T st_mat_val{m_stiffness_extra_val[i]};
136 T ca_mat_val{m_capacitance_extra_val[i]};
137 m_stiffness_extra_val[i] *= 0.0;
138 m_capacitance_extra_val[i] *= 0.0;
139 // Calculate the temperature for this and the previous time step. The first is used for the
140 // top equation and the other for the second equation
141 T temperature_previous_ts{
142 m_base_temperature
143 - m_temperature_amplitude
144 * std::sin(2 * std::numbers::pi * (this->m_sim->time_step - 1) * m_dt
145 / m_period)};
146 T temperature{
147 m_base_temperature
148 - m_temperature_amplitude
149 * std::sin(2 * std::numbers::pi * (this->m_sim->time_step * m_dt) / m_period)};
150 this->m_forcing_vector_elements[i] += temperature;
151 m_forcing_extra_val[i] -= (st_mat_val * temperature + ca_mat_val * temperature / m_dt
152 - ca_mat_val * temperature_previous_ts / m_dt);
153 // this->m_sim->forcing_vector[1] -= (st_mat_val * temperature * dt + 0.0 * ca_mat_val *
154 // temperature);
155 }
156}
157
158#endif
Concrete implementation of a sinusoidally varying temperature boundary condition in 1D.
Definition BCSinusoidalTemperature.h:19
void writeBoundaryCondition() override
Actual implementation of the boundary condition.
Definition BCSinusoidalTemperature.h:119
void getReferenceToElements(GenericMatrix< T > &capacitance_matrix, GenericMatrix< T > &stiffness_matrix, std::valarray< T > &forcing_vector) override
Sets up a referrence_wrapper vector to the elements within the matrix object. This adds a bit of pre-...
Definition BCSinusoidalTemperature.h:88
BCSinusoidalTemperature(SimulationClassBase< T > *sim, std::vector< int > boundary_points, std::vector< std::vector< int > > connected_points, int facet_nr, T area, std::string face_prefix)
Constructor for a BCSinusoidalTemperature object.
Definition BCSinusoidalTemperature.h:54
~BCSinusoidalTemperature() override=default
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
Boundary condition abstract base class for the finite element method. All boundary condition implemen...
Definition GenericBoundaryCondition.h:18
Template generalized Matrix object that should be the base for all further implementations of a matri...
Definition GenericMatrix.h:19
double getDoubleParameters(const std::string &key) const
Getter function to access the m_DoubleParameters map and returns a double.
Definition InputManager.cpp:477
Definition SimulationClassBase.h:15
InputManager m_simulation_config
Definition SimulationClassBase.h:24