File: ScalarFunctionIntegrate.cpp

package info (click to toggle)
freefem3d 1.0pre10-3.4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 25,016 kB
  • ctags: 8,675
  • sloc: cpp: 57,204; sh: 8,788; yacc: 2,975; makefile: 1,149; ansic: 508; perl: 110
file content (242 lines) | stat: -rw-r--r-- 6,855 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//  This file is part of ff3d - http://www.freefem.org/ff3d
//  Copyright (C) 2001, 2002, 2003 Stphane Del Pino

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

//  $Id: ScalarFunctionIntegrate.cpp,v 1.5 2006/09/12 22:34:39 delpinux Exp $

#include <ScalarFunctionIntegrate.hpp>
#include <ErrorHandler.hpp>

#include <Structured3DMesh.hpp>
#include <ConnectivityBuilder.hpp>
#include <ConformTransformation.hpp>

std::ostream& 
ScalarFunctionIntegrate::
__put(std::ostream& os) const
{
  switch(__direction) {
  case ScalarFunctionIntegrate::x: {
    os << "int[x](";
    break;
  }
  case ScalarFunctionIntegrate::y: {
    os << "int[y](";
    break;
  }
  case ScalarFunctionIntegrate::z: {
    os << "int[z](";
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "unknown integrate direction",
		       ErrorHandler::unexpected);
  }
  }
  os << *__functionToIntegrate << ')';
  return os;
}

template <typename MeshType>
real_t
ScalarFunctionIntegrate::
__evaluate(const TinyVector<3,real_t>& X) const
{
  throw ErrorHandler(__FILE__,__LINE__,
		     "mesh type not supported",
		     ErrorHandler::unexpected);
  return 0;
}

template <>
real_t
ScalarFunctionIntegrate::
__evaluate<Structured3DMesh>(const TinyVector<3,real_t>& X) const
{
  const Mesh& baseMesh = *__functionToIntegrate->baseMesh();
  const Structured3DMesh& mesh = static_cast<const Structured3DMesh&>(baseMesh);

  const real_t lowerBound = (*__lowerBound)(X);
  const real_t upperBound = (*__upperBound)(X);

  const real_t minBound = std::min(lowerBound,upperBound);
  const real_t maxBound = std::max(lowerBound,upperBound);

  TinyVector<3, real_t> a = X;
  TinyVector<3, real_t> b = X;

  a[__direction] = std::max(mesh.shape().a()[__direction], minBound);
  b[__direction] = std::min(mesh.shape().b()[__direction], maxBound);

  Structured3DMesh::const_iterator h = mesh.find(a);

  if (h.end()) {
    return 0; // we are outside the mesh
  }

  const Connectivity<Structured3DMesh>& connectivity = mesh.connectivity();
  if (not(connectivity.hasCellToCells())) {
    ConnectivityBuilder<Structured3DMesh> c(mesh);
    c.generates(Connectivity<Structured3DMesh>::CellToCells);
  }

  TinyVector<3, real_t> X0 = a;

  TinyVector<3, real_t> Xhat0;
  {
    ConformTransformationQ1CartesianHexahedron T0(*h);
    T0.invertT(X0, Xhat0);
  }

  const TinyVector<3,real_t> u = b-a;
  real_t dt0 = 1;

  const size_t faceNumberConverter[6] = {4,2,1,3,0,5};

  real_t integrale = 0;

  do {
    const CartesianHexahedron& H = (*h);
    ConformTransformationQ1CartesianHexahedron T(H);

    a=X0;

    T.value(Xhat0,X0);

    integrale += (*__functionToIntegrate)(0.5*(X0+a))*(X0[__direction]-a[__direction]);

    TinyVector<3, real_t> X1 = X0+dt0*u;

    TinyVector<3, real_t> Xhat1;
    T.invertT(b, Xhat1);

    if((   Xhat1[0] >= 0)
       and(Xhat1[1] >= 0)
       and(Xhat1[2] >= 0)
       and(Xhat1[0] <= 1)
       and(Xhat1[1] <= 1)
       and(Xhat1[2] <= 1)) {

      dt0 = 0;
      integrale += (*__functionToIntegrate)(0.5*(X0+X1))*Norm(X1-X0);
    } else {

      const TinyVector<3, real_t> deltaX = Xhat1-Xhat0;
	
      // Get the planes which are required to compute intersection
      TinyVector<3, real_t> x0 = 0;
      for (size_t i=0; i<3; ++i) {
	if (deltaX[i]>0) x0[i] = 1;
      }

      // Computes the linear abcisse required to reach faces
      TinyVector<3, real_t> coef = std::numeric_limits<real_t>::max();
      for (size_t i=0; i<3; ++i) {
	if (std::abs(deltaX[i])>0) {
	  coef[i] = std::abs((x0[i]-Xhat0[i])/deltaX[i]);
	}
      }

      size_t minimumCoefficentNumber = 0;
      for (size_t i=1; i<3; ++i) {
	minimumCoefficentNumber
	  = (coef[minimumCoefficentNumber]<coef[i])?minimumCoefficentNumber:i;
      }

      const size_t outGoingFace
	= faceNumberConverter[2*minimumCoefficentNumber + ((deltaX[minimumCoefficentNumber]>0)?1:0)];

      const real_t dt = dt0*coef[minimumCoefficentNumber];

      Xhat0 += coef[minimumCoefficentNumber] * deltaX;
      dt0-=dt;

      h = connectivity.cells(*h)[outGoingFace];
      if (h.end()) {
	throw ErrorHandler(__FILE__,__LINE__,
			   "cannot find next cell",
			   ErrorHandler::unexpected);
      } // takes the value on the border when going outside

      // X0 is not updated. We compute the value of Xhat0 in the next cell
      Xhat0[minimumCoefficentNumber] = 1-x0[minimumCoefficentNumber];
    }
  } while (dt0>0);

  if (lowerBound == minBound) {
    return integrale;
  } else {
    return -integrale;
  }
}

real_t
ScalarFunctionIntegrate::
operator()(const TinyVector<3, real_t>& X) const
{
  const Mesh& baseMesh = *__functionToIntegrate->baseMesh();
  if (baseMesh.type() != Mesh::cartesianHexahedraMesh) {
    throw ErrorHandler(__FILE__,__LINE__,
		       "Can only evaluate integrate functions on cartesian structured meshes",
		       ErrorHandler::normal);
  }

  return this->__evaluate<Structured3DMesh>(X);
}

ScalarFunctionIntegrate::
ScalarFunctionIntegrate(ConstReferenceCounting<ScalarFunctionBase> lowerBound,
			ConstReferenceCounting<ScalarFunctionBase> upperBound,
			ConstReferenceCounting<ScalarFunctionBase> functionToIntegrate,
			const ScalarFunctionIntegrate::Direction& direction)
  : ScalarFunctionBase(ScalarFunctionBase::integrate),
    __lowerBound(lowerBound),
    __upperBound(upperBound),
    __direction(direction)
{
  if (functionToIntegrate->type() != ScalarFunctionBase::femfunction) {
    std::stringstream errorMsg;

    errorMsg << "cannot compute 1D integrale of non FEM functions :-(\n";
    errorMsg << "the function " << (*functionToIntegrate) << " is not of that kind"
	     << std::ends;

    throw ErrorHandler(__FILE__,__LINE__,
		       errorMsg.str(),
		       ErrorHandler::normal);

  }
  const ScalarFunctionBase* f = functionToIntegrate;
  __functionToIntegrate = dynamic_cast<const FEMFunctionBase*>(f);
}

ScalarFunctionIntegrate::
ScalarFunctionIntegrate(const ScalarFunctionIntegrate& f)
  : ScalarFunctionBase(f),
    __lowerBound(f.__lowerBound),
    __upperBound(f.__upperBound),
    __functionToIntegrate(f.__functionToIntegrate),
    __direction(f.__direction)
{
  ;
}
    
ScalarFunctionIntegrate::
~ScalarFunctionIntegrate()
{
  ;
}