RobinBC.cc 8.84 KB
Newer Older
1
2
3
4
5
#include "RobinBC.h"
#include "Estimator.h"
#include "Assembler.h"
#include "DOFVector.h"
#include "DOFMatrix.h"
6
#include "OpenMP.h"
7
8
9
10
11
12
13
14
#include "SurfaceOperator.h"
#include <math.h>

namespace AMDiS {

  RobinBC::RobinBC(BoundaryType type,
		   AbstractFunction<double, WorldVector<double> > *j,
		   AbstractFunction<double, WorldVector<double> > *alpha,
15
16
17
		   FiniteElemSpace *rowFeSpace_,
		   FiniteElemSpace *colFeSpace_)
    : BoundaryCondition(type, rowFeSpace_, colFeSpace_), 
18
19
20
      neumannOperators(NULL), 
      robinOperators(NULL)
  {
21
    int dim = rowFeSpace->getMesh()->getDim();
22
23
24

    // create barycentric coords for each vertex of each side
    const Element *refElement = Global::getReferenceElement(dim);
25
    coords = new VectorOfFixVecs<DimVec<double> >*[dim+1];
26
27

    // for all element sides
28
    for (int i = 0; i < dim+1; i++) {
29
30
31
32
      coords[i] = new VectorOfFixVecs<DimVec<double> >(dim, dim, DEFAULT_VALUE,
						       DimVec<double>(dim, 
								      DEFAULT_VALUE, 
								      0.0));
33
      // for each vertex of the side
34
35
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
36
37
38
39
	(*coords[i])[k][index] = 1.0;
      }
    }

40
    if (j) {
41
      Operator *jOp = new Operator(Operator::VECTOR_OPERATOR, rowFeSpace);
42
      jOp->addZeroOrderTerm(new CoordsAtQP_ZOT(j));
Thomas Witkowski's avatar
Thomas Witkowski committed
43
      neumannOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
44
    
45
      for (int i = 0; i < dim + 1; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
46
	(*neumannOperators)[i] = new SurfaceOperator(jOp, *coords[i]);
47

Thomas Witkowski's avatar
Thomas Witkowski committed
48
      delete jOp;
49
50
    }

51
52
    if (alpha) {
      Operator *alphaOp = 
53
	new Operator(Operator::MATRIX_OPERATOR, rowFeSpace, colFeSpace);
54
      alphaOp->addZeroOrderTerm(new CoordsAtQP_ZOT(alpha));
Thomas Witkowski's avatar
Thomas Witkowski committed
55
      robinOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
56

57
      for (int i = 0; i < dim + 1; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
58
	(*robinOperators)[i] = new SurfaceOperator(alphaOp, *coords[i]);
59
      
Thomas Witkowski's avatar
Thomas Witkowski committed
60
      delete alphaOp;
61
62
63
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
64

65
66
67
  RobinBC::RobinBC(BoundaryType type,
		   DOFVectorBase<double> *j,
		   DOFVectorBase<double> *alpha,
68
69
70
		   FiniteElemSpace *rowFeSpace_,
		   FiniteElemSpace *colFeSpace_)
    : BoundaryCondition(type, rowFeSpace_, colFeSpace_), 
71
72
73
      neumannOperators(NULL), 
      robinOperators(NULL)
  {
74
    int dim = rowFeSpace->getMesh()->getDim();
75
76
77

    // create barycentric coords for each vertex of each side
    const Element *refElement = Global::getReferenceElement(dim);
78
    coords = new VectorOfFixVecs<DimVec<double> >*[dim+1];
79
80

    // for all element sides
81
82
83
84
85
86
    for (int i = 0; i < dim + 1; i++) {
      coords[i] = new VectorOfFixVecs<DimVec<double> >(dim, dim, DEFAULT_VALUE, 
						       DimVec<double>(dim, 
								      DEFAULT_VALUE, 
								      0.0)
						       );
87
      // for each vertex of the side
88
89
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
90
91
92
93
	(*coords[i])[k][index] = 1.0;
      }
    }

94
    if (j) {
95
      Operator *jOp = new Operator(Operator::VECTOR_OPERATOR, rowFeSpace);
96
      jOp->addZeroOrderTerm(new VecAtQP_ZOT(j, NULL));
Thomas Witkowski's avatar
Thomas Witkowski committed
97
      neumannOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
98
    
99
      for (int i=0; i < dim+1; i++)
100
	(*neumannOperators)[i] = new SurfaceOperator(jOp, *coords[i]);    
101

Thomas Witkowski's avatar
Thomas Witkowski committed
102
      delete jOp;
103
104
    }

105
106
    if (alpha) {
      Operator *alphaOp = 
107
	new Operator(Operator::MATRIX_OPERATOR, rowFeSpace, colFeSpace);
108
      alphaOp->addZeroOrderTerm(new VecAtQP_ZOT(alpha, NULL));
Thomas Witkowski's avatar
Thomas Witkowski committed
109
      robinOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
110

111
112
      for (int i = 0; i < dim + 1; i++)
	(*robinOperators)[i] = new SurfaceOperator(alphaOp, *coords[i]);     
113

Thomas Witkowski's avatar
Thomas Witkowski committed
114
      delete alphaOp;
115
116
117
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
118

119
120
  RobinBC::RobinBC(BoundaryType type,
		   Operator* jOp, Operator* alphaOp,
121
122
123
		   FiniteElemSpace *rowFeSpace_,
		   FiniteElemSpace *colFeSpace_) 
    : BoundaryCondition(type, rowFeSpace_, colFeSpace_), 
124
125
126
      neumannOperators(NULL), 
      robinOperators(NULL)
  {
127
    int dim = rowFeSpace->getMesh()->getDim();
128
129
130

    // create barycentric coords for each vertex of each side
    const Element *refElement = Global::getReferenceElement(dim);
131
    coords = new VectorOfFixVecs<DimVec<double> >*[dim+1];
132
133

    // for all element sides
134
135
136
137
138
    for (int i = 0; i < dim + 1; i++) {
      coords[i] = new VectorOfFixVecs<DimVec<double> >(dim, dim, DEFAULT_VALUE,
						       DimVec<double>(dim,
								      DEFAULT_VALUE, 
								      0.0));
139
      // for each vertex of the side
140
141
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
142
143
144
145
	(*coords[i])[k][index] = 1.0;
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
146
147
    neumannOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
    robinOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
148

149
150
    for (int i = 0; i < dim + 1; i++) {
      if (jOp)
Thomas Witkowski's avatar
Thomas Witkowski committed
151
	(*neumannOperators)[i] = new SurfaceOperator(jOp, *coords[i]);
152
      if (alphaOp)
Thomas Witkowski's avatar
Thomas Witkowski committed
153
	(*robinOperators)[i] = new SurfaceOperator(alphaOp, *coords[i]);
154
155
156
    }   
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
157

158
  void RobinBC::fillBoundaryCondition(DOFVectorBase<double>* vector, 
159
160
161
162
				      ElInfo* elInfo,
				      const DegreeOfFreedom* dofIndices,
				      const BoundaryType* localBound,
				      int nBasFcts)
163
164
  {
    FUNCNAME("RobinBC::fillBoundaryCondition()");
165
    TEST_EXIT_DBG(vector->getFeSpace() == rowFeSpace)("invalid row fe space\n");
166
167
168

    int dim = elInfo->getMesh()->getDim();

Thomas Witkowski's avatar
Thomas Witkowski committed
169
170
171
    if (neumannOperators)
      for (int i = 0; i < dim + 1; i++)
	if (elInfo->getBoundary(i) == boundaryType)
172
173
174
	  vector->assemble(1.0, elInfo, localBound, (*neumannOperators)[i]);
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
175

176
177
  void RobinBC::fillBoundaryCondition(DOFMatrix* matrix,
				      ElInfo* elInfo,
178
				      const DegreeOfFreedom* dofIndices,
179
180
				      const BoundaryType* localBound,
				      int nBasFcts) 
181
  {
182
    if (robinOperators) {
183
184
185
186
      int dim = elInfo->getMesh()->getDim();

      for (int i = 0; i < dim + 1; i++)
	if (elInfo->getBoundary(i) == boundaryType)
187
188
189
190
	  matrix->assemble(-1.0, elInfo, localBound, (*robinOperators)[i]);
    }
  }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
191

192
193
194
195
196
  double RobinBC::boundResidual(ElInfo *elInfo,
				DOFMatrix *matrix,
				const DOFVectorBase<double> *dv)
  {
    FUNCNAME("RobinBC::fillBoundaryCondition()");
197
198
    TEST_EXIT(matrix->getRowFeSpace() == rowFeSpace)("invalid row fe space\n");
    TEST_EXIT(matrix->getColFeSpace() == colFeSpace)("invalid col fe space\n");
199
200
201
202
203
204
205
206

    int dim = elInfo->getMesh()->getDim();
    DimVec<double>  lambda(dim, NO_INIT);
    double n_A_grdUh, val = 0.0;
    WorldVector<double> normal;
    const DimVec<WorldVector<double> > &Lambda = elInfo->getGrdLambda();
    double det = elInfo->getDet();
    bool neumannQuad = false;
207
    const BasisFunction *basFcts = dv->getFeSpace()->getBasisFcts();
208

209
    TEST_EXIT(basFcts == rowFeSpace->getBasisFcts())("invalid basFcts\n");
210

211
    double *uhEl = new double[basFcts->getNumber()];
212
213
214
215
216
217

    dv->getLocalVector(elInfo->getElement(), uhEl);

    TEST_EXIT(neumannOperators || robinOperators)
      ("neither neumann nor robin operators set!\n");

218
    if (!robinOperators) {
219
      neumannQuad = true;
220
221
222
223
224
225
    } else {
      if (neumannOperators) {
	if ((*neumannOperators)[0]->getAssembler(omp_get_thread_num())->
	    getZeroOrderAssembler()->getQuadrature()->getNumPoints() > 
	    (*robinOperators)[0]->getAssembler(omp_get_thread_num())->
	    getZeroOrderAssembler()->getQuadrature()->getNumPoints()) 
226
	  neumannQuad = true;
227
228
229
      }
    }

230
    std::vector<Operator*>::iterator op;
231
    for (op = matrix->getOperatorsBegin(); op != matrix->getOperatorsEnd(); ++op)
Thomas Witkowski's avatar
Thomas Witkowski committed
232
      (*op)->getAssembler(omp_get_thread_num())->initElement(elInfo);        
233

234
    for (int face = 0; face < dim + 1; face++) {
235
236
237

      elInfo->getNormal(face, normal);

238
      Quadrature *quadrature = 
239
	neumannQuad ? 
240
	(*neumannOperators)[face]->getAssembler(omp_get_thread_num())->
241
	getZeroOrderAssembler()->getQuadrature() :
242
	(*robinOperators)[face]->getAssembler(omp_get_thread_num())->
243
244
	getZeroOrderAssembler()->getQuadrature();

Thomas Witkowski's avatar
Thomas Witkowski committed
245
      int nPoints = quadrature->getNumPoints();
246

247
      if (elInfo->getBoundary(face) == boundaryType) {
Thomas Witkowski's avatar
Thomas Witkowski committed
248
249
	(*neumannOperators)[face]->getAssembler(omp_get_thread_num())->
	  getZeroOrderAssembler()->initElement(elInfo);
250

Thomas Witkowski's avatar
Thomas Witkowski committed
251
	const double *uhAtQp = dv->getVecAtQPs(elInfo, quadrature, NULL, NULL);
252

Thomas Witkowski's avatar
Thomas Witkowski committed
253
	std::vector<double> f(nPoints, 0.0);
254

255
	if (robinOperators)
Thomas Witkowski's avatar
Thomas Witkowski committed
256
	  (*robinOperators)[face]->evalZeroOrder(nPoints, 
257
258
259
						 uhAtQp,
						 NULL,
						 NULL,
260
						 &f[0],
261
						 1.0);
262
	
Thomas Witkowski's avatar
Thomas Witkowski committed
263
264
	std::vector<WorldVector<double> > grdUh(nPoints);
	std::vector<WorldVector<double> > A_grdUh(nPoints);
265

Thomas Witkowski's avatar
Thomas Witkowski committed
266
	for (int iq = 0; iq < nPoints; iq++) {
267
268
269
270
	  A_grdUh[iq].set(0.0);	
	  lambda = quadrature->getLambda(iq);
	  basFcts->evalGrdUh(lambda, Lambda, uhEl, &grdUh[iq]);
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
271
	
272
	for (op = matrix->getOperatorsBegin(); op != matrix->getOperatorsEnd(); ++op)
Thomas Witkowski's avatar
Thomas Witkowski committed
273
	  (*op)->weakEvalSecondOrder(grdUh, A_grdUh);		
274

275
	if (neumannOperators)
Thomas Witkowski's avatar
Thomas Witkowski committed
276
	  (*neumannOperators)[face]->getC(elInfo, nPoints, f);
277

Thomas Witkowski's avatar
Thomas Witkowski committed
278
279
	val = 0.0;
	for (int iq = 0; iq < nPoints; iq++) {
280
281
282
283
284
285
	  n_A_grdUh = (normal*A_grdUh[iq]) - f[iq]; 
	  val += quadrature->getWeight(iq)*sqr(n_A_grdUh);
	}
      }
    }

286
    delete [] uhEl;
287
288
289
290
291

    return det * val;
  }

}