DamBreak++ Wave Simulator 0.3
DamBreak++ Simulation Framework
Loading...
Searching...
No Matches
Testvs19_Numethod_f.h
Go to the documentation of this file.
1#pragma once
2
3// SxBase19 includes
4#include "include/Sfx_Directory.h"
5// DamBreak include
13
14namespace Testvs19
15{
19 {
20 public:
26 : m_rhsAlgo{ aRhsAlgo },
27 // m_phySystem{ nullptr },
28 m_U12{ nullptr,nullptr },
29 // m_U12p{ nullptr,nullptr },
30 m_dbgFile{ true }
31 {}
32
36 void initialize( const std::shared_ptr<dbpp::FiniteVolumeDiscretization>& aGblDiscr) override final
37 {
38 // PhysicalAlgorithm w_phyAlgo;
39 if (nullptr == w_phySystem)
40 {
41 // use of factory to create these simulation types
42 // IPhysicalConfigure = new PhysicalConfigure
43 // IPhysicalMeasurement = new PhysicalMeasure
44 //
45 // physical system (made of physical objects)
47 w_phyConfig->configure( w_phySystem/*, aGblDiscr*/);
48 }
49 }
50
55 void mainLoop( const std::shared_ptr<dbpp::FiniteVolumeDiscretization>& aGblDiscr,
56 Sfx::SfxTimePrm& aTimePrm) override final
57 {
58
59 w_fileDatStore.open();
60 if (!w_fileDatStore.isOpen())
61 {
62 std::cerr << "Couldn't open data store -- Stop simulation\n"; // debugging purpose
63 }
64
65 // TODO
66 // Time stepping (cfl criteria)
67 // Physical boundary condition
68 // Set time parameters (start,stop) ok its done since we retrieve from arg
69 // Simulaton setimestep
70 //
71
72 std::cout << "Simulation initial time is: " << aTimePrm.getStartTime() << "\n";
73 std::cout << "Simulation final time is: " << aTimePrm.getFinalTime() << "\n";
74 std::cout << "Simulation delta (time step) is: " << aTimePrm.Delta() << "\n";
75
76 // ================================================
77 // logical time loop SfxTimePrm
78 // ================================================
79
80 while (!aTimePrm.finished()) // base on stop time value
81 {
82#if _DEBUG
83 // Writing some debug results to file for visualization (DamBreak++ visual debugger)
84 Sfx::DbgLogger* w_dbgLog = Sfx::DbgLogger::instance();
85 // auto chk = Sfx::Directory::getWrkDbgFile();
86 w_dbgLog->open(Sfx::Directory::getWrkDbgFile());
87 if (!w_dbgLog->isOpen())
88 {
89 std::cout << "Couldn't open file for writing debug info\n";
90 }
91 // shall use the time prm method "numberStepNo()" something like that!!
92 // Simulation is model of a bean ()
93 w_dbgLog->write2file(std::string(""), Sfx::SimulationMgr::getSingleton().getNbIterations() + 1);
94 w_dbgLog->close();
95#endif
96 // calculate(physical system, simulation)
97 // Design Note
98 // Physical algorithm contains data and information to simulate at each time
99 // step the St-Venant equations system (wave profile) wawe propagation.
100 // See advance method below for what it should be
101 //
102 // Physical algorithm contains information or components to compute RHS
103 // actually it contains the numerical representation of the math equations??
104 // is that make sense?? neded to think about that!!!
105 w_phyAlgo->calculate(w_phySystem, /*SweRhsAlgorithm* aRhsAlgo,*/Sfx::Simulation::getSingleton()); // does one time step??
106 m_rhsAlgo->calculate(w_phySystem->getStateVector(), aGblDiscr); //???
107
108 // take measure on physical system such as energy, velocity, ...
109 // we measure wet area and discharge of the cross-section
111
112 // save measure to data store to eventually do some manip
113 // try-catch file open exception (take a look at Swe proto version)
114 // create a final report (simulation result)
115 w_fileDatStore.save( &w_phyMeasure, Sfx::Simulation::getSingleton());
116
117 std::vector<double> w_A; w_A.reserve(w_phySystem->getStateVariables().size());
118 std::vector<double> w_Q; w_Q.reserve(w_phySystem->getStateVariables().size());
119
120 namespace vws = std::views; // ranges library
121 for( const auto& w_pairVar : w_phySystem->getStateVariables() | vws::values)
122 {
123 // should I use std::move? or std::forward?
124 w_A.push_back(w_pairVar.first.value());
125 w_Q.push_back(w_pairVar.second.value());
126 }
127
128 auto w_CFL = Sfx::SimulationMgr::getSingleton().getCFL();
129
130 auto w_dt = w_CFL * TimeStepCriteria::timeStep(w_A, w_Q);
131
132 // set simulation time step
133 Sfx::Simulation::getSingleton().setSimulationTimeStep(w_dt);
134 aTimePrm.increaseTime(w_dt); // t1=t0+dt;
135
136 std::cout << "Finished time step at iteration " <<
137 Sfx::SimulationMgr::getSingleton().getNbIterations() + 1 << "\n";
138
139 // increase iteration by 1 (should be done in the Simulato::run())
140 Sfx::SimulationMgr::getSingleton().incrIteration();
141
142 // create a report for end result (yes)
143 //Final report (create the final report)
144 std::cout << "End of an iteration\n";
145 }//while-loop
146
147 // close data store
148 w_fileDatStore.close();
149
150 if (w_phySystem)
151 {
152 delete w_phySystem;
153 w_phySystem = nullptr;
154 }
155 }
156
157 // void setCalculFFAlgo(CalculFFAlgo aOldAlgo) { m_oldAlgo = aOldAlgo; }
158 // returns a string representation of the class
159 // std::string toString() override final { return std::string{ "EMcNeil1d_f" }; }
160
162 void setDebugFileON(bool aFileOn) { m_dbgFile = aFileOn; }
163 bool isDebugFileON() const { return m_dbgFile; }
164 protected:
166 // Want to re-implement
167 void advance( const dbpp::PhysicalSystem* aPhysys,
168 const dbpp::FiniteVolumeDiscretization* aGblDiscr, double aDt) //override final;
169 {
170#if _DEBUG
171 // Writing some debug results to file
172 Sfx::DbgLogger* w_dbgLog = Sfx::DbgLogger::instance();
173 auto chk = Sfx::Directory::getWrkDbgFile();
174 w_dbgLog->open(Sfx::Directory::getWrkDbgFile());
175 if (!w_dbgLog->isOpen())
176 {
177 std::cout << "Couldn't open file for writing debug info\n";
178 }
179#endif
180
181 // create an time stepper
182 dbpp::NujicIntegrator w_twoStepsIntegrator;
183 // NOTE use vectorField ctor to convert from StateVector
184 // auto statVec = aPhysys->getStateVector().first->values().to_stdVector();
185
186 w_twoStepsIntegrator.setInitialState(w_phySystem->getStateVector());
187
188 // just a check (2 impl of the SweRhsAlgorithm)
189 //auto w_sweRhsAlgo = aGblDiscr->numericalMethod()->getSweRhsAlgorithm();
190 //assert(nullptr != w_sweRhsAlgo);
191 //w_sweRhsAlgo->calculate(aPhysys->getStateVector(), aGblDiscr);
192
193 // call physical algorithm??? compute rhs??
194 w_phyAlgo->calculate(w_phySystem, Sfx::Simulation::getSingleton());
195
196 if (auto w_rhs = dynamic_cast<dbpp::TestRhsImpl*>(m_rhsAlgo); w_rhs != nullptr)
197 {
198 m_rhsAlgo->calculate(w_phySystem->getStateVector(), aGblDiscr);
199 }
200 else { // RhsPtr2FuncFlux (original algorithm wrapped (DamBreakSim::run())
201 // in the ... to be completed)
202 assert(nullptr != dynamic_cast<dbpp::RhsPtr2FuncFlux*>(m_rhsAlgo)); //sanity check
203 m_rhsAlgo->setBC(aGblDiscr->gamma());
204 m_rhsAlgo->calculate(aPhysys->getStateVector());
205 }
206 //m_rhsAlgo->calculate( aPhysys->getStateVector(), aGblDiscr); new algorithm using BaseRhsAlgorithm and
209 w_twoStepsIntegrator.step(m_rhsAlgo, aDt); // time-step algo
210 const auto w_halfStep = w_twoStepsIntegrator.getMidState(); // deprecated (debugging)
211
212#if _DEBUG // writing to debug for visualization
213 // write to log file for debugging purpose
214 w_dbgLog->write2file_p({ static_cast<unsigned>(w_halfStep.first->values().size()),
215 std::vector(std::begin(w_halfStep.first->values()), std::end(w_halfStep.first->values())),
216 std::vector(std::begin(w_halfStep.second->values()),std::end(w_halfStep.second->values())) });
217#endif
218
219 if (auto w_rhs = dynamic_cast<TestRhsImpl*>(m_rhsAlgo); w_rhs != nullptr)
220 {
221 m_rhsAlgo->calculate(w_twoStepsIntegrator.getMidState(), aGblDiscr);
222 }
223 else { // RhsPtr2FuncFlux (original algorithm wrapped (DamBreakSim::run())
224 // in the ... to be completed)
225 assert(nullptr != dynamic_cast<dbpp::RhsPtr2FuncFlux*>(m_rhsAlgo)); //sanity check
226 m_rhsAlgo->calculate(w_twoStepsIntegrator.getMidState());
227 }
228 //m_rhsAlgo->calculate( w_halfStep, aGblDiscr); new algorithm using BaseRhsAlgorithm and
229
230 w_twoStepsIntegrator.setIntegratorStep(NujicIntegrator::eIntegratorStep::corrector);
231 w_twoStepsIntegrator.step(m_rhsAlgo, aDt);
232 // i am not sure if a numerical method shall return a state
233 // for now will do but i have to think about that
234 m_U12 = w_twoStepsIntegrator.getCurrentState(); // update values
235
236#if _DEBUG
237 // write to log file for debugging purpose
238 w_dbgLog->write2file({ static_cast<unsigned>(m_U12.first->values().size()),
239 std::vector(std::begin(m_U12.first->values()),std::end(m_U12.first->values())),
240 std::vector(std::begin(m_U12.second->values()),std::end(m_U12.second->values())) });
241 // closing file debug info
242 if (w_dbgLog->isOpen())
243 {
244 w_dbgLog->close();
245 }
246#endif
247
248 }
249 private:
251 // PhysicalSystem* m_phySystem; /**< Physics System */
253 // StateVector m_U12p; /**< state vector for mid time step*/
255 // prototyping ne wimplementation of numrical method
256 dbpp::PhysicalSystem* w_phySystem{ nullptr }; // = new PhysicalSystem;
257 Sfx::PhysicalConfigure* w_phyConfig{ nullptr }; //physics configuration (set initial condition)
258 Sfx::PhysicalMeasurement* w_phyMeasure{ nullptr }; // physical quantities measured on the system
260 Sfx::FileDataStore w_fileDatStore; // save measure to the data store
261 };
262}// End of namespace
std::pair< fieldptr, fieldptr > StateVector
Definition SimulationConfig.h:27
Helper utility that save result to a file to be used for debugging and visualizing.
Definition Sfx_DbgLogger.h:19
bool isOpen()
check file is open
Definition Sfx_DbgLogger.h:43
void open(const std::string &aFilename="LoggerFile.txt")
open file for writing (file name as default)
Definition Sfx_DbgLogger.cpp:43
void close()
close file
Definition Sfx_DbgLogger.cpp:23
void write2file_p(const tuplevec &aTuple)
Write tuple of vector.
Definition Sfx_DbgLogger.cpp:99
void write2file(const std::string &aMsg="")=delete
write a message to file
Abstract class provide an interface with services to implement physical based algorithm to solve st-V...
Definition Sfx_PhysicalAlgorithm.h:20
Responsible to set initial configuration of the physical system (e.g. initial condition).
Definition Sfx_PhysicalConfigure.h:19
Physical measuremt taken on physical system (e.g. energy, friction, ...).
Definition Sfx_PhysicalMeasurement.h:14
void advance(const dbpp::PhysicalSystem *aPhysys, const dbpp::FiniteVolumeDiscretization *aGblDiscr, double aDt)
Definition Testvs19_Numethod_f.h:167
Sfx::PhysicalMeasurement * w_phyMeasure
Definition Testvs19_Numethod_f.h:258
void setDebugFileON(bool aFileOn)
Definition Testvs19_Numethod_f.h:162
Sfx::PhysicalAlgorithm * w_phyAlgo
Definition Testvs19_Numethod_f.h:259
void initialize(const std::shared_ptr< dbpp::FiniteVolumeDiscretization > &aGblDiscr) override final
Initialize.
Definition Testvs19_Numethod_f.h:36
Numethod_f(dbpp::SweRhsAlgorithm *aRhsAlgo)
Ctor rhs discretization algorithm.
Definition Testvs19_Numethod_f.h:25
dbpp::PhysicalSystem * w_phySystem
Definition Testvs19_Numethod_f.h:256
Sfx::PhysicalConfigure * w_phyConfig
Definition Testvs19_Numethod_f.h:257
StateVector m_U12
Definition Testvs19_Numethod_f.h:252
bool m_dbgFile
Definition Testvs19_Numethod_f.h:254
bool isDebugFileON() const
Definition Testvs19_Numethod_f.h:163
Sfx::FileDataStore w_fileDatStore
Definition Testvs19_Numethod_f.h:260
void mainLoop(const std::shared_ptr< dbpp::FiniteVolumeDiscretization > &aGblDiscr, Sfx::SfxTimePrm &aTimePrm) override final
Main loop (update nodal values)
Definition Testvs19_Numethod_f.h:55
dbpp::SweRhsAlgorithm * m_rhsAlgo
Definition Testvs19_Numethod_f.h:250
global discretized domain is defined as a set of all nodes and all elements. PHYSICAL ENTITIES includ...
Definition dbpp_FiniteVolumeDiscretization.h:26
Gamma & gamma() const
boundary condition (numeric)
Belongs two-step integrator Runge-Kutta family.
Definition dbpp_NujicIntegrator.h:24
void step(dbpp::SweRhsData< float64, std::valarray< float64 > > &&aRhs, float64 aDt)
Step algorithm.
Definition dbpp_NujicIntegrator.cpp:361
StateVector getMidState() const
Getter.
Definition dbpp_NujicIntegrator.h:131
eIntegratorStep
Enumeration integrator steps.
Definition dbpp_NujicIntegrator.h:37
@ predictor
Definition dbpp_NujicIntegrator.h:38
void setIntegratorStep(const eIntegratorStep aIntegratorStep)
Set integrator algorithm step.
Definition dbpp_NujicIntegrator.h:193
void setInitialState(StateVector aField)
Initial state.
Definition dbpp_NujicIntegrator.cpp:392
StateVector getCurrentState() const
Getter.
Definition dbpp_NujicIntegrator.h:122
Mapping between continuum and discrete domain. Transformation of the differential or integral equatio...
Definition dbpp_NumericalMethod.h:20
Physical system made of physical objects under study and described by the state variables....
Definition dbpp_PhysicalSystem.h:32
Sfx::StateVectorField getStateVector() const
physical system state vector (scalar field)
Definition dbpp_PhysicalSystem.cpp:80
Wrappper class to support legacy code (C-functions).
Definition dbpp_RhsPtr2FuncFlux.h:21
Abstract class. "Model" of the right-hand-side terms discretization. Hold discretization scheme or al...
Definition dbpp_SweRhsAlgorithm.h:28
Definition Testvs19_BaseRhsPhysicsAlgorithm.cpp:19