CodedSource.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) 2012-2019 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 "CodedSource.H"
27 #include "fvMesh.H"
28 #include "fvMatrices.H"
29 #include "dynamicCode.H"
30 #include "dynamicCodeContext.H"
31 
32 // * * * * * * * * * * * Protected Static Data Members * * * * * * * * * * * //
33 
34 template<class Type>
35 const Foam::wordList Foam::fv::CodedSource<Type>::codeKeys_ =
36  {"codeAddSup", "codeCorrect", "codeInclude", "codeSetValue", "localCode"};
37 
38 
39 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
40 
41 template<class Type>
43 (
44  dynamicCode& dynCode,
45  const dynamicCodeContext& context
46 ) const
47 {
48  word sourceType(pTraits<Type>::typeName);
49 
50  // Set additional rewrite rules
51  dynCode.setFilterVariable("typeName", name_);
52  dynCode.setFilterVariable("TemplateType", sourceType);
53  dynCode.setFilterVariable("SourceType", sourceType + "Source");
54 
55  // compile filtered C template
56  dynCode.addCompileFile("codedFvOptionTemplate.C");
57 
58  // copy filtered H template
59  dynCode.addCopyFile("codedFvOptionTemplate.H");
60 
61  // debugging: make BC verbose
62  // dynCode.setFilterVariable("verbose", "true");
63  // Info<<"compile " << name_ << " sha1: "
64  // << context.sha1() << endl;
65 
66  // define Make/options
67  dynCode.setMakeOptions
68  (
69  "EXE_INC = -g \\\n"
70  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
71  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
72  "-I$(LIB_SRC)/sampling/lnInclude \\\n"
73  "-I$(LIB_SRC)/fvOptions/lnInclude \\\n"
74  + context.options()
75  + "\n\nLIB_LIBS = \\\n"
76  + " -lmeshTools \\\n"
77  + " -lfvOptions \\\n"
78  + " -lsampling \\\n"
79  + " -lfiniteVolume \\\n"
80  + context.libs()
81  );
82 }
83 
84 
85 template<class Type>
87 {
88  return const_cast<Time&>(mesh_.time()).libs();
89 }
90 
91 
92 template<class Type>
94 {
95  return "fvOption:: " + name_;
96 }
97 
98 
99 template<class Type>
101 {
102  redirectFvOptionPtr_.clear();
103 }
104 
105 
106 template<class Type>
108 {
109  return coeffs_;
110 }
111 
112 
113 template<class Type>
115 {
116  return codeKeys_;
117 }
118 
119 
120 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
121 
122 template<class Type>
124 (
125  const word& name,
126  const word& modelType,
127  const dictionary& dict,
128  const fvMesh& mesh
129 )
130 :
131  cellSetOption(name, modelType, dict, mesh)
132 {
133  read(dict);
134 }
135 
136 
137 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
138 
139 template<class Type>
141 {
142  if (!redirectFvOptionPtr_.valid())
143  {
144  dictionary constructDict(dict_);
145  constructDict.set("type", name_);
146 
147  redirectFvOptionPtr_ = option::New
148  (
149  name_,
150  constructDict,
151  mesh_
152  );
153  }
154  return redirectFvOptionPtr_();
155 }
156 
157 
158 template<class Type>
160 (
162 )
163 {
164  if (debug)
165  {
166  Info<< "CodedSource<"<< pTraits<Type>::typeName
167  << ">::correct for source " << name_ << endl;
168  }
169 
170  updateLibrary(name_);
171  redirectFvOption().correct(field);
172 }
173 
174 
175 template<class Type>
177 (
178  fvMatrix<Type>& eqn,
179  const label fieldi
180 )
181 {
182  if (debug)
183  {
184  Info<< "CodedSource<"<< pTraits<Type>::typeName
185  << ">::addSup for source " << name_ << endl;
186  }
187 
188  updateLibrary(name_);
189  redirectFvOption().addSup(eqn, fieldi);
190 }
191 
192 
193 template<class Type>
195 (
196  const volScalarField& rho,
197  fvMatrix<Type>& eqn,
198  const label fieldi
199 )
200 {
201  if (debug)
202  {
203  Info<< "CodedSource<"<< pTraits<Type>::typeName
204  << ">::addSup for source " << name_ << endl;
205  }
206 
207  updateLibrary(name_);
208  redirectFvOption().addSup(rho, eqn, fieldi);
209 }
210 
211 
212 template<class Type>
214 (
215  fvMatrix<Type>& eqn,
216  const label fieldi
217 )
218 {
219  if (debug)
220  {
221  Info<< "CodedSource<"<< pTraits<Type>::typeName
222  << ">::constrain for source " << name_ << endl;
223  }
224 
225  updateLibrary(name_);
226  redirectFvOption().constrain(eqn, fieldi);
227 }
228 
229 
230 // ************************************************************************* //
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
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:158
void setMakeOptions(const std::string &content)
Define contents for Make/options.
Definition: dynamicCode.C:388
virtual void correct(GeometricField< Type, fvPatchField, volMesh > &)
Correct field.
Definition: CodedSource.C:160
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:256
virtual const dictionary & codeDict() const
Get the dictionary to initialize the codeContext.
Definition: CodedSource.C:107
Traits class for primitives.
Definition: pTraits.H:50
virtual void clearRedirect() const
Clear any redirected objects.
Definition: CodedSource.C:100
Generic GeometricField class.
virtual void constrain(fvMatrix< Type > &eqn, const label fieldi)
Set value.
Definition: CodedSource.C:214
void addCompileFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:356
void read(Istream &, label &, const dictionary &)
In-place read with dictionary lookup.
Class to control time during OpenFOAM simulations that is also the top-level objectRegistry.
Definition: Time.H:68
virtual const wordList & codeKeys() const
Get the keywords associated with source code.
Definition: CodedSource.C:114
tmp< DimensionedField< TypeR, GeoMesh > > New(const tmp< DimensionedField< TypeR, GeoMesh >> &tdf1, const word &name, const dimensionSet &dimensions)
option & redirectFvOption() const
Dynamically compiled fvOption.
Definition: CodedSource.C:140
A class for handling words, derived from string.
Definition: word.H:59
A special matrix type and solver, designed for finite volume solutions of scalar equations. Face addressing is used to make all matrix assembly and solution loops vectorise.
Definition: fvPatchField.H:72
virtual string description() const
Return a description (type + name) for the output.
Definition: CodedSource.C:93
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedSource.C:43
A table of dynamically loaded libraries.
CodedSource(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
Definition: CodedSource.C:124
const string & libs() const
Return the code-libs.
Tools for handling dynamic code compilation.
Definition: dynamicCode.H:56
virtual void addSup(fvMatrix< Type > &eqn, const label fieldi)
Explicit and implicit matrix contributions.
Definition: CodedSource.C:177
Encapsulation of dynamic code dictionaries.
void setFilterVariable(const word &key, const std::string &value)
Define a filter variable.
Definition: dynamicCode.C:379
const string & options() const
Return the code-options.
Mesh data needed to do the Finite Volume discretisation.
Definition: fvMesh.H:78
virtual dlLibraryTable & libs() const
Get the loaded dynamic libraries.
Definition: CodedSource.C:86
void addCopyFile(const fileName &name)
Add a file template name, which will be found and filtered.
Definition: dynamicCode.C:362
A special matrix type and solver, designed for finite volume solutions of scalar equations.
void set(entry *)
Assign a new entry, overwrite any existing entry.
Definition: dictionary.C:948
messageStream Info
Cell-set options abstract base class. Provides a base set of controls, e.g.:
Definition: cellSetOption.H:69
A class for handling character strings derived from std::string.
Definition: string.H:74
Finite volume options abstract base class. Provides a base set of controls, e.g.: ...
Definition: fvOption.H:66