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-2020 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 {
37  "codeAddSup",
38  "codeCorrect",
39  "codeInclude",
40  "codeSetValue",
41  "localCode"
42 };
43 
44 
45 // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * //
46 
47 template<class Type>
49 (
50  dynamicCode& dynCode,
51  const dynamicCodeContext& context
52 ) const
53 {
54  word sourceType(pTraits<Type>::typeName);
55 
56  // Set additional rewrite rules
57  dynCode.setFilterVariable("typeName", name_);
58  dynCode.setFilterVariable("TemplateType", sourceType);
59  dynCode.setFilterVariable("SourceType", sourceType + "Source");
60 
61  // compile filtered C template
62  dynCode.addCompileFile("codedFvOptionTemplate.C");
63 
64  // copy filtered H template
65  dynCode.addCopyFile("codedFvOptionTemplate.H");
66 
67  // debugging: make BC verbose
68  // dynCode.setFilterVariable("verbose", "true");
69  // Info<<"compile " << name_ << " sha1: "
70  // << context.sha1() << endl;
71 
72  // define Make/options
73  dynCode.setMakeOptions
74  (
75  "EXE_INC = -g \\\n"
76  "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
77  "-I$(LIB_SRC)/meshTools/lnInclude \\\n"
78  "-I$(LIB_SRC)/sampling/lnInclude \\\n"
79  "-I$(LIB_SRC)/fvOptions/lnInclude \\\n"
80  + context.options()
81  + "\n\nLIB_LIBS = \\\n"
82  + " -lmeshTools \\\n"
83  + " -lfvOptions \\\n"
84  + " -lsampling \\\n"
85  + " -lfiniteVolume \\\n"
86  + context.libs()
87  );
88 }
89 
90 
91 template<class Type>
93 {
94  return "fvOption:: " + name_;
95 }
96 
97 
98 template<class Type>
100 {
101  redirectFvOptionPtr_.clear();
102 }
103 
104 
105 template<class Type>
107 {
108  return coeffs_;
109 }
110 
111 
112 template<class Type>
114 {
115  return codeKeys_;
116 }
117 
118 
119 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
120 
121 template<class Type>
123 (
124  const word& name,
125  const word& modelType,
126  const dictionary& dict,
127  const fvMesh& mesh
128 )
129 :
130  cellSetOption(name, modelType, dict, mesh)
131 {
132  read(dict);
133 }
134 
135 
136 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
137 
138 template<class Type>
140 {
141  if (!redirectFvOptionPtr_.valid())
142  {
143  dictionary constructDict(dict_);
144  constructDict.set("type", name_);
145 
146  redirectFvOptionPtr_ = option::New
147  (
148  name_,
149  constructDict,
150  mesh_
151  );
152  }
153  return redirectFvOptionPtr_();
154 }
155 
156 
157 template<class Type>
159 (
161 )
162 {
163  if (debug)
164  {
165  Info<< "CodedSource<"<< pTraits<Type>::typeName
166  << ">::correct for source " << name_ << endl;
167  }
168 
169  updateLibrary();
170  redirectFvOption().correct(field);
171 }
172 
173 
174 template<class Type>
176 (
177  fvMatrix<Type>& eqn,
178  const label fieldi
179 )
180 {
181  if (debug)
182  {
183  Info<< "CodedSource<"<< pTraits<Type>::typeName
184  << ">::addSup for source " << name_ << endl;
185  }
186 
187  updateLibrary();
188  redirectFvOption().addSup(eqn, fieldi);
189 }
190 
191 
192 template<class Type>
194 (
195  const volScalarField& rho,
196  fvMatrix<Type>& eqn,
197  const label fieldi
198 )
199 {
200  if (debug)
201  {
202  Info<< "CodedSource<"<< pTraits<Type>::typeName
203  << ">::addSup for source " << name_ << endl;
204  }
205 
206  updateLibrary();
207  redirectFvOption().addSup(rho, eqn, fieldi);
208 }
209 
210 
211 template<class Type>
213 (
214  fvMatrix<Type>& eqn,
215  const label fieldi
216 )
217 {
218  if (debug)
219  {
220  Info<< "CodedSource<"<< pTraits<Type>::typeName
221  << ">::constrain for source " << name_ << endl;
222  }
223 
224  updateLibrary();
225  redirectFvOption().constrain(eqn, fieldi);
226 }
227 
228 
229 // ************************************************************************* //
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:159
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
virtual const dictionary & codeDict() const
Get the dictionary to initialize the codeContext.
Definition: CodedSource.C:106
Traits class for primitives.
Definition: pTraits.H:50
virtual void clearRedirect() const
Clear any redirected objects.
Definition: CodedSource.C:99
Generic GeometricField class.
virtual void constrain(fvMatrix< Type > &eqn, const label fieldi)
Set value.
Definition: CodedSource.C:213
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.
virtual const wordList & codeKeys() const
Get the keywords associated with source code.
Definition: CodedSource.C:113
option & redirectFvOption() const
Dynamically compiled fvOption.
Definition: CodedSource.C:139
autoPtr< BasicCompressibleMomentumTransportModel > New(const volScalarField &rho, const volVectorField &U, const surfaceScalarField &phi, const typename BasicCompressibleMomentumTransportModel::transportModel &transport)
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:92
virtual void prepare(dynamicCode &, const dynamicCodeContext &) const
Adapt the context for the current object.
Definition: CodedSource.C:49
CodedSource(const word &name, const word &modelType, const dictionary &dict, const fvMesh &mesh)
Construct from components.
Definition: CodedSource.C:123
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:176
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
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:1194
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:76
Finite volume options abstract base class. Provides a base set of controls, e.g.: ...
Definition: fvOption.H:66