All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ODESystem.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "ODESystem.H"
27 
28 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
29 
31 {}
32 
33 
34 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
35 
37 {}
38 
39 
40 // * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
41 
43 (
44  const scalar x,
45  const scalarField& y,
46  const scalarField& dy,
47  const label li
48 ) const
49 {
50  // Evaluate the derivatives using the derivatives method
51  scalarField dfdx0(nEqns());
52  derivatives(x, y, li, dfdx0);
53 
54  // Evaluate the derivatives and the Jacobian using the Jacobian method
55  scalarField dfdx1(nEqns());
56  scalarSquareMatrix d2fdxdyAnalytic(nEqns());
57  jacobian(x, y, li, dfdx1, d2fdxdyAnalytic);
58 
59  // Compare derivatives
60  Info<< "[derivatives] dfdx = " << dfdx0 << nl;
61  Info<< "[ jacobian] dfdx = " << dfdx1 << nl;
62 
63  // Construct a Jacobian using the finite differences and the derivatives
64  // method
65  scalarSquareMatrix d2fdxdyFiniteDifference(nEqns());
66  scalarField y0(y), y1(y);
67  for (label i = 0; i < nEqns(); ++ i)
68  {
69  y0[i] = y[i] - dy[i];
70  y1[i] = y[i] + dy[i];
71 
72  derivatives(x, y0, li, dfdx0);
73  derivatives(x, y1, li, dfdx1);
74 
75  for (label j = 0; j < nEqns(); j++)
76  {
77  d2fdxdyFiniteDifference(j, i) = (dfdx1[j] - dfdx0[j])/(2*dy[i]);
78  }
79 
80  y0[i] = y[i];
81  y1[i] = y[i];
82  }
83 
84  for (label i = 0; i < nEqns(); ++ i)
85  {
86  Info<< "[derivatives] d2fdxdy[" << i << "] = "
87  << UList<scalar>(d2fdxdyFiniteDifference[i], nEqns()) << nl;
88  Info<< "[ jacobian] d2fdxdy[" << i << "] = "
89  << UList<scalar>(d2fdxdyAnalytic[i], nEqns()) << nl;
90  }
91 }
92 
93 
94 // ************************************************************************* //
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
virtual void derivatives(const scalar x, const scalarField &y, const label li, scalarField &dydx) const =0
Calculate the derivatives in dydx.
void check(const scalar x, const scalarField &y, const scalarField &dy, const label li) const
Print correspondence between derivatives and Jacobian methods.
Definition: ODESystem.C:43
dimensionedScalar y0(const dimensionedScalar &ds)
virtual ~ODESystem()
Destructor.
Definition: ODESystem.C:36
dimensionedScalar y1(const dimensionedScalar &ds)
static const char nl
Definition: Ostream.H:260
ODESystem()
Construct null.
Definition: ODESystem.C:30
virtual void jacobian(const scalar x, const scalarField &y, const label li, scalarField &dfdx, scalarSquareMatrix &dfdy) const =0
Calculate the Jacobian of the system.
messageStream Info
virtual label nEqns() const =0
Return the number of equations in the system.