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 VERSION "4"
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>
41class SolarVectorVariableSpice : public GenericSubmodule<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 ConstSpiceChar* m_target = "SUN";
52 SpiceDouble m_et;
53 SpiceDouble m_start_time_et;
54 SpiceDouble m_light_time;
55 SpiceDouble m_body_center_to_sun_vector_rotating[3];
56
57 public:
58 SolarVectorVariableSpice(SimulationClassBase<T>* sim);
59 bool setup(std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
60 override; // loads module into necessary module chains (auto load for calculation
61 // chains)
62 bool exec(std::string_view param) override; // main functionality of the module
63
64 bool init() override;
65 bool preTimeStep() override;
66 bool postTimeStep() override { return true; };
67 bool output() override { return true; };
68
69 void calculateSolarVector();
70
71 void setFieldPtr(std::shared_ptr<std::valarray<T>> field_ptr) override
72 {
73 this->module_field = field_ptr;
74 }
75
76 static std::shared_ptr<GenericSubmodule<T>> createMethode(SimulationClassBase<T>* sim)
77 {
78 return std::make_shared<SolarVectorVariableSpice<T>>(sim);
79 }
80 static std::string getName() { return "SolarVectorVariableSpice"; }
81 std::string_view getNameLocal() const override { return "SolarVectorVariableSpice"; };
82 std::vector<std::string> getDependencies() const override
83 {
84 return this->ini_file_data.getStringVectorParameters("dependencies");
85 };
86};
87
88// ================= Implementation =================
89
90template <typename T>
91SolarVectorVariableSpice<T>::SolarVectorVariableSpice(SimulationClassBase<T>* sim)
92 : GenericSubmodule<T>(sim)
93{
94 try
95 {
96 std::string ini_folder_path{
97 this->sim->m_simulation_config.getStringParameters("ini_folder_path")};
98 this->ini_file_data.loadUserInput(ini_folder_path + m_ini_filepath);
99 this->m_generic_submodules = this->ini_file_data.getStringVectorParameters("submodules");
100 }
101 catch (const BadInput& e)
102 {
103 std::cerr << e.what() << '\n';
104 }
105}
106
107template <typename T>
108bool SolarVectorVariableSpice<T>::setup(
109 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_submodules)
110{
111 m_solar_vector.resize(3);
112
113 return true;
114}
115
116template <typename T>
117bool SolarVectorVariableSpice<T>::exec(std::string_view param)
118{
119 if (param == "InitChain")
120 {
121 return init();
122 }
123 if (param == "PreTimeStepChain")
124 {
125 return preTimeStep();
126 }
127 if (param == "PostTimeStepChain")
128 {
129 return postTimeStep();
130 }
131 if (param == "OutputChain")
132 {
133 return output();
134 }
135 return false;
136}
137
138template <typename T>
139void SolarVectorVariableSpice<T>::calculateSolarVector()
140{
141 // Calculate current ephemerides times from current timestep
142 this->m_et = this->m_start_time_et + this->sim->elapsed_time;
143
144 spkpos_c(this->m_target, this->m_et, this->m_rotating_frame_id.c_str(), "NONE",
145 this->m_observer_id.c_str(), this->m_body_center_to_sun_vector_rotating,
146 &this->m_light_time);
147
148 // Change unit from km to m and save in module field
149 for (int i{0}; i < 3; i++)
150 {
151 (*this->module_field)[i] = this->m_body_center_to_sun_vector_rotating[i] * 1000;
152 }
153}
154
155template <typename T>
156bool SolarVectorVariableSpice<T>::preTimeStep()
157{
158 calculateSolarVector();
159 return true;
160}
161
162template <typename T>
163bool SolarVectorVariableSpice<T>::init()
164{
165 // Load metadata kernel (can be done multiple times for different modules)
166 std::cout << "Loading SPICE kernels.\n";
167 furnsh_c(this->ini_file_data.getStringParameters("spice_metakernel_path").c_str());
168 std::cout << "Done.\n";
169
170 // Read ini file for correct IDs
171 this->m_start_time_utc = this->ini_file_data.getStringParameters("start_time_utc");
172 str2et_c(this->m_start_time_utc.c_str(), &this->m_start_time_et);
173 this->m_et = this->m_start_time_et;
174 this->m_observer_id = this->ini_file_data.getStringParameters("object_id");
175 this->m_reference_frame_id = this->ini_file_data.getStringParameters("reference_frame_id");
176 this->m_rotating_frame_id = this->ini_file_data.getStringParameters("rotating_frame_id");
177
178 calculateSolarVector();
179
180 return true;
181}
182
183template <typename T>
184bool SolarVectorVariableSpice<T>::m_registered =
185 ModuleFactory<T>::registerModule(getName(), createMethode);
186
187#endif // SPICE_PRESENT
188
189#endif // SOLAR_VECTOR_VARIABLE_SPICE_H
This error class inherits from std::exception and marks faulty parameter or CL inputs....
Definition IniParser.h:71
const char * what() const noexcept override
Definition IniParser.h:78
Abstract base class for the submodules. Submodules are below managing modules and will only be run by...
Definition GenericSubmodule.h:25
virtual void setFieldPtr(std::shared_ptr< std::valarray< T > > field_ptr)
Definition GenericSubmodule.h:85
virtual std::vector< std::string > getDependencies() const
Definition GenericSubmodule.h:73
virtual bool setup(std::vector< std::shared_ptr< GenericSubmodule< T > > > all_submodules)
Definition GenericSubmodule.h:60
virtual std::string_view getNameLocal() const =0
virtual bool postTimeStep()
Definition GenericSubmodule.h:69
virtual bool exec(std::string_view param)=0
virtual bool init()
Definition GenericSubmodule.h:67
const SimulationClassBase< T > * sim
Definition GenericSubmodule.h:32
virtual bool output()
Definition GenericSubmodule.h:71
virtual bool preTimeStep()
Definition GenericSubmodule.h:68
std::string getStringParameters(const std::string &key) const
Getter function to access the m_parameters map and returns a string. Throws a BadInput error,...
Definition InputManager.cpp:493
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:68
Definition SimulationClassBase.h:15
InputManager m_simulation_config
Definition SimulationClassBase.h:24
T elapsed_time
Definition SimulationClassBase.h:26