CodedFunction2.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) 2020-2024 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 "CodedFunction2.H"
27 #include "dynamicCode.H"
28 #include "dynamicCodeContext.H"
29 
30 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
31 
32 template<class Type>
34 {
35  return {"code", "codeInclude"};
36 }
37 
38 
39 template<class Type>
41 {
42  return {word::null, word::null};
43 }
44 
45 
46 template<class Type>
48 (
49  dynamicCode& dynCode,
50  const dynamicCodeContext& context
51 ) const
52 {
53  dynCode.setFilterVariable("typeName", codeName());
54 
55  // Set TemplateType filter variables
56  dynCode.setFilterVariable("TemplateType", pTraits<Type>::typeName);
57 
58  // Compile filtered C template
59  dynCode.addCompileFile(codeTemplateC("codedFunction2"));
60 
61  // Copy filtered H template
62  dynCode.addCopyFile(codeTemplateH("codedFunction2"));
63 
64  // Make verbose if debugging
65  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
66 
67  if (debug)
68  {
69  Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl;
70  }
71 
72  // Define Make/options
73  dynCode.setMakeOptions
74  (
75  "EXE_INC = -g \\\n"
76  + context.options()
77  + "\n\nLIB_LIBS = \\\n"
78  + " -lOpenFOAM \\\n"
79  + context.libs()
80  );
81 }
82 
83 
84 template<class Type>
86 {
87  // Remove instantiation of Function2 provided by library
88  redirectFunction2Ptr_.clear();
89 }
90 
91 
92 template<class Type>
95 {
96  this->updateLibrary();
97 
98  dictionary redirectDict(codeDict());
99  redirectDict.set(codeName(), codeName());
100 
101  return Function2<Type>::New(codeName(), this->units_, redirectDict);
102 }
103 
104 
105 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
106 
107 template<class Type>
109 (
110  const word& name,
111  const unitConversions& units,
112  const dictionary& dict
113 )
114 :
115  Function2<Type>(name),
116  codedBase(name, dict),
117  units_(units)
118 {
119  redirectFunction2Ptr_ = compileNew();
120 }
121 
122 
123 
124 template<class Type>
126 :
127  Function2<Type>(cf2),
128  codedBase(cf2),
129  units_(cf2.units_)
130 {
131  redirectFunction2Ptr_ = compileNew();
132 }
133 
134 
135 template<class Type>
137 {
138  return tmp<Function2<Type>>(new Coded<Type>(*this));
139 }
140 
141 
142 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
143 
144 template<class Type>
146 {}
147 
148 
149 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
150 
151 template<class Type>
153 (
154  const scalarField& x,
155  const scalarField& y
156 ) const
157 {
158  return
159  units_.value.toStandard
160  (
161  redirectFunction2Ptr_->value
162  (
163  units_.x.toUser(x),
164  units_.y.toUser(y)
165  )
166  );
167 }
168 
169 
170 template<class Type>
172 (
173  Ostream& os,
174  const unitConversions& units
175 ) const
176 {
177  writeCode(os);
178 }
179 
180 
181 // ************************************************************************* //
scalar y
Run-time selectable function of two variables.
Definition: Function2.H:98
Constructs a dynamically compiled function of two variables.
virtual tmp< Function2< Type > > clone() const
Construct and return a clone.
virtual void write(Ostream &os, const unitConversions &units) const
Write data to dictionary stream.
Coded(const word &name, const unitConversions &units, const dictionary &dict)
Construct from name and dictionary.
virtual ~Coded()
Destructor.
virtual Type value(const scalar x, const scalar y) const
Return value as a function of two scalars.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
An auto-pointer similar to the STL auto_ptr but with automatic casting to a reference to the type and...
Definition: autoPtr.H:51
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:54
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:162
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
autoPtr< CompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const viscosity &viscosity)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:257
word name(const bool)
Return a word representation of a bool.
Definition: boolIO.C:39
messageStream Info
const HashTable< unitConversion > & units()
Get the table of unit conversions.
dictionary dict