ENH: patchCloudSet: new sampledSet
[OpenFOAM-1.7.x.git] / src / sampling / sampledSurface / sampledSurface / sampledSurfaceTemplates.C
blob922e4247d96a015e55843b8f9c6a86280cc02be1
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
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 "sampledSurface.H"
28 template<class Type>
29 bool Foam::sampledSurface::checkFieldSize(const Field<Type>& field) const
31     if (faces().empty() || field.empty())
32     {
33         return false;
34     }
36     if (field.size() != faces().size())
37     {
38         FatalErrorIn
39         (
40             "sampledSurface::checkFieldSize(const Field<Type>&) const"
41         )
42             << "size mismatch: "
43             << "field (" << field.size()
44             << ") != surface (" << faces().size() << ")"
45             << exit(FatalError);
46     }
48     return true;
52 template<class Type>
53 Type Foam::sampledSurface::integrate(const Field<Type>& field) const
55     Type value = pTraits<Type>::zero;
57     if (checkFieldSize(field))
58     {
59         value = sum(field * magSf());
60     }
62     reduce(value, sumOp<Type>());
63     return value;
67 template<class Type>
68 Type Foam::sampledSurface::integrate(const tmp<Field<Type> >& field) const
70     Type value = integrate(field());
71     field.clear();
72     return value;
76 template<class Type>
77 Type Foam::sampledSurface::average(const Field<Type>& field) const
79     Type value = pTraits<Type>::zero;
81     if (checkFieldSize(field))
82     {
83         value = sum(field * magSf());
84     }
86     reduce(value, sumOp<Type>());
88     // avoid divide-by-zero
89     if (area())
90     {
91         return value / area();
92     }
93     else
94     {
95         return pTraits<Type>::zero;
96     }
100 template<class Type>
101 Type Foam::sampledSurface::average(const tmp<Field<Type> >& field) const
103     Type value = average(field());
104     field.clear();
105     return value;
109 template<class ReturnType, class Type>
110 void Foam::sampledSurface::project
112     Field<ReturnType>& res,
113     const Field<Type>& field
114 ) const
116     if (checkFieldSize(field))
117     {
118         const vectorField& norm = Sf();
120         forAll(norm, faceI)
121         {
122             res[faceI] = field[faceI] & (norm[faceI] / mag(norm[faceI]));
123         }
124     }
125     else
126     {
127         res.clear();
128     }
132 template<class ReturnType, class Type>
133 void Foam::sampledSurface::project
135     Field<ReturnType>& res,
136     const tmp<Field<Type> >& field
137 ) const
139     project(res, field());
140     field.clear();
144 template<class ReturnType, class Type>
145 Foam::tmp<Foam::Field<ReturnType> >
146 Foam::sampledSurface::project
148     const tmp<Field<Type> >& field
149 ) const
151     tmp<Field<ReturnType> > tRes(new Field<ReturnType>(faces().size()));
152     project(tRes(), field);
153     return tRes;
157 // ************************************************************************* //