CodedFunction1.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-2025 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 "CodedFunction1.H"
27 #include "dynamicCode.H"
28 #include "dynamicCodeContext.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
32 template<class Type>
34 (
35  {"code", "codeInclude"}
36 );
37 
38 template<class Type>
40 (
41  {word(), word()}
42 );
43 
44 
45 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
46 
47 template<class Type>
49 (
50  dynamicCode& dynCode,
51  const dynamicCodeContext& context
52 ) const
53 {
54  dynCode.setFilterVariable("typeName", codeName());
55 
56  // Set TemplateType filter variables
57  dynCode.setFilterVariable("TemplateType", pTraits<Type>::typeName);
58 
59  // Compile filtered C template
60  dynCode.addCompileFile(codeTemplateC("codedFunction1"));
61 
62  // Copy filtered H template
63  dynCode.addCopyFile(codeTemplateH("codedFunction1"));
64 
65  // Make verbose if debugging
66  dynCode.setFilterVariable("verbose", Foam::name(bool(debug)));
67 
68  if (debug)
69  {
70  Info<<"compile " << codeName() << " sha1: " << context.sha1() << endl;
71  }
72 
73  // Define Make/options
74  dynCode.setMakeOptions
75  (
76  "EXE_INC = -g \\\n"
77  + context.options()
78  + "\n\nLIB_LIBS = \\\n"
79  + " -lOpenFOAM \\\n"
80  + context.libs()
81  );
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
86 
87 template<class Type>
89 (
90  const word& name,
92  const dictionary& dict
93 )
94 :
95  Function1<Type>(name),
96  codedBase(dict, codeKeys, codeDictVars),
97  units_(units)
98 {
99  this->updateLibrary(dict);
100 
101  dictionary redirectDict(dict);
102  redirectDict.set(codeName(), codeName());
103 
104  redirectFunction1Ptr_ =
105  Function1<Type>::New(codeName(), unitAny, unitAny, redirectDict);
106 }
107 
108 
109 template<class Type>
111 :
112  Function1<Type>(cf1),
113  codedBase(cf1),
114  units_(cf1.units_),
115  redirectFunction1Ptr_(cf1.redirectFunction1Ptr_, false)
116 {}
117 
118 
119 template<class Type>
121 {
122  return tmp<Function1<Type>>(new Coded<Type>(*this));
123 }
124 
125 
126 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
127 
128 template<class Type>
130 {}
131 
132 
133 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
134 
135 template<class Type>
137 (
138  const scalarField& x
139 ) const
140 {
141  return
142  units_.value.toStandard
143  (
144  redirectFunction1Ptr_->value
145  (
146  units_.x.toUser(x)
147  )
148  );
149 }
150 
151 
152 template<class Type>
154 (
155  const scalar x1,
156  const scalar x2
157 ) const
158 {
160  return pTraits<Type>::zero;
161 }
162 
163 
164 template<class Type>
166 (
167  const scalarField& x1,
168  const scalarField& x2
169 ) const
170 {
172  return tmp<Field<Type>>();
173 }
174 
175 
176 template<class Type>
178 (
179  Ostream& os,
180  const unitConversions& units
181 ) const
182 {
183  codedBase::write(os);
184 }
185 
186 
187 // ************************************************************************* //
Run-time selectable general function of one variable.
Definition: Function1.H:125
static autoPtr< Function1< Type > > New(const word &name, const Function1s::unitConversions &units, const dictionary &dict)
Select from dictionary.
Definition: Function1New.C:32
Constructs a dynamically compiled Function1.
virtual Type integral(const scalar x1, const scalar x2) const
Integrate between two scalars.
virtual void write(Ostream &os, const unitConversions &units) const
Write data to dictionary stream.
virtual ~Coded()
Destructor.
virtual Type value(const scalar x) const
Return value as a function of a scalar.
virtual tmp< Function1< Type > > clone() const
Construct and return a clone.
Coded(const word &name, const Function1s::unitConversions &units, const dictionary &dict)
Construct from name and dictionary.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
Base class for function objects and boundary conditions using dynamic code.
Definition: codedBase.H:52
const word & codeName() const
Name of the dynamically generated CodedType.
Definition: codedBase.C:374
void write(Ostream &os) const
Write the code for restart.
Definition: codedBase.C:461
bool updateLibrary(const dictionary &dict) const
Update library from given updated dictionary as required.
Definition: codedBase.C:398
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
Traits class for primitives.
Definition: pTraits.H:53
A class for managing temporary objects.
Definition: tmp.H:55
A class for handling words, derived from string.
Definition: word.H:62
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:381
const unitConversion unitAny
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
messageStream Info
const HashTable< unitConversion > & units()
Get the table of unit conversions.
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
dictionary dict