ENH: codedFixedValue: refactored codedFixedValue and codedFunctionObject
[OpenFOAM-2.0.x.git] / src / finiteVolume / fields / fvPatchFields / derived / codedFixedValue / codedFixedValueFvPatchField.C
blobacffe1a9b37a4e3fb2126fa47510822e6b350c56
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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.
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.
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/>.
24 \*---------------------------------------------------------------------------*/
26 #include "codedFixedValueFvPatchField.H"
27 #include "addToRunTimeSelectionTable.H"
28 #include "fvPatchFieldMapper.H"
29 #include "volFields.H"
30 #include "dynamicCode.H"
31 #include "dynamicCodeContext.H"
32 #include "stringOps.H"
34 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
36 template<class Type>
37 const Foam::word Foam::codedFixedValueFvPatchField<Type>::codeTemplateC
38     = "fixedValueFvPatchFieldTemplate.C";
40 template<class Type>
41 const Foam::word Foam::codedFixedValueFvPatchField<Type>::codeTemplateH
42     = "fixedValueFvPatchFieldTemplate.H";
45 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
47 template<class Type>
48 void Foam::codedFixedValueFvPatchField<Type>::setFieldTemplates
50     dynamicCode& dynCode
53     word fieldType(pTraits<Type>::typeName);
55     // template type for fvPatchField
56     dynCode.setFilterVariable("TemplateType", fieldType);
58     // Name for fvPatchField - eg, ScalarField, VectorField, ...
59     fieldType[0] = toupper(fieldType[0]);
60     dynCode.setFilterVariable("FieldType", fieldType + "Field");
64 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
66 template<class Type>
67 const Foam::IOdictionary& Foam::codedFixedValueFvPatchField<Type>::dict() const
69     const objectRegistry& obr = this->db();
71     if (obr.foundObject<IOdictionary>("codeDict"))
72     {
73         return obr.lookupObject<IOdictionary>("codeDict");
74     }
75     else
76     {
77         return obr.store
78         (
79             new IOdictionary
80             (
81                 IOobject
82                 (
83                     "codeDict",
84                     this->db().time().system(),
85                     this->db(),
86                     IOobject::MUST_READ_IF_MODIFIED,
87                     IOobject::NO_WRITE
88                 )
89             )
90         );
91     }
95 template<class Type>
96 Foam::dlLibraryTable& Foam::codedFixedValueFvPatchField<Type>::libs() const
98     return const_cast<dlLibraryTable&>(this->db().time().libs());
102 template<class Type>
103 void Foam::codedFixedValueFvPatchField<Type>::prepare
105     dynamicCode& dynCode,
106     const dynamicCodeContext& context
107 ) const
109     // take no chances - typeName must be identical to redirectType_
110     dynCode.setFilterVariable("typeName", redirectType_);
112     // set TemplateType and FieldType filter variables
113     // (for fvPatchField)
114     setFieldTemplates(dynCode);
116     // compile filtered C template
117     dynCode.addCompileFile(codeTemplateC);
119     // copy filtered H template
120     dynCode.addCopyFile(codeTemplateH);
123     // debugging: make BC verbose
124     //  dynCode.setFilterVariable("verbose", "true");
125     //  Info<<"compile " << redirectType_ << " sha1: "
126     //      << context.sha1() << endl;
128     // define Make/options
129     dynCode.setMakeOptions
130         (
131             "EXE_INC = -g \\\n"
132             "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
133             + context.options()
134             + "\n\nLIB_LIBS = \\\n"
135             + "    -lOpenFOAM \\\n"
136             + "    -lfiniteVolume \\\n"
137             + context.libs()
138         );
142 template<class Type>
143 const Foam::dictionary& Foam::codedFixedValueFvPatchField<Type>::codeDict()
144 const
146     // use system/codeDict or in-line
147     return
148     (
149         dict_.found("code")
150       ? dict_
151       : this->dict().subDict(redirectType_)
152     );
156 template<class Type>
157 Foam::string Foam::codedFixedValueFvPatchField<Type>::description() const
159     return
160         "patch "
161       + this->patch().name()
162       + " on field "
163       + this->dimensionedInternalField().name();
167 template<class Type>
168 void Foam::codedFixedValueFvPatchField<Type>::clearRedirect() const
170     // remove instantiation of fvPatchField provided by library
171     redirectPatchFieldPtr_.clear();
175 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
177 template<class Type>
178 Foam::codedFixedValueFvPatchField<Type>::codedFixedValueFvPatchField
180     const fvPatch& p,
181     const DimensionedField<Type, volMesh>& iF
184     fixedValueFvPatchField<Type>(p, iF),
185     codedBase(),
186     redirectPatchFieldPtr_()
190 template<class Type>
191 Foam::codedFixedValueFvPatchField<Type>::codedFixedValueFvPatchField
193     const codedFixedValueFvPatchField<Type>& ptf,
194     const fvPatch& p,
195     const DimensionedField<Type, volMesh>& iF,
196     const fvPatchFieldMapper& mapper
199     fixedValueFvPatchField<Type>(ptf, p, iF, mapper),
200     codedBase(),
201     dict_(ptf.dict_),
202     redirectType_(ptf.redirectType_),
203     redirectPatchFieldPtr_()
207 template<class Type>
208 Foam::codedFixedValueFvPatchField<Type>::codedFixedValueFvPatchField
210     const fvPatch& p,
211     const DimensionedField<Type, volMesh>& iF,
212     const dictionary& dict
215     fixedValueFvPatchField<Type>(p, iF, dict),
216     codedBase(),
217     dict_(dict),
218     redirectType_(dict.lookup("redirectType")),
219     redirectPatchFieldPtr_()
221     updateLibrary(redirectType_);
225 template<class Type>
226 Foam::codedFixedValueFvPatchField<Type>::codedFixedValueFvPatchField
228     const codedFixedValueFvPatchField<Type>& ptf
231     fixedValueFvPatchField<Type>(ptf),
232     codedBase(),
233     dict_(ptf.dict_),
234     redirectType_(ptf.redirectType_),
235     redirectPatchFieldPtr_()
239 template<class Type>
240 Foam::codedFixedValueFvPatchField<Type>::codedFixedValueFvPatchField
242     const codedFixedValueFvPatchField<Type>& ptf,
243     const DimensionedField<Type, volMesh>& iF
246     fixedValueFvPatchField<Type>(ptf, iF),
247     codedBase(),
248     dict_(ptf.dict_),
249     redirectType_(ptf.redirectType_),
250     redirectPatchFieldPtr_()
254 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
256 template<class Type>
257 const Foam::fvPatchField<Type>&
258 Foam::codedFixedValueFvPatchField<Type>::redirectPatchField() const
260     if (!redirectPatchFieldPtr_.valid())
261     {
262         // Construct a patch
263         // Make sure to construct the patchfield with up-to-date value
265         OStringStream os;
266         os.writeKeyword("type") << redirectType_ << token::END_STATEMENT
267             << nl;
268         static_cast<const Field<Type>&>(*this).writeEntry("value", os);
269         IStringStream is(os.str());
270         dictionary dict(is);
272         redirectPatchFieldPtr_.set
273         (
274             fvPatchField<Type>::New
275             (
276                 this->patch(),
277                 this->dimensionedInternalField(),
278                 dict
279             ).ptr()
280         );
281     }
282     return redirectPatchFieldPtr_();
286 template<class Type>
287 void Foam::codedFixedValueFvPatchField<Type>::updateCoeffs()
289     if (this->updated())
290     {
291         return;
292     }
294     // Make sure library containing user-defined fvPatchField is up-to-date
295     updateLibrary(redirectType_);
297     const fvPatchField<Type>& fvp = redirectPatchField();
299     const_cast<fvPatchField<Type>&>(fvp).updateCoeffs();
301     // Copy through value
302     this->operator==(fvp);
304     fixedValueFvPatchField<Type>::updateCoeffs();
308 template<class Type>
309 void Foam::codedFixedValueFvPatchField<Type>::evaluate
311     const Pstream::commsTypes commsType
314     // Make sure library containing user-defined fvPatchField is up-to-date
315     updateLibrary(redirectType_);
317     const fvPatchField<Type>& fvp = redirectPatchField();
319     const_cast<fvPatchField<Type>&>(fvp).evaluate(commsType);
321     fixedValueFvPatchField<Type>::evaluate(commsType);
325 template<class Type>
326 void Foam::codedFixedValueFvPatchField<Type>::write(Ostream& os) const
328     fixedValueFvPatchField<Type>::write(os);
329     os.writeKeyword("redirectType") << redirectType_
330         << token::END_STATEMENT << nl;
332     if (dict_.found("code"))
333     {
334         os.writeKeyword("code")
335             << token::HASH << token::BEGIN_BLOCK;
337         os.writeQuoted(string(dict_["code"]), false)
338             << token::HASH << token::END_BLOCK
339             << token::END_STATEMENT << nl;
340     }
344 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
347 // ************************************************************************* //