DPsim
ODEintSolver.cpp
1 /* Copyright 2017-2021 Institute for Automation of Complex Power Systems,
2  * EONERC, RWTH Aachen University
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
7  *********************************************************************************/
8 
9 #include <dpsim/ODEintSolver.h>
10 
11 using namespace DPsim;
12 
13 ODEintSolver::ODEintSolver(String name, CPS::ODEintInterface::Ptr comp, Real dt,
14  Real t0)
15  : mComponent(comp), mTimestep(dt) {
16  times.push_back(t0);
17  self = this; // sets static pointer to current object
18  ProbDim = comp->num_states();
19 
20  curSolution.resize(ProbDim);
21 
22  //register all system functions
23  system.push_back([comp](const double y[], double ydot[], const double t) {
24  comp->odeint(y, ydot, t);
25  });
26 }
27 
28 Real ODEintSolver::step(Real time) {
29 
30  mComponent->pre_step();
31  curSolution.assign(mComponent->state_vector(),
32  mComponent->state_vector() + ProbDim);
33  Real NextTime = time + mTimestep;
34 
35  std::cout << "Current Time " << NextTime << std::endl;
36 
37  stepper.do_step(
38  &ODEintSolver::StateSpaceWrapper, curSolution, time,
39  mTimestep);
40  mComponent->set_state_vector(
41  curSolution);
42  solution.push_back(curSolution);
43  times.push_back(time);
44 
45  // TODO: add proper logging
46  mComponent->post_step();
47  return NextTime;
48 }
49 
50 void ODEintSolver::StateSpaceWrapper(const std::vector<double> &y,
51  std::vector<double> ydot, double t) {
52 
53  for (
54  auto comp :
55  self->system) { // call system functions of the components with the state vector
56  comp(y.data(), ydot.data(), t);
57  }
58 }
59 
std::vector< CPS::ODEintInterface::stateFnc > system
ODE of Component.
Definition: ODEintSolver.h:40
std::vector< Real > times
Vector containing all timesteps.
Definition: ODEintSolver.h:38
ODEintSolver(String name, CPS::ODEintInterface::Ptr comp, Real dt, Real t0)
Create solve object with given parameters.
boost::numeric::odeint::runge_kutta4< std::vector< Real > > stepper
Stepper needed by ODEint.
Definition: ODEintSolver.h:34
int ProbDim
Problem Size.
Definition: ODEintSolver.h:32
Real mTimestep
Constant time step.
Definition: ODEintSolver.h:30
Real step(Real time)
Solve system for the current time.
std::vector< std::vector< Real > > solution
Vector containing the solution at every timestep.
Definition: ODEintSolver.h:36
~ODEintSolver()
Deallocate all memory.
std::vector< Real > curSolution
Current solution vector.
Definition: ODEintSolver.h:51
CPS::ODEintInterface::Ptr mComponent
Pointer to current Component.
Definition: ODEintSolver.h:28