Merge branch 'master' of ssh://git.code.sf.net/p/foam-extend/foam-extend-3.2
[foam-extend-3.2.git] / src / foam / primitives / BlockCoeff / DecoupledBlockCoeff.C
blobdc7b2b38f233b5b0db0922782909f691186c7e29
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "demandDrivenData.H"
28 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
30 template<class Type>
31 typename Foam::DecoupledBlockCoeff<Type>::scalarType&
32 Foam::DecoupledBlockCoeff<Type>::toScalar()
34     if (!scalarCoeffPtr_)
35     {
36         // Debug check: demotion
37         if (linearCoeffPtr_)
38         {
39             FatalErrorIn
40             (
41                 "DecoupledBlockCoeff<Type>::scalarType& "
42                 "DecoupledBlockCoeff<Type>::toScalar()"
43             )   << "Detected demotion to scalar.  Probably an error"
44                 << abort(FatalError);
45         }
47         scalarCoeffPtr_ = new scalarType(pTraits<scalarType>::zero);
48     }
50     return *scalarCoeffPtr_;
54 template<class Type>
55 typename Foam::DecoupledBlockCoeff<Type>::linearType&
56 Foam::DecoupledBlockCoeff<Type>::toLinear()
58     if (!linearCoeffPtr_)
59     {
60         linearCoeffPtr_ = new linearType(pTraits<linearType>::zero);
62         // If scalar is active, promote to linear
63         if (scalarCoeffPtr_)
64         {
65             *linearCoeffPtr_ = (*scalarCoeffPtr_)*pTraits<linearType>::one;
66             deleteDemandDrivenData(scalarCoeffPtr_);
67         }
68     }
70     return *linearCoeffPtr_;
74 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
76 template<class Type>
77 Foam::DecoupledBlockCoeff<Type>::DecoupledBlockCoeff()
79     scalarCoeffPtr_(NULL),
80     linearCoeffPtr_(NULL)
84 template<class Type>
85 Foam::DecoupledBlockCoeff<Type>::DecoupledBlockCoeff
87     const DecoupledBlockCoeff<Type>& f
90     scalarCoeffPtr_(NULL),
91     linearCoeffPtr_(NULL)
93     if (f.scalarCoeffPtr_)
94     {
95         scalarCoeffPtr_ = new scalarType(*(f.scalarCoeffPtr_));
96     }
97     else if (f.linearCoeffPtr_)
98     {
99         linearCoeffPtr_ = new linearType(*(f.linearCoeffPtr_));
100     }
104 template<class Type>
105 Foam::DecoupledBlockCoeff<Type>::DecoupledBlockCoeff(Istream& is)
107     scalarCoeffPtr_(NULL),
108     linearCoeffPtr_(NULL)
110     // Read keyword and pick up allocated field
111     word key(is);
113     if
114     (
115         key
116      == blockCoeffBase::activeLevelNames_[blockCoeffBase::UNALLOCATED]
117     )
118     {
119     }
120     else if
121     (
122         key
123      == blockCoeffBase::activeLevelNames_[blockCoeffBase::SCALAR]
124     )
125     {
126         scalarCoeffPtr_ = new scalarType(readScalar(is));
127     }
128     else if
129     (
130         key
131      == blockCoeffBase::activeLevelNames_[blockCoeffBase::LINEAR]
132     )
133     {
134         linearCoeffPtr_ = new linearType(is);
135     }
136     else
137     {
138         FatalIOErrorIn
139         (
140             "DecoupledBlockCoeff<Type>::DecoupledBlockCoeff(Istream& is)",
141             is
142         )   << "invalid keyword while reading: " << key
143             << exit(FatalIOError);
144     }
148 template<class Type>
149 Foam::DecoupledBlockCoeff<Type> Foam::DecoupledBlockCoeff<Type>::clone() const
151     return DecoupledBlockCoeff<Type>(*this);
155 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
157 template<class Type>
158 Foam::DecoupledBlockCoeff<Type>::~DecoupledBlockCoeff()
160     this->clear();
164 template<class Type>
165 void Foam::DecoupledBlockCoeff<Type>::clear()
167     deleteDemandDrivenData(scalarCoeffPtr_);
168     deleteDemandDrivenData(linearCoeffPtr_);
172 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
174 template<class Type>
175 Foam::blockCoeffBase::activeLevel
176 Foam::DecoupledBlockCoeff<Type>::activeType() const
178     if (scalarCoeffPtr_)
179     {
180         return blockCoeffBase::SCALAR;
181     }
182     else if (linearCoeffPtr_)
183     {
184         return blockCoeffBase::LINEAR;
185     }
186     else
187     {
188         return blockCoeffBase::UNALLOCATED;
189     }
193 template<class Type>
194 void Foam::DecoupledBlockCoeff<Type>::checkActive() const
196     label nActive = 0;
198     if (scalarCoeffPtr_) nActive++;
199     if (linearCoeffPtr_) nActive++;
201     if (nActive > 1)
202     {
203         FatalErrorIn
204         (
205             "void Foam::DecoupledBlockCoeff<Type>::checkActive() const"
206         )   << "Activation/deactivation error.  nActive = " << nActive
207             << abort(FatalError);
208     }
212 template<class Type>
213 const typename Foam::DecoupledBlockCoeff<Type>::scalarType&
214 Foam::DecoupledBlockCoeff<Type>::asScalar() const
216     if (!scalarCoeffPtr_)
217     {
218         FatalErrorIn
219         (
220             "DecoupledBlockCoeff<Type>::scalarType& "
221             "DecoupledBlockCoeff<Type>::asScalar()"
222         )   << "Requested scalar but active type is: "
223             << blockCoeffBase::activeLevelNames_[this->activeType()]
224             << ".  This is not allowed."
225             << abort(FatalError);
226     }
228     return *scalarCoeffPtr_;
232 template<class Type>
233 const typename Foam::DecoupledBlockCoeff<Type>::linearType&
234 Foam::DecoupledBlockCoeff<Type>::asLinear() const
236     if (!linearCoeffPtr_)
237     {
238         FatalErrorIn
239         (
240             "DecoupledBlockCoeff<Type>::linearType& "
241             "DecoupledBlockCoeff<Type>::asLinear()"
242         )   << "Requested linear but active type is: "
243             << blockCoeffBase::activeLevelNames_[this->activeType()]
244             << ".  This is not allowed."
245             << abort(FatalError);
246     }
248     return *linearCoeffPtr_;
252 template<class Type>
253 typename Foam::DecoupledBlockCoeff<Type>::scalarType&
254 Foam::DecoupledBlockCoeff<Type>::asScalar()
256     if (linearCoeffPtr_)
257     {
258         FatalErrorIn
259         (
260             "DecoupledBlockCoeff<Type>::scalarType& "
261             "DecoupledBlockCoeff<Type>::asScalar()"
262         )   << "Requested scalar but active type is: "
263             << blockCoeffBase::activeLevelNames_[this->activeType()]
264             << ".  This is not allowed."
265             << abort(FatalError);
266     }
268     if (!scalarCoeffPtr_)
269     {
270         return this->toScalar();
271     }
273     return *scalarCoeffPtr_;
277 template<class Type>
278 typename Foam::DecoupledBlockCoeff<Type>::linearType&
279 Foam::DecoupledBlockCoeff<Type>::asLinear()
281     if (!linearCoeffPtr_)
282     {
283         return this->toLinear();
284     }
286     return *linearCoeffPtr_;
290 template<class Type>
291 typename Foam::DecoupledBlockCoeff<Type>::scalarType
292 Foam::DecoupledBlockCoeff<Type>::component(const direction dir) const
294     if (scalarCoeffPtr_)
295     {
296         return *scalarCoeffPtr_;
297     }
298     else if (linearCoeffPtr_)
299     {
300         return linearCoeffPtr_->component(dir);
301     }
302     else
303     {
304         FatalErrorIn
305         (
306             "tmp<DecoupledBlockCoeff<Type>::scalarType>"
307             "DecoupledBlockCoeff<Type>::component(const direction dir) const"
308         )   << " not allocated."
309             << abort(FatalError);
310     }
312     // Dummy return to keep compiler happy
313     return *scalarCoeffPtr_;
317 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
319 template<class Type>
320 void Foam::DecoupledBlockCoeff<Type>::operator=
322     const DecoupledBlockCoeff<Type>& f
325     if (this == &f)
326     {
327         FatalErrorIn
328         (
329             "DecoupledBlockCoeff<Type>::operator=("
330             "const DecoupledBlockCoeff<Type>&)"
331         )   << "attempted assignment to self"
332             << abort(FatalError);
333     }
335     if (f.scalarCoeffPtr_)
336     {
337         this->toScalar() = *(f.scalarCoeffPtr_);
338     }
339     else if (f.linearCoeffPtr_)
340     {
341         this->toLinear() = *(f.linearCoeffPtr_);
342     }
343     else
344     {
345         // Not allocated - do nothing
346     }
350 // * * * * * * * * * * * * * * * Ostream Operator  * * * * * * * * * * * * * //
352 template<class Type>
353 Foam::Ostream& Foam::operator<<(Ostream& os, const DecoupledBlockCoeff<Type>& f)
355     // Write active type
356     os << blockCoeffBase::activeLevelNames_[f.activeType()] << nl;
358     if (f.scalarCoeffPtr_)
359     {
360         os << *(f.scalarCoeffPtr_);
361     }
362     else if (f.linearCoeffPtr_)
363     {
364         os << *(f.linearCoeffPtr_);
365     }
367     return os;
371 // ************************************************************************* //