Cantera 2.6.0
ResidEval.h
Go to the documentation of this file.
1//! @file ResidEval.h
2
3// This file is part of Cantera. See License.txt in the top-level directory or
4// at https://cantera.org/license.txt for license and copyright information.
5
6#ifndef CT_RESIDEVAL_H
7#define CT_RESIDEVAL_H
8
12#include "cantera/base/global.h"
13
14namespace Cantera
15{
16
17const int c_NONE = 0;
18const int c_GE_ZERO = 1;
19const int c_GT_ZERO = 2;
20const int c_LE_ZERO = -1;
21const int c_LT_ZERO = -2;
22
23/**
24 * Virtual base class for DAE residual function evaluators.
25 * Classes derived from ResidEval evaluate the residual function
26 * \f[
27 * \vec{F}(t,\vec{y}, \vec{y^\prime})
28 * \f]
29 * The DAE solver attempts to find a solution y(t) such that F = 0.
30 * @ingroup DAE_Group
31 */
33{
34public:
35 ResidEval() {}
36 virtual ~ResidEval() {}
37
38 /**
39 * Constrain solution component k. Possible values for
40 * 'flag' are:
41 * - c_NONE no constraint
42 * - c_GE_ZERO >= 0
43 * - c_GT_ZERO > 0
44 * - c_LE_ZERO <= 0
45 * - c_LT_ZERO < 0
46 */
47 virtual void constrain(const int k, const int flag) {
48 m_constrain[k] = flag;
49 }
50 int constraint(const int k) const {
51 return getValue(m_constrain, k, c_NONE);
52 }
53
54 //! Initialization function
55 virtual void initSizes() {
56 int neq = nEquations();
57 m_alg.resize(neq, 0);
58 }
59
60 /**
61 * Specify that solution component k is purely algebraic - that is, the
62 * derivative of this component does not appear in the residual function.
63 */
64 virtual void setAlgebraic(const int k) {
65 if ((int) m_alg.size() < (k+1)) {
66 initSizes();
67 }
68 m_alg[k] = 1;
69 }
70
71 virtual bool isAlgebraic(const int k) {
72 return (m_alg[k] == 1);
73 }
74
75 /**
76 * Evaluate the residual function. Called by the integrator.
77 * @param t time. (input)
78 * @param y solution vector. (input)
79 * @param ydot rate of change of solution vector. (input)
80 * @param r residual vector (output)
81 */
82 virtual int eval(const doublereal t, const doublereal* const y,
83 const doublereal* const ydot,
84 doublereal* const r) {
85 throw NotImplementedError("ResidEval::eval");
86 }
87
88 virtual int evalSS(const doublereal t, const doublereal* const y,
89 doublereal* const r) {
90 return eval(t, y, 0, r);
91 }
92
93 virtual int evalSimpleTD(const doublereal t, const doublereal* const y,
94 const doublereal* const yold, doublereal deltaT,
95 doublereal* const r) {
96 int nn = nEquations();
97 vector_fp ydot(nn);
98 for (int i = 0; i < nn; i++) {
99 ydot[i] = (y[i] - yold[i]) / deltaT;
100 }
101 return eval(t, y, ydot.data(), r);
102 }
103
104 //! Fill in the initial conditions
105 /*!
106 * Values for both the solution and the value of ydot may be provided.
107 *
108 * @param[in] t0 Time
109 * @param[out] y Solution vector
110 * @param[out] ydot Rate of change of solution vector.
111 *
112 * @returns a flag to indicate that operation is successful.
113 * 1 Means a successful operation
114 * -0 or neg value Means an unsuccessful operation
115 */
116 virtual int getInitialConditions(const doublereal t0, doublereal* const y,
117 doublereal* const ydot) {
118 initSizes();
119 throw NotImplementedError("ResidEval::GetInitialConditions");
120 return 1;
121 }
122
123 //! Return the number of equations in the equation system
124 virtual int nEquations() const = 0;
125
126 //! Write out to a file or to standard output the current solution
127 /*!
128 * ievent is a description of the event that caused this function to be
129 * called.
130 */
131 virtual void writeSolution(int ievent, const double time,
132 const double deltaT,
133 const int time_step_num,
134 const double* y, const double* ydot) {
135 int k;
136 writelog("ResidEval::writeSolution\n");
137 writelogf(" Time = %g, ievent = %d, deltaT = %g\n", time, ievent, deltaT);
138 if (ydot) {
139 writelogf(" k y[] ydot[]\n");
140 for (k = 0; k < nEquations(); k++) {
141 writelogf("%d %g %g\n", k, y[k], ydot[k]);
142 }
143 } else {
144 writelogf(" k y[]\n");
145 for (k = 0; k < nEquations(); k++) {
146 writelogf("%d %g \n", k, y[k]);
147 }
148 }
149 }
150
151 //! Return the number of parameters in the calculation
152 /*!
153 * This is the number of parameters in the sensitivity calculation. We have
154 * set this to zero and have included it for later expansion
155 */
156 int nparams() const {
157 return 0;
158 }
159
160protected:
161 //! Mapping vector that stores whether a degree of freedom is a DAE or not
162 /*!
163 * The first index is the equation number. The second index is 1 if it is a
164 * DAE, and zero if it is not.
165 */
167 std::map<int, int> m_constrain;
168};
169
170}
171
172#endif
An error indicating that an unimplemented function has been called.
Definition: ctexceptions.h:187
Virtual base class for DAE residual function evaluators.
Definition: ResidEval.h:33
virtual int eval(const doublereal t, const doublereal *const y, const doublereal *const ydot, doublereal *const r)
Evaluate the residual function.
Definition: ResidEval.h:82
virtual void writeSolution(int ievent, const double time, const double deltaT, const int time_step_num, const double *y, const double *ydot)
Write out to a file or to standard output the current solution.
Definition: ResidEval.h:131
virtual int getInitialConditions(const doublereal t0, doublereal *const y, doublereal *const ydot)
Fill in the initial conditions.
Definition: ResidEval.h:116
virtual void setAlgebraic(const int k)
Specify that solution component k is purely algebraic - that is, the derivative of this component doe...
Definition: ResidEval.h:64
virtual int nEquations() const =0
Return the number of equations in the equation system.
int nparams() const
Return the number of parameters in the calculation.
Definition: ResidEval.h:156
vector_int m_alg
Mapping vector that stores whether a degree of freedom is a DAE or not.
Definition: ResidEval.h:166
virtual void constrain(const int k, const int flag)
Constrain solution component k.
Definition: ResidEval.h:47
virtual void initSizes()
Initialization function.
Definition: ResidEval.h:55
This file contains definitions of constants, types and terms that are used in internal routines and a...
Definitions for the classes that are thrown when Cantera experiences an error condition (also contain...
This file contains definitions for utility functions and text for modules, inputfiles,...
void writelog(const std::string &fmt, const Args &... args)
Write a formatted message to the screen.
Definition: global.h:164
void writelogf(const char *fmt, const Args &... args)
Write a formatted message to the screen.
Definition: global.h:185
Namespace for the Cantera kernel.
Definition: AnyMap.h:29
std::vector< int > vector_int
Vector of ints.
Definition: ct_defs.h:186
std::vector< double > vector_fp
Turn on the use of stl vectors for the basic array type within cantera Vector of doubles.
Definition: ct_defs.h:184
const U & getValue(const std::map< T, U > &m, const T &key, const U &default_val)
Const accessor for a value in a std::map.
Definition: utilities.h:184
Various templated functions that carry out common vector operations (see Templated Utility Functions)...