MoCSI API Reference
Loading...
Searching...
No Matches
SolarVectorVariableSpice.h
Go to the documentation of this file.
1#ifndef SOLAR_VECTOR_VARIABLE_SPICE_H
2#define SOLAR_VECTOR_VARIABLE_SPICE_H
3
4#define SOLAR_VECTOR_VARIABLE_SPICE_VERSION "8"
5
6#ifdef SPICE_PRESENT
7
8extern "C"
9{
10#include "SpiceUsr.h"
11}
12
13#include <iostream>
14#include <memory>
15#include <string>
16#include <valarray>
17
18#include "../../src/GenericSubmodule.h"
19#include "../../src/ModuleFactory.h"
20
40template <typename T>
42{
43 private:
44 static bool m_registered;
45 std::string m_ini_filepath{"solar_vector/SolarVectorVariableSpice.ini"};
46 std::valarray<T> m_solar_vector;
47 std::string m_start_time_utc;
48 std::string m_observer_id;
49 std::string m_reference_frame_id;
50 std::string m_rotating_frame_id;
51 std::string m_abcorr;
52 ConstSpiceChar* m_target = "SUN";
57
58 public:
60 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
61 override; // loads module into necessary module chains (auto load for calculation
62 // chains)
63 bool exec(std::string_view param) override; // main functionality of the module
64
65 bool init() override;
66 bool preTimeStep() override;
67 bool postTimeStep() override { return true; };
68 bool output() override { return true; };
69
70 void calculateSolarVector();
71
72 void setFieldPtr(std::shared_ptr<std::valarray<T>> field_ptr) override
73 {
74 this->module_field = field_ptr;
75 }
76
77 static std::shared_ptr<GenericSubmodule<T>> createMethode(SimulationClassBase<T>* sim)
78 {
79 return std::make_shared<SolarVectorVariableSpice<T>>(sim);
80 }
81 static std::string getName() { return "SolarVectorVariableSpice"; }
82 std::string_view getNameLocal() const override { return "SolarVectorVariableSpice"; };
83 std::vector<std::string> getDependencies() const override
84 {
85 return this->ini_file_data.getStringVectorParameters("dependencies");
86 };
87};
88
89// ================= Implementation =================
90
91template <typename T>
93 : GenericSubmodule<T>(sim)
94{
95 try
96 {
97 std::string ini_folder_path{
98 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
99 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
100 this->m_generic_submodules = this->ini_file_data.getStringVectorParameters("submodules");
101 }
102 catch (const BadInput& e)
103 {
104 std::cerr << "[SolarVectorVariableSpice]: " << e.what() << '\n';
105 throw BadInput(static_cast<std::string>("[SolarVectorVariableSpice]: ")
106 + static_cast<std::string>(e.what()));
107 }
108}
109
110template <typename T>
112 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
113{
114 m_solar_vector.resize(3);
115
116 return true;
117}
118
119template <typename T>
120bool SolarVectorVariableSpice<T>::exec(std::string_view param)
121{
122 if (param == "InitChain")
123 {
124 return init();
125 }
126 if (param == "PreTimeStepChain")
127 {
128 return preTimeStep();
129 }
130 if (param == "PostTimeStepChain")
131 {
132 return postTimeStep();
133 }
134 if (param == "OutputChain")
135 {
136 return output();
137 }
138 return false;
139}
140
141template <typename T>
143{
144 // Calculate current ephemerides times from current timestep
145 this->m_et = this->m_start_time_et + this->sim->elapsed_time;
146
147 spkpos_c(this->m_target, this->m_et, this->m_rotating_frame_id.c_str(), this->m_abcorr.c_str(),
148 this->m_observer_id.c_str(), this->m_body_center_to_sun_vector_rotating,
149 &this->m_light_time);
150
151 // Change unit from km to m and save in module field
152 for (int i{0}; i < 3; i++)
153 {
154 (*this->module_field)[i] = this->m_body_center_to_sun_vector_rotating[i] * 1000;
155 }
156}
157
158template <typename T>
160{
161 calculateSolarVector();
162 return true;
163}
164
165template <typename T>
167{
168 // Load metadata kernel (can be done multiple times for different modules)
169 if (this->sim->my_rank == 0)
170 {
171 std::cout << "[SolarVectorVariableSpice]: Loading SPICE kernels.\n";
172 }
173 try
174 {
175 furnsh_c(this->ini_file_data.getStringParameters("spice_metakernel_path").c_str());
176 }
177 catch (const BadInput& e)
178 {
179 std::cerr << "[SolarVectorVariableSpice]: " << e.what() << '\n';
180 throw BadInput(static_cast<std::string>("[SolarVectorVariableSpice]: ")
181 + static_cast<std::string>(e.what()));
182 }
183 if (this->sim->my_rank == 0)
184 {
185 std::cout << "[SolarVectorVariableSpice]: Done.\n";
186 }
187
188 // Read ini file for correct IDs
189 try
190 {
191 this->m_start_time_utc = this->ini_file_data.getStringParameters("start_time_utc");
192 str2et_c(this->m_start_time_utc.c_str(), &this->m_start_time_et);
193 this->m_et = this->m_start_time_et;
194 this->m_observer_id = this->ini_file_data.getStringParameters("object_id");
195 this->m_reference_frame_id = this->ini_file_data.getStringParameters("reference_frame_id");
196 this->m_rotating_frame_id = this->ini_file_data.getStringParameters("rotating_frame_id");
197 this->m_abcorr = this->ini_file_data.getStringParameters("abcorr");
198 }
199 catch (const BadInput& e)
200 {
201 std::cerr << "[SolarVectorVariableSpice]: " << e.what() << '\n';
202 throw BadInput(static_cast<std::string>("[SolarVectorVariableSpice]: ")
203 + static_cast<std::string>(e.what()));
204 }
205 if (this->sim->my_rank == 0)
206 {
207 std::cout << "[SolarVectorVariableSpice]: Loaded parameters.\n";
208 }
209
210 calculateSolarVector();
211
212 return true;
213}
214
215template <typename T>
217 ModuleFactory<T>::registerModule(getName(), createMethode);
218
219#endif // SPICE_PRESENT
220
221#endif // SOLAR_VECTOR_VARIABLE_SPICE_H
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:94
Abstract base class for the submodules. Submodules are below managing modules and will only be run by...
Definition GenericSubmodule.h:25
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:76