DPsim
DAESolver.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 <iostream>
12 #include <list>
13 #include <vector>
14 
15 #include <dpsim/Solver.h>
16 
17 #include <dpsim-models/Logger.h>
18 #include <dpsim-models/Solver/DAEInterface.h>
19 #include <dpsim-models/SystemTopology.h>
20 
21 #include <ida/ida.h>
22 #include <ida/ida_direct.h>
23 #include <nvector/nvector_serial.h>
24 #include <sundials/sundials_types.h>
25 #include <sunlinsol/sunlinsol_dense.h>
26 
27 namespace DPsim {
28 
30 class DAESolver : public Solver {
31 protected:
32  // General simulation parameters
33  CPS::SystemTopology mSystem;
35  std::vector<Int> mOffsets;
37  Real mTimestep;
39  Int mNEQ;
41  CPS::IdentifiedObject::List mComponents;
43  CPS::SimNode<Complex>::List mNodes;
44 
45  // Initial time t0
46  Real mT0;
47 
48  // IDA simulation variables
50  void *mem = NULL;
52  N_Vector state = NULL;
54  N_Vector dstate_dt = NULL;
56  realtype tret;
58  realtype abstol;
60  realtype rtol;
62  SUNMatrix A = NULL;
64  SUNLinearSolver LS = NULL;
65  long int interalSteps = 0;
66  long int resEval = 0;
67  std::vector<CPS::DAEInterface::ResFn> mResidualFunctions;
68 
70  static int residualFunctionWrapper(realtype ttime, N_Vector state,
71  N_Vector dstate_dt, N_Vector resid,
72  void *user_data);
73  int residualFunction(realtype ttime, N_Vector state, N_Vector dstate_dt,
74  N_Vector resid);
75 
76 public:
78  DAESolver(String name, const CPS::SystemTopology &system, Real dt, Real mT0);
80  ~DAESolver();
82  void initialize() final;
83 
85  Real step(Real time);
86 
87  CPS::Task::List getTasks() override;
88 };
89 } // namespace DPsim
Solver class which uses Differential Algebraic Equation(DAE) systems.
Definition: DAESolver.h:30
void * mem
Memory block allocated by IDA.
Definition: DAESolver.h:50
N_Vector state
Vector of problem variables.
Definition: DAESolver.h:52
N_Vector dstate_dt
Derivates of the state vector with respect to time.
Definition: DAESolver.h:54
SUNLinearSolver LS
Linear solver object.
Definition: DAESolver.h:64
Real step(Real time)
Solve system for the current time.
Definition: DAESolver.cpp:209
Int mNEQ
Number of equations in problem.
Definition: DAESolver.h:39
CPS::Task::List getTasks() override
Get tasks for scheduler.
Definition: DAESolver.cpp:229
std::vector< Int > mOffsets
Offsets vector for adding new equations to the residual vector.
Definition: DAESolver.h:35
Real mTimestep
Constant time step.
Definition: DAESolver.h:37
~DAESolver()
Deallocate all memory.
Definition: DAESolver.cpp:234
CPS::SimNode< Complex >::List mNodes
Nodes of the Problem.
Definition: DAESolver.h:43
DAESolver(String name, const CPS::SystemTopology &system, Real dt, Real mT0)
Create solve object with given parameters.
Definition: DAESolver.cpp:18
realtype rtol
Relative tolerance.
Definition: DAESolver.h:60
realtype abstol
Scalar absolute tolerance.
Definition: DAESolver.h:58
void initialize() final
Initialize Components & Nodes with initial values.
Definition: DAESolver.cpp:73
CPS::IdentifiedObject::List mComponents
Components of the Problem.
Definition: DAESolver.h:41
static int residualFunctionWrapper(realtype ttime, N_Vector state, N_Vector dstate_dt, N_Vector resid, void *user_data)
Residual Function of entire System.
Definition: DAESolver.cpp:173
realtype tret
Time IDA reached while solving.
Definition: DAESolver.h:56
SUNMatrix A
Template Jacobian Matrix.
Definition: DAESolver.h:62
Base class for more specific solvers such as MNA, ODE or IDA.
Definition: Solver.h:30