RobinBC.cc 9.09 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


13
14
15
16
17
#include "RobinBC.h"
#include "Assembler.h"
#include "DOFVector.h"
#include "DOFMatrix.h"
#include "SurfaceOperator.h"
18
19
#include "est/Estimator.h"

20
21
22
23
24
25
26
#include <math.h>

namespace AMDiS {

  RobinBC::RobinBC(BoundaryType type,
		   AbstractFunction<double, WorldVector<double> > *j,
		   AbstractFunction<double, WorldVector<double> > *alpha,
27
28
		   const FiniteElemSpace *rowFeSpace_,
		   const FiniteElemSpace *colFeSpace_)
29
    : BoundaryCondition(type, rowFeSpace_, colFeSpace_), 
30
31
32
      neumannOperators(NULL), 
      robinOperators(NULL)
  {
33
    int dim = rowFeSpace->getMesh()->getDim();
34
35
36

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

    // for all element sides
40
    for (int i = 0; i < dim + 1; i++) {
41
42
43
      coords[i] = 
	new VectorOfFixVecs<DimVec<double> >(dim, dim, DEFAULT_VALUE,
					     DimVec<double>(dim, DEFAULT_VALUE, 0.0));
44
      // for each vertex of the side
45
46
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
47
48
49
50
	(*coords[i])[k][index] = 1.0;
      }
    }

51
    if (j) {
52
      Operator *jOp = new Operator(rowFeSpace);
53
      jOp->addZeroOrderTerm(new CoordsAtQP_ZOT(j));
Thomas Witkowski's avatar
Thomas Witkowski committed
54
      neumannOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
55
    
56
      for (int i = 0; i < dim + 1; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
57
	(*neumannOperators)[i] = new SurfaceOperator(jOp, *coords[i]);
58

Thomas Witkowski's avatar
Thomas Witkowski committed
59
      delete jOp;
60
61
    }

62
    if (alpha) {
63
      Operator *alphaOp = new Operator(rowFeSpace, colFeSpace);
64
      alphaOp->addZeroOrderTerm(new CoordsAtQP_ZOT(alpha));
Thomas Witkowski's avatar
Thomas Witkowski committed
65
      robinOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
66

67
      for (int i = 0; i < dim + 1; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
68
	(*robinOperators)[i] = new SurfaceOperator(alphaOp, *coords[i]);
69
      
Thomas Witkowski's avatar
Thomas Witkowski committed
70
      delete alphaOp;
71
72
      
      WARNING("Sign of alpha changed in RobinBC!\n");
73
74
75
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
76

77
78
79
  RobinBC::RobinBC(BoundaryType type,
		   DOFVectorBase<double> *j,
		   DOFVectorBase<double> *alpha,
80
81
		   const FiniteElemSpace *rowFeSpace_,
		   const FiniteElemSpace *colFeSpace_)
82
    : BoundaryCondition(type, rowFeSpace_, colFeSpace_), 
83
84
85
      neumannOperators(NULL), 
      robinOperators(NULL)
  {
86
    int dim = rowFeSpace->getMesh()->getDim();
87
88
89

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

    // for all element sides
93
    for (int i = 0; i < dim + 1; i++) {
94
95
96
      coords[i] =
	new VectorOfFixVecs<DimVec<double> >(dim, dim, DEFAULT_VALUE, 
					     DimVec<double>(dim, DEFAULT_VALUE, 0.0));
97
      // for each vertex of the side
98
99
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
100
101
102
103
	(*coords[i])[k][index] = 1.0;
      }
    }

104
    if (j) {
105
      Operator *jOp = new Operator(rowFeSpace);
106
      jOp->addZeroOrderTerm(new VecAtQP_ZOT(j, NULL));
Thomas Witkowski's avatar
Thomas Witkowski committed
107
      neumannOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
108
    
109
      for (int i = 0; i < dim + 1; i++)
110
	(*neumannOperators)[i] = new SurfaceOperator(jOp, *coords[i]);    
111

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

115
    if (alpha) {
116
      Operator *alphaOp = new Operator(rowFeSpace, colFeSpace);
117
      alphaOp->addZeroOrderTerm(new VecAtQP_ZOT(alpha, NULL));
Thomas Witkowski's avatar
Thomas Witkowski committed
118
      robinOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
119

120
121
      for (int i = 0; i < dim + 1; i++)
	(*robinOperators)[i] = new SurfaceOperator(alphaOp, *coords[i]);     
122

Thomas Witkowski's avatar
Thomas Witkowski committed
123
      delete alphaOp;
124
      WARNING("Sign of alpha changed in RobinBC!\n");
125
126
127
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
128

129
130
  RobinBC::RobinBC(BoundaryType type,
		   Operator* jOp, Operator* alphaOp,
131
132
		   const FiniteElemSpace *rowFeSpace_,
		   const FiniteElemSpace *colFeSpace_) 
133
    : BoundaryCondition(type, rowFeSpace_, colFeSpace_), 
134
135
136
      neumannOperators(NULL), 
      robinOperators(NULL)
  {
137
    int dim = rowFeSpace->getMesh()->getDim();
138
139
140

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

    // for all element sides
144
145
146
147
148
    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));
149
      // for each vertex of the side
150
151
      for (int k = 0; k < dim; k++) {
	int index = refElement->getVertexOfPosition(INDEX_OF_DIM(dim - 1, dim), i, k);
152
153
154
155
	(*coords[i])[k][index] = 1.0;
      }
    }

156
157
158
    if (jOp) {
      neumannOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
      for (int i = 0; i < dim + 1; i++) 
Thomas Witkowski's avatar
Thomas Witkowski committed
159
	(*neumannOperators)[i] = new SurfaceOperator(jOp, *coords[i]);
160
161
162
163
    }
    if (alphaOp) {
      robinOperators = new DimVec<SurfaceOperator*>(dim, NO_INIT);
      for (int i = 0; i < dim + 1; i++) 
Thomas Witkowski's avatar
Thomas Witkowski committed
164
	(*robinOperators)[i] = new SurfaceOperator(alphaOp, *coords[i]);
165
      WARNING("Sign of alpha changed in RobinBC!\n");
166
167
    }

168
169
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
170

171
  void RobinBC::fillBoundaryCondition(DOFVectorBase<double>* vector, 
172
173
174
175
				      ElInfo* elInfo,
				      const DegreeOfFreedom* dofIndices,
				      const BoundaryType* localBound,
				      int nBasFcts)
176
177
  {
    FUNCNAME("RobinBC::fillBoundaryCondition()");
178
    TEST_EXIT_DBG(vector->getFeSpace() == rowFeSpace)("invalid row fe space\n");
179
180
181

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

Thomas Witkowski's avatar
Thomas Witkowski committed
182
183
184
    if (neumannOperators)
      for (int i = 0; i < dim + 1; i++)
	if (elInfo->getBoundary(i) == boundaryType)
185
186
187
	  vector->assemble(1.0, elInfo, localBound, (*neumannOperators)[i]);
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
188

189
190
  void RobinBC::fillBoundaryCondition(DOFMatrix* matrix,
				      ElInfo* elInfo,
191
				      const DegreeOfFreedom* dofIndices,
192
193
				      const BoundaryType* localBound,
				      int nBasFcts) 
194
  {
195
    if (robinOperators) {
196
197
198
199
      int dim = elInfo->getMesh()->getDim();

      for (int i = 0; i < dim + 1; i++)
	if (elInfo->getBoundary(i) == boundaryType)
200
	  matrix->assemble(1.0, elInfo, localBound, (*robinOperators)[i]);
201
202
203
    }
  }
  
Thomas Witkowski's avatar
Thomas Witkowski committed
204

205
206
207
208
209
  double RobinBC::boundResidual(ElInfo *elInfo,
				DOFMatrix *matrix,
				const DOFVectorBase<double> *dv)
  {
    FUNCNAME("RobinBC::fillBoundaryCondition()");
210
211
    TEST_EXIT(matrix->getRowFeSpace() == rowFeSpace)("invalid row fe space\n");
    TEST_EXIT(matrix->getColFeSpace() == colFeSpace)("invalid col fe space\n");
212
213
214
215
216
217
218
219

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

222
    TEST_EXIT(basFcts == rowFeSpace->getBasisFcts())("invalid basFcts\n");
223

224
    ElementVector uhEl(basFcts->getNumber());
225
226
227
228
229
    dv->getLocalVector(elInfo->getElement(), uhEl);

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

230
    if (!robinOperators) {
231
      neumannQuad = true;
232
233
    } else {
      if (neumannOperators) {
234
	if ((*neumannOperators)[0]->getAssembler()->
235
	    getZeroOrderAssembler()->getQuadrature()->getNumPoints() > 
236
	    (*robinOperators)[0]->getAssembler()->
237
	    getZeroOrderAssembler()->getQuadrature()->getNumPoints()) 
238
	  neumannQuad = true;
239
240
241
      }
    }

242
    std::vector<Operator*>::iterator op;
243
    for (op = matrix->getOperatorsBegin(); op != matrix->getOperatorsEnd(); ++op)
244
      (*op)->getAssembler()->initElement(elInfo);        
245

246
    for (int face = 0; face < dim + 1; face++) {
247
248
      elInfo->getNormal(face, normal);

249
      Quadrature *quadrature = 
250
	neumannQuad ? 
251
	(*neumannOperators)[face]->getAssembler()->
252
	getZeroOrderAssembler()->getQuadrature() :
253
	(*robinOperators)[face]->getAssembler()->
254
255
	getZeroOrderAssembler()->getQuadrature();

256
      if (elInfo->getBoundary(face) == boundaryType) {
257
	(*neumannOperators)[face]->getAssembler()->
Thomas Witkowski's avatar
Thomas Witkowski committed
258
	  getZeroOrderAssembler()->initElement(elInfo);
259

260
261
	int nPoints = quadrature->getNumPoints();
	mtl::dense_vector<double> uhAtQp(nPoints);
262
263
	mtl::dense_vector<WorldVector<double> > grdUhAtQp;
	mtl::dense_vector<WorldMatrix<double> > D2UhAtQp;
264
	dv->getVecAtQPs(elInfo, quadrature, NULL, uhAtQp);
265
266
	ElementVector f(nPoints);
	f = 0.0;
267

268
	if (robinOperators)
269
	  (*robinOperators)[face]->evalZeroOrder(nPoints, uhAtQp, grdUhAtQp,  D2UhAtQp, f, -1.0);
270
	
Thomas Witkowski's avatar
Thomas Witkowski committed
271
272
	std::vector<WorldVector<double> > grdUh(nPoints);
	std::vector<WorldVector<double> > A_grdUh(nPoints);
273

Thomas Witkowski's avatar
Thomas Witkowski committed
274
	for (int iq = 0; iq < nPoints; iq++) {
275
276
	  A_grdUh[iq].set(0.0);	
	  lambda = quadrature->getLambda(iq);
277
	  basFcts->evalGrdUh(lambda, Lambda, uhEl, grdUh[iq]);
278
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
279
	
280
	for (op = matrix->getOperatorsBegin(); op != matrix->getOperatorsEnd(); ++op)
Thomas Witkowski's avatar
Thomas Witkowski committed
281
	  (*op)->weakEvalSecondOrder(grdUh, A_grdUh);		
282

283
	if (neumannOperators)
Thomas Witkowski's avatar
Thomas Witkowski committed
284
	  (*neumannOperators)[face]->getC(elInfo, nPoints, f);
285

Thomas Witkowski's avatar
Thomas Witkowski committed
286
287
	val = 0.0;
	for (int iq = 0; iq < nPoints; iq++) {
288
289
	  n_A_grdUh = (normal * A_grdUh[iq]) - f[iq]; 
	  val += quadrature->getWeight(iq) * sqr(n_A_grdUh);
290
291
292
293
294
295
296
297
	}
      }
    }

    return det * val;
  }

}