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 
21 #include "EventDriven.hpp"
22 #include "LsodarOSI.hpp"
23 #include "EventsManager.hpp"
24 #include "Event.hpp"
25 #include "NonSmoothDynamicalSystem.hpp"
26 #include "SimulationGraphs.hpp"
27 
28 #include "ControlLinearAdditionalTermsED.hpp"
29 #include "ControlManager.hpp"
30 #include "ControlLsodarSimulation.hpp"
31 #include "ControlSimulation_impl.hpp"
32 #include "Observer.hpp"
33 #include "Actuator.hpp"
34 
35 
ControlLsodarSimulation(double t0,double T,double h)36 ControlLsodarSimulation::ControlLsodarSimulation(double t0, double T, double h):
37   ControlSimulation(t0, T, h)
38 {
39   _processIntegrator.reset(new LsodarOSI());
40   _processSimulation.reset(new EventDriven(_nsds, _processTD, 0));
41   _processSimulation->setName("plant simulation");
42   _processSimulation->insertIntegrator(_processIntegrator);
43   std::static_pointer_cast<LsodarOSI>(_processIntegrator)->setExtraAdditionalTerms(
44     std::shared_ptr<ControlLinearAdditionalTermsED>(new ControlLinearAdditionalTermsED()));
45 
46   _DSG0 = _nsds->topology()->dSG(0);
47   _IG0 = _nsds->topology()->indexSet0();
48 
49   // Control part
50   _CM.reset(new ControlManager(_processSimulation));
51 }
52 
run()53 void ControlLsodarSimulation::run()
54 {
55   EventsManager& eventsManager = *_processSimulation->eventsManager();
56   unsigned k = 0;
57   std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
58   EventDriven& sim = static_cast<EventDriven&>(*_processSimulation);
59 
60   while(sim.hasNextEvent())
61   {
62     if(eventsManager.needsIntegration())
63     {
64       sim.advanceToEvent();
65     }
66     sim.processEvents();
67     Event& currentEvent = *eventsManager.currentEvent();
68     if(currentEvent.getType() == ACTUATOR_EVENT)
69     {
70       // this is necessary since we changed the control input, hence the RHS
71       sim.setIstate(1);
72     }
73     if(sim.hasNextEvent() && eventsManager.nextEvent()->getType() == TD_EVENT)  // We store only on TD_EVENT, this should be settable
74     {
75       (*_dataM)(k, 0) = sim.startingTime();
76       storeData(k);
77       ++k;
78     }
79   }
80 
81   /* saves last status */
82   (*_dataM)(k, 0) = sim.startingTime();
83   storeData(k);
84   ++k;
85 
86   std::chrono::system_clock::time_point end = std::chrono::system_clock::now();
87   std::chrono::duration<double, std::milli> fp_s = end - start;
88   _elapsedTime = fp_s.count();
89   _dataM->resize(k, _nDim + 1);
90 }
91