RobinBC.cc 8.74 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(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
    if (alpha) {
52
      Operator *alphaOp = new Operator(rowFeSpace, colFeSpace);
53
      alphaOp->addZeroOrderTerm(new CoordsAtQP_ZOT(alpha));
Thomas Witkowski's avatar
Thomas Witkowski committed
54
      robinOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
55

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

Thomas Witkowski's avatar
Thomas Witkowski committed
63

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

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

    // for all element sides
80
81
82
83
84
85
    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)
						       );
86
      // for each vertex of the side
87
88
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
89
90
91
92
	(*coords[i])[k][index] = 1.0;
      }
    }

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
112
      delete alphaOp;
113
114
115
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
116

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

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

    // for all element sides
132
133
134
135
136
    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));
137
      // for each vertex of the side
138
139
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
140
141
142
143
	(*coords[i])[k][index] = 1.0;
      }
    }

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
155

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
173

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

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

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

    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;
205
    const BasisFunction *basFcts = dv->getFeSpace()->getBasisFcts();
206

207
    TEST_EXIT(basFcts == rowFeSpace->getBasisFcts())("invalid basFcts\n");
208

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

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

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

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

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

232
    for (int face = 0; face < dim + 1; face++) {
233
234
235

      elInfo->getNormal(face, normal);

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

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

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

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

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

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

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

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

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

284
    delete [] uhEl;
285
286
287
288
289

    return det * val;
  }

}