DPsim
Loading...
Searching...
No Matches
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
23namespace DPsim {
25class ODESolver : public Solver {
26protected:
28 CPS::ODEInterface::Ptr mComponent;
29
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
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
57 int mFlag{0};
58
59 // Similar to DAE-Solver
60 CPS::ODEInterface::StSpFn mStSpFunction;
61 CPS::ODEInterface::JacFn mJacFunction;
62
64 static int StateSpaceWrapper(realtype t, N_Vector y, N_Vector ydot,
65 void *user_data);
66 int StateSpace(realtype t, N_Vector y, N_Vector ydot);
67
68 static int JacobianWrapper(realtype t, N_Vector y, N_Vector fy, SUNMatrix J,
69 void *user_data, N_Vector tmp1, N_Vector tmp2,
70 N_Vector tmp3);
71 int Jacobian(realtype t, N_Vector y, N_Vector fy, SUNMatrix J, N_Vector tmp1,
72 N_Vector tmp2, N_Vector tmp3);
74 int check_flag(void *flagvalue, const std::string &funcname, int opt);
75
76public:
78 ODESolver(String name, const CPS::ODEInterface::Ptr &comp,
79 bool implicit_integration, Real timestep);
81 ~ODESolver();
82
83 class SolveTask : public CPS::Task {
84 public:
85 SolveTask(ODESolver &solver)
86 : Task(solver.mName + ".Solve"), mSolver(solver) {
87 mAttributeDependencies.push_back(solver.mComponent->mOdePreState);
88 mModifiedAttributes.push_back(solver.mComponent->mOdePostState);
89 }
90
91 void execute(Real time, Int timeStepCount);
92
93 private:
94 ODESolver &mSolver;
95 };
96
97 virtual CPS::Task::List getTasks() {
98 return CPS::Task::List{std::make_shared<SolveTask>(*this)};
99 }
100
102 void initialize();
104 Real step(Real initial_time);
105};
106} // namespace DPsim
std::function< void(double, const double *, double *)> StSpFn
Use this to pass the individual components StateSpace implementation to the ODESolver class.
Tasks to be defined by every component.
Definition Task.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:97
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:67
void * mArkode_mem
Memory block allocated by ARKode.
Definition ODESolver.h:35
int mFlag
Reusable error-checking flag.
Definition ODESolver.h:57
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...
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.
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:42
String mName
Name for logging.
Definition Solver.h:39