1 /* Siconos is a program dedicated to modeling, simulation and control
2 * of non smooth dynamical systems.
3 *
4 * Copyright 2021 INRIA.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #include <chrono>
20 #include "TimeStepping.hpp"
21 #include "ZeroOrderHoldOSI.hpp"
22 #include "EventsManager.hpp"
23 #include "Event.hpp"
24 #include "NonSmoothDynamicalSystem.hpp"
25 #include "SimulationGraphs.hpp"
26
27 #include "ControlManager.hpp"
28 #include "ControlZOHAdditionalTerms.hpp"
29 #include "ControlZOHSimulation.hpp"
30 #include "ControlSimulation_impl.hpp"
31
32 //#define DEBUG_BEGIN_END_ONLY
33 //#define DEBUG_NOCOLOR
34 //#define DEBUG_STDOUT
35 //#define DEBUG_MESSAGES
36 #include "siconos_debug.h"
37
38
ControlZOHSimulation(double t0,double T,double h)39 ControlZOHSimulation::ControlZOHSimulation(double t0, double T, double h):
40 ControlSimulation(t0, T, h)
41 {
42 _processIntegrator.reset(new ZeroOrderHoldOSI());
43 std::static_pointer_cast<ZeroOrderHoldOSI>(_processIntegrator)->setExtraAdditionalTerms(
44 std::shared_ptr<ControlZOHAdditionalTerms>(new ControlZOHAdditionalTerms()));
45 _processSimulation.reset(new TimeStepping(_nsds,_processTD, 0));
46 _processSimulation->setName("plant simulation");
47 _processSimulation->insertIntegrator(_processIntegrator);
48
49 _DSG0 = _nsds->topology()->dSG(0);
50 _IG0 = _nsds->topology()->indexSet0();
51
52 // Control part
53 _CM.reset(new ControlManager(_processSimulation));
54 }
55
run()56 void ControlZOHSimulation::run()
57 {
58 DEBUG_BEGIN("void ControlZOHSimulation::run()\n");
59 EventsManager& eventsManager = *_processSimulation->eventsManager();
60 unsigned k = 0;
61 std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
62
63 TimeStepping& sim = static_cast<TimeStepping&>(*_processSimulation);
64
65 while(sim.hasNextEvent())
66 {
67 Event& nextEvent = *eventsManager.nextEvent();
68 if(nextEvent.getType() == TD_EVENT)
69 {
70 sim.computeOneStep();
71 }
72
73 sim.nextStep();
74
75 if(sim.hasNextEvent() && eventsManager.nextEvent()->getType() == TD_EVENT) // We store only on TD_EVENT
76 {
77 (*_dataM)(k, 0) = sim.startingTime();
78 storeData(k);
79 ++k;
80 }
81 }
82
83 /* saves last status */
84 (*_dataM)(k, 0) = sim.startingTime();
85 storeData(k);
86 ++k;
87
88 std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
89 std::chrono::duration<double, std::milli> fp_s = end - start;
90 _elapsedTime = fp_s.count();
91
92 _dataM->resize(k, _nDim + 1);
93 DEBUG_END("void ControlZOHSimulation::run()\n");
94 }
95