ENH: codedFixedValue: refactored codedFixedValue and codedFunctionObject
[OpenFOAM-2.0.x.git] / src / postProcessing / functionObjects / utilities / codedFunctionObject / codedFunctionObject.C
blob92f0bf417d74a099d9cc15c6059cb36dbddfca7a
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 "codedFunctionObject.H"
27 #include "volFields.H"
28 #include "dictionary.H"
29 #include "Time.H"
30 #include "SHA1Digest.H"
31 #include "dynamicCode.H"
32 #include "dynamicCodeContext.H"
33 #include "stringOps.H"
34 #include "addToRunTimeSelectionTable.H"
36 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 namespace Foam
40     defineTypeNameAndDebug(codedFunctionObject, 0);
42     addToRunTimeSelectionTable
43     (
44         functionObject,
45         codedFunctionObject,
46         dictionary
47     );
50 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
53 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
55 void Foam::codedFunctionObject::prepare
57     dynamicCode& dynCode,
58     const dynamicCodeContext& context
59 ) const
61     // Set additional rewrite rules
62     dynCode.setFilterVariable("typeName", redirectType_);
63     dynCode.setFilterVariable("codeRead", codeRead_);
64     dynCode.setFilterVariable("codeExecute", codeExecute_);
65     dynCode.setFilterVariable("codeEnd", codeEnd_);
66     //dynCode.setFilterVariable("codeWrite", codeWrite_);
67     
68     // compile filtered C template
69     dynCode.addCompileFile("functionObjectTemplate.C");
70     dynCode.addCompileFile("FilterFunctionObjectTemplate.C");
71     
72     // copy filtered H template
73     dynCode.addCopyFile("FilterFunctionObjectTemplate.H");
74     dynCode.addCopyFile("functionObjectTemplate.H");
75     dynCode.addCopyFile("IOfunctionObjectTemplate.H");
76     
77     // debugging: make BC verbose
78     //         dynCode.setFilterVariable("verbose", "true");
79     //         Info<<"compile " << redirectType_ << " sha1: "
80     //             << context.sha1() << endl;
81     
82     // define Make/options
83     dynCode.setMakeOptions
84         (
85             "EXE_INC = -g \\\n"
86             "-I$(LIB_SRC)/finiteVolume/lnInclude \\\n"
87             + context.options()
88             + "\n\nLIB_LIBS = \\\n"
89             + "    -lOpenFOAM \\\n"
90             + "    -lfiniteVolume \\\n"
91             + context.libs()
92         );
96 Foam::dlLibraryTable& Foam::codedFunctionObject::libs() const
98     return const_cast<Time&>(time_).libs();
102 Foam::string Foam::codedFunctionObject::description() const
104     return "functionObject " + name();
108 void Foam::codedFunctionObject::clearRedirect() const
110     redirectFunctionObjectPtr_.clear();
114 const Foam::dictionary& Foam::codedFunctionObject::codeDict() const
116     return dict_;
120 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
122 Foam::codedFunctionObject::codedFunctionObject
124     const word& name,
125     const Time& time,
126     const dictionary& dict,
127     bool readNow
130     functionObject(name),
131     codedBase(),
132     time_(time)
134     if (readNow)
135     {
136         read(dict_);
137     }
141 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
143 Foam::codedFunctionObject::~codedFunctionObject()
147 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
149 Foam::functionObject&
150 Foam::codedFunctionObject::redirectFunctionObject() const
152     if (!redirectFunctionObjectPtr_.valid())
153     {
154         dictionary constructDict(dict_);
155         constructDict.set("type", redirectType_);
157         redirectFunctionObjectPtr_ = functionObject::New
158         (
159             redirectType_,
160             time_,
161             constructDict
162         );
163     }
164     return redirectFunctionObjectPtr_();
168 bool Foam::codedFunctionObject::start()
170     updateLibrary(redirectType_);
171     return redirectFunctionObject().start();
175 bool Foam::codedFunctionObject::execute(const bool forceWrite)
177     updateLibrary(redirectType_);
178     return redirectFunctionObject().execute(forceWrite);
182 bool Foam::codedFunctionObject::end()
184     updateLibrary(redirectType_);
185     return redirectFunctionObject().end();
189 bool Foam::codedFunctionObject::read(const dictionary& dict)
191     dict.lookup("redirectType") >> redirectType_;
193     const entry* readPtr = dict.lookupEntryPtr
194     (
195         "codeRead",
196         false,
197         false
198     );
199     if (readPtr)
200     {
201         codeRead_ = stringOps::trim(readPtr->stream());
202         stringOps::inplaceExpand(codeRead_, dict);
203         dynamicCodeContext::addLineDirective
204         (
205             codeRead_,
206             readPtr->startLineNumber(),
207             dict.name()
208         );
209     }
211     const entry* execPtr = dict.lookupEntryPtr
212     (
213         "codeExecute",
214         false,
215         false
216     );
217     if (execPtr)
218     {
219         codeExecute_ = stringOps::trim(execPtr->stream());
220         stringOps::inplaceExpand(codeExecute_, dict);
221         dynamicCodeContext::addLineDirective
222         (
223             codeExecute_,
224             execPtr->startLineNumber(),
225             dict.name()
226         );
227     }
229     const entry* endPtr = dict.lookupEntryPtr
230     (
231         "codeEnd",
232         false,
233         false
234     );
235     if (execPtr)
236     {
237         codeEnd_ = stringOps::trim(endPtr->stream());
238         stringOps::inplaceExpand(codeEnd_, dict);
239         dynamicCodeContext::addLineDirective
240         (
241             codeEnd_,
242             endPtr->startLineNumber(),
243             dict.name()
244         );
245     }
247     updateLibrary(redirectType_);
248     return redirectFunctionObject().read(dict);
252 // ************************************************************************* //