DPsim
ODESolver.h
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 #pragma once
10 
11 #include <dpsim/Solver.h>
12 
13 #include <dpsim-models/Solver/ODEInterface.h>
14 
15 #include <arkode/arkode.h> // prototypes for ARKode fcts., consts. and includes sundials_types.h
16 #include <arkode/arkode_direct.h> // access to ARKDls interface
17 #include <nvector/nvector_serial.h> // access to serial N_Vector
18 #include <sunlinsol/sunlinsol_dense.h> // access to dense SUNLinearSolver
19 #include <sunmatrix/sunmatrix_dense.h> // access to dense SUNMatrix
20 
21 //using namespace CPS; // led to problems
22 
23 namespace DPsim {
25 class ODESolver : public Solver {
26 protected:
28  CPS::ODEInterface::Ptr mComponent;
29 
31  Int mProbDim;
32 
33  // ###ARKode-specific variables ###
35  void *mArkode_mem{nullptr};
37  N_Vector mStates{nullptr};
38 
39  //for implicit solve:
43  SUNMatrix A{nullptr};
45  SUNLinearSolver LS{nullptr};
46 
48  Real mTimestep;
49 
50  // Same tolerance for each component regardless of system characteristics
52  realtype reltol = RCONST(1.0e-6);
54  realtype abstol = RCONST(1.0e-10);
55 
56  // TODO: Variables for implicit solve?
58  /* SUNMatrix A = NULL;
60  SUNLinearSolver LS = NULL; */
61 
63  int mFlag{0};
64 
65  // Similar to DAE-Solver
66  CPS::ODEInterface::StSpFn mStSpFunction;
67  CPS::ODEInterface::JacFn mJacFunction;
68 
70  static int StateSpaceWrapper(realtype t, N_Vector y, N_Vector ydot,
71  void *user_data);
72  int StateSpace(realtype t, N_Vector y, N_Vector ydot);
73 
74  static int JacobianWrapper(realtype t, N_Vector y, N_Vector fy, SUNMatrix J,
75  void *user_data, N_Vector tmp1, N_Vector tmp2,
76  N_Vector tmp3);
77  int Jacobian(realtype t, N_Vector y, N_Vector fy, SUNMatrix J, N_Vector tmp1,
78  N_Vector tmp2, N_Vector tmp3);
80  int check_flag(void *flagvalue, const std::string &funcname, int opt);
81 
82 public:
84  ODESolver(String name, const CPS::ODEInterface::Ptr &comp,
85  bool implicit_integration, Real timestep);
87  ~ODESolver();
88 
89  class SolveTask : public CPS::Task {
90  public:
91  SolveTask(ODESolver &solver)
92  : Task(solver.mName + ".Solve"), mSolver(solver) {
93  mAttributeDependencies.push_back(solver.mComponent->mOdePreState);
94  mModifiedAttributes.push_back(solver.mComponent->mOdePostState);
95  }
96 
97  void execute(Real time, Int timeStepCount);
98 
99  private:
100  ODESolver &mSolver;
101  };
102 
103  virtual CPS::Task::List getTasks() {
104  return CPS::Task::List{std::make_shared<SolveTask>(*this)};
105  }
106 
108  void initialize();
110  Real step(Real initial_time);
111 };
112 } // namespace DPsim
std::function< void(double, const double *, double *)> StSpFn
Use this to pass the individual components StateSpace implementation to the ODESolver class.
Definition: ODEInterface.h:27
Tasks to be defined by every component.
Definition: Task.h:25
Solver class for ODE (Ordinary Differential Equation) systems.
Definition: ODESolver.h:25
ODESolver(String name, const CPS::ODEInterface::Ptr &comp, bool implicit_integration, Real timestep)
Create solve object with corresponding component and information on the integration type.
Definition: ODESolver.cpp:14
virtual CPS::Task::List getTasks()
Get tasks for scheduler.
Definition: ODESolver.h:103
CPS::ODEInterface::Ptr mComponent
Component to simulate, possible specialized component needed.
Definition: ODESolver.h:28
Real step(Real initial_time)
Solve system for the current time.
Definition: ODESolver.cpp:115
void * mArkode_mem
Memory block allocated by ARKode.
Definition: ODESolver.h:35
int mFlag
Template Jacobian Matrix (implicit solver)
Definition: ODESolver.h:63
realtype abstol
Scalar absolute tolerance.
Definition: ODESolver.h:54
SUNLinearSolver LS
Empty linear solver object.
Definition: ODESolver.h:45
int check_flag(void *flagvalue, const std::string &funcname, int opt)
ARKode- standard error detection function; in DAE-solver not detection function is used -> for effici...
Definition: ODESolver.cpp:212
N_Vector mStates
State vector.
Definition: ODESolver.h:37
void initialize()
Initialize ARKode-solve_environment.
Definition: ODESolver.cpp:22
bool mImplicitIntegration
Indicates whether the ODE shall be solved using an implicit scheme.
Definition: ODESolver.h:41
~ODESolver()
Deallocate all memory.
Definition: ODESolver.cpp:243
Real mTimestep
Constant time step.
Definition: ODESolver.h:48
SUNMatrix A
Empty matrix for linear solve in each Newton step while solving the Jacobian Matrix.
Definition: ODESolver.h:43
Int mProbDim
Number of differential Variables (states)
Definition: ODESolver.h:31
realtype reltol
Relative tolerance.
Definition: ODESolver.h:52
static int StateSpaceWrapper(realtype t, N_Vector y, N_Vector ydot, void *user_data)
use wrappers similar to DAE_Solver
Definition: ODESolver.cpp:90
Base class for more specific solvers such as MNA, ODE or IDA.
Definition: Solver.h:30
String mName
Name for logging.
Definition: Solver.h:39