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-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 "CodedFunction1.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("codedFunction1"));
60 
61  // Copy filtered H template
62  dynCode.addCopyFile(codeTemplateH("codedFunction1"));
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 Function1 provided by library
88  redirectFunction1Ptr_.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 Function1<Type>::New(codeName(), unitAny, unitAny, redirectDict);
102 }
103 
104 
105 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
106 
107 template<class Type>
109 (
110  const word& name,
112  const dictionary& dict
113 )
114 :
115  Function1<Type>(name),
116  codedBase(dict),
117  units_(units)
118 {
119  redirectFunction1Ptr_ = compileNew();
120 }
121 
122 
123 
124 template<class Type>
126 :
127  Function1<Type>(cf1),
128  codedBase(cf1),
129  units_(cf1.units_)
130 {
131  redirectFunction1Ptr_ = compileNew();
132 }
133 
134 
135 template<class Type>
137 {
138  return tmp<Function1<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
156 {
157  return
158  units_.value.toStandard
159  (
160  redirectFunction1Ptr_->value
161  (
162  units_.x.toUser(x)
163  )
164  );
165 }
166 
167 
168 template<class Type>
170 (
171  const scalar x1,
172  const scalar x2
173 ) const
174 {
176  return pTraits<Type>::zero;
177 }
178 
179 
180 template<class Type>
182 (
183  const scalarField& x1,
184  const scalarField& x2
185 ) const
186 {
188  return tmp<Field<Type>>();
189 }
190 
191 
192 template<class Type>
194 (
195  Ostream& os,
196  const unitConversions& units
197 ) const
198 {
199  writeCode(os);
200 }
201 
202 
203 // ************************************************************************* //
Run-time selectable general function of one variable.
Definition: Function1.H:125
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
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
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
autoPtr< CompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const viscosity &viscosity)
const unitConversion unitAny
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