SecondOrderTerm.cc 43.2 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 "SecondOrderTerm.h"
#include "DOFVector.h"

namespace AMDiS {

18
19
  void SecondOrderTerm::lalt(const DimVec<WorldVector<double> >& Lambda,
			     const WorldMatrix<double>& matrix,
20
			     mtl::dense2D<double>& LALt,
21
22
23
			     bool symm,
			     double factor) const
  {
24
    int dim = num_rows(LALt);
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
  
    if (symm) {
      for (int i = 0; i < dim; i++) {
	double val = 0.0;
	for (int k = 0; k < dimOfWorld; k++)
	  for (int l = 0; l < dimOfWorld; l++)
	    val += Lambda[i][k] * matrix[k][l] * Lambda[i][l];
	val *= factor;
	LALt[i][i] += val;
	for (int j = i + 1; j < dim; j++) {
	  val = 0.0;
	  for (int k = 0; k < dimOfWorld; k++)
	    for (int l = 0; l < dimOfWorld; l++)
	      val += Lambda[i][k] * matrix[k][l] * Lambda[j][l];
	  val *= factor;
	  LALt[i][j] += val;
	  LALt[j][i] += val;
	}
      }    
    } else {	
      for (int i = 0; i < dim; i++) {
	for (int j = 0; j < dim; j++) {
	  double val = 0.0;
	  for (int k = 0; k < dimOfWorld; k++)
	    for (int l = 0; l < dimOfWorld; l++) 
	      val += Lambda[i][k] * matrix[k][l] * Lambda[j][l];
	  val *= factor;
	  LALt[i][j] += val;
	}
      }    
    }      
  }


  void SecondOrderTerm::lalt_kl(const DimVec<WorldVector<double> >& Lambda,
				int k, int l,
61
				mtl::dense2D<double>& LALt,
62
63
				double factor)
  {
64
    int dim = num_rows(LALt);
65
66
67
68
69
70
71

    for (int i = 0; i < dim; i++)
      for (int j = 0; j < dim; j++)
	LALt[i][j] += factor * Lambda[i][k] * Lambda[j][l];
  }


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  // ========== MatrixFct_SOT ==========

  MatrixFct_SOT::MatrixFct_SOT(DOFVectorBase<double> *dv, 
			       AbstractFunction<WorldMatrix<double>, double> *fct,
			       AbstractFunction<WorldVector<double>, WorldMatrix<double> > *div,
			       bool sym)
    : SecondOrderTerm(fct->getDegree()), 
      vec(dv), 
      matrixFct(fct), 
      divFct(div),
      symmetric(sym)
  {
    setSymmetric(symmetric);

    TEST_EXIT(dv)("No vector!\n");

88
    auxFeSpaces.insert(dv->getFeSpace());
89
90
91
92
93
94
  }

  void MatrixFct_SOT::initElement(const ElInfo* elInfo, 
				  SubAssembler* subAssembler,
				  Quadrature *quad) 
  {
95
    getVectorAtQPs(vec, elInfo, subAssembler, quad, vecAtQPs);
96
97
  }

98
99
  void MatrixFct_SOT::getLALt(const ElInfo *elInfo, 
			      vector<mtl::dense2D<double> > &LALt) const
100
  {
101
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
102
103
    const int nPoints = static_cast<int>(LALt.size());

104
    for (int iq = 0; iq < nPoints; iq++)
105
      lalt(grdLambda, (*matrixFct)(vecAtQPs[iq]), LALt[iq], symmetric, 1.0);
106
107
  }

108
109
110
111
112
113
  void MatrixFct_SOT::eval( int nPoints,
			    const mtl::dense_vector<double>& uhAtQP,
			    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			    mtl::dense_vector<double>& result,
			    double factor) 
114
115
116
117
118
119
120
121
  {
    int dow = Global::getGeo(WORLD);

    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;

      WorldMatrix<double> A = (*matrixFct)(vecAtQPs[iq]);

122
      if (num_rows(D2UhAtQP) > 0)
123
124
125
126
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];

127
      if (num_rows(grdUhAtQP) > 0)
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
	resultQP += (*divFct)(A) * grdUhAtQP[iq];

      result[iq] += resultQP * factor;
    }
  }

  void MatrixFct_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
			       std::vector<WorldVector<double> > &result)
  {
    int nPoints = grdUhAtQP.size();
    WorldMatrix<double> A;
    for (int iq = 0; iq < nPoints; iq++) {
      A = (*matrixFct)(vecAtQPs[iq]);
      result[iq] += A * grdUhAtQP[iq];
    }    
  }


  // ========== Matrix_SOT ==========

  void Matrix_SOT::eval(int nPoints,
149
150
151
152
			const mtl::dense_vector<double>& uhAtQP,
			const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			mtl::dense_vector<double>& result,
153
154
155
156
			double factor) 
  {
    int dow = Global::getGeo(WORLD);
  
157
    if (num_rows(D2UhAtQP) > 0) {
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
      for (int iq = 0; iq < nPoints; iq++) {
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += matrix[i][j] * D2UhAtQP[iq][j][i];	  
	
	result[iq] += resultQP * factor;
      }
    }
  }

  void Matrix_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
			    std::vector<WorldVector<double> > &result)
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++)
      result[iq] += matrix * grdUhAtQP[iq];
  }


  // ========== VecAtQP_SOT ==========

  VecAtQP_SOT::VecAtQP_SOT(DOFVectorBase<double> *dv, 
			   AbstractFunction<double, double> *af)
182
    : SecondOrderTerm(af ? af->getDegree() : dv->getFeSpace()->getBasisFcts()->getDegree()), vec(dv), f(af)
183
184
185
186
187
  {
    setSymmetric(true);

    TEST_EXIT(dv)("No vector!\n");

188
    auxFeSpaces.insert(dv->getFeSpace());
189
190
191
192
193
194
  }

  void VecAtQP_SOT::initElement(const ElInfo* elInfo, 
				SubAssembler* subAssembler,
				Quadrature* quad) 
  {
195
    getVectorAtQPs(vec, elInfo, subAssembler, quad, vecAtQPs);
196
197
198
199
200
201
202
  }

  void VecAtQP_SOT::initElement(const ElInfo* smallElInfo, 
				const ElInfo* largeElInfo,
				SubAssembler* subAssembler,
				Quadrature* quad) 
  {
203
    getVectorAtQPs(vec, smallElInfo, largeElInfo, subAssembler, quad, vecAtQPs);
204
205
  }

206
207
  void VecAtQP_SOT::getLALt(const ElInfo *elInfo, 
			    vector<mtl::dense2D<double> > &LALt) const 
208
  {
209
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
210
211
    const int nPoints = static_cast<int>(LALt.size());

212
213
214
215
216
217
218
    if (f) {
      for (int iq = 0; iq < nPoints; iq++)
	l1lt(grdLambda, LALt[iq], (*f)(vecAtQPs[iq]));
    } else {
      for (int iq = 0; iq < nPoints; iq++)
	l1lt(grdLambda, LALt[iq], vecAtQPs[iq]);
    }
219
220
  }

221
222
223
224
225
226
  void VecAtQP_SOT::eval( int nPoints,
			  const mtl::dense_vector<double>& uhAtQP,
			  const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			  const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			  mtl::dense_vector<double>& result,
			  double fac) 
227
228
229
  {
    int dow = Global::getGeo(WORLD);

230
    if (num_rows(D2UhAtQP) > 0) {
231
232
233
234
235
236
237
238
239
240
241
242
243
244
      if (f) {
	for (int iq = 0; iq < nPoints; iq++) {
	  double resultQP = 0.0;
	  for (int i = 0; i < dow; i++)
	    resultQP += D2UhAtQP[iq][i][i];
	  result[iq] += fac * (*f)(vecAtQPs[iq]) * resultQP;
	}
      } else {
	for (int iq = 0; iq < nPoints; iq++) {
	  double resultQP = 0.0;
	  for (int i = 0; i < dow; i++)
	    resultQP += D2UhAtQP[iq][i][i];
	  result[iq] += fac * vecAtQPs[iq] * resultQP;
	}
245
246
247
248
249
250
251
252
      }
    }
  }

  void VecAtQP_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
			     std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
253
254
255
256
257
258
259
260
261
262
    if (f) {
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*f)(vecAtQPs[iq]);
	axpy(factor, grdUhAtQP[iq], result[iq]);
      }
    } else {
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = vecAtQPs[iq];
	axpy(factor, grdUhAtQP[iq], result[iq]);
      }
263
264
265
266
267
268
269
270
    }
  }


  // ========== Vec2AtQP_SOT ==========

  Vec2AtQP_SOT::Vec2AtQP_SOT(DOFVectorBase<double> *dv1, DOFVectorBase<double> *dv2, 
			     BinaryAbstractFunction<double, double, double> *af)
Praetorius, Simon's avatar
Praetorius, Simon committed
271
    : SecondOrderTerm(af ? af->getDegree() : 2*dv1->getFeSpace()->getBasisFcts()->getDegree()), vec1(dv1), vec2(dv2), f(af)
272
273
274
275
276
277
  {
    setSymmetric(true);

    TEST_EXIT(dv1)("No first vector!\n");
    TEST_EXIT(dv2)("No second vector!\n");

278
279
    auxFeSpaces.insert(dv1->getFeSpace());
    auxFeSpaces.insert(dv2->getFeSpace());
280
281
282
283
284
285
  }

  void Vec2AtQP_SOT::initElement(const ElInfo* elInfo, 
				 SubAssembler* subAssembler,
				 Quadrature *quad) 
  {
286
287
    getVectorAtQPs(vec1, elInfo, subAssembler, quad, vecAtQPs1);
    getVectorAtQPs(vec2, elInfo, subAssembler, quad, vecAtQPs2);
288
289
  }

290
291
  void Vec2AtQP_SOT::getLALt(const ElInfo *elInfo, 
			     vector<mtl::dense2D<double> > &LALt) const 
292
  {
293
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
294
295
    const int nPoints = static_cast<int>(LALt.size());

Praetorius, Simon's avatar
Praetorius, Simon committed
296
297
298
299
300
301
    for (int iq = 0; iq < nPoints; iq++) {
      if (f)
	l1lt(grdLambda, LALt[iq], (*f)(vecAtQPs1[iq], vecAtQPs2[iq]));
      else
	l1lt(grdLambda, LALt[iq], vecAtQPs1[iq] * vecAtQPs2[iq]);
    }
302
303
304
  }

  void Vec2AtQP_SOT::eval(int nPoints,
305
306
307
308
			  const mtl::dense_vector<double>& uhAtQP,
			  const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			  const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			  mtl::dense_vector<double>& result,
309
310
311
312
			  double fac) 
  {
    int dow = Global::getGeo(WORLD);
    
313
    if (num_rows(D2UhAtQP) > 0) {
314
      for (int iq = 0; iq < nPoints; iq++) {
Praetorius, Simon's avatar
Praetorius, Simon committed
315
316
317
318
319
	double factor = 1.0;
	if (f)
	  factor = (*f)(vecAtQPs1[iq], vecAtQPs2[iq]);
	else
	  factor = vecAtQPs1[iq] * vecAtQPs2[iq];
320
321
322
323
324
325
326
327
328
329
330
331
332
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  resultQP += D2UhAtQP[iq][i][i];	
	result[iq] += fac * factor * resultQP;
      }
    }
  }
  
  void Vec2AtQP_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
			      std::vector<WorldVector<double> > &result)
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
Praetorius, Simon's avatar
Praetorius, Simon committed
333
334
335
336
337
      double factor = 1.0;
      if (f)
	factor = (*f)(vecAtQPs1[iq], vecAtQPs2[iq]);
      else
	factor = vecAtQPs1[iq] * vecAtQPs2[iq];
338
339
340
341
342
343
344
345
346
347
348
349
350
351
      axpy(factor, grdUhAtQP[iq], result[iq]);      
    }
  }


  // ========== CoordsAtQP_SOT ==========

  void CoordsAtQP_SOT::initElement(const ElInfo* elInfo, 
				   SubAssembler* subAssembler,
				   Quadrature *quad) 
  {
    coordsAtQPs = subAssembler->getCoordsAtQPs(elInfo, quad);
  }

352
353
  void CoordsAtQP_SOT::getLALt(const ElInfo *elInfo, 
			       vector<mtl::dense2D<double> > &LALt) const 
354
  {
355
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
356
357
    const int nPoints = static_cast<int>(LALt.size());

358
    for (int iq = 0; iq < nPoints; iq++)
359
      l1lt(grdLambda, LALt[iq], (*g)(coordsAtQPs[iq]));
360
361
362
  }

  void CoordsAtQP_SOT::eval(int nPoints,
363
364
365
366
			    const mtl::dense_vector<double>& uhAtQP,
			    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			    mtl::dense_vector<double>& result,
367
368
369
370
			    double f) 
  {
    int dow = Global::getGeo(WORLD);

371
    if (num_rows(D2UhAtQP) > 0) {
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*g)(coordsAtQPs[iq]);
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  resultQP += D2UhAtQP[iq][i][i];	
	result[iq] += factor * f * resultQP;
      }
    }
  }

  void CoordsAtQP_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      double factor = (*g)(coordsAtQPs[iq]);  
      axpy(factor, grdUhAtQP[iq], result[iq]);      
    }
  }


  // ========== MatrixGradient_SOT ==========

  MatrixGradient_SOT::MatrixGradient_SOT(DOFVectorBase<double> *dv,
					 AbstractFunction<WorldMatrix<double>, WorldVector<double> > *af,
					 AbstractFunction<WorldVector<double>, WorldMatrix<double> > *divAf,
					 bool symm) 
    : SecondOrderTerm(af->getDegree()), 
      vec(dv), 
      f(af), 
      divFct(divAf),
      symmetric(symm)
  {
    setSymmetric(symmetric);

    TEST_EXIT(dv)("No vector!\n");

409
    auxFeSpaces.insert(dv->getFeSpace());
410
411
412
413
414
415
  }

  void MatrixGradient_SOT::initElement(const ElInfo* elInfo, 
				       SubAssembler* subAssembler,
				       Quadrature *quad) 
  {
416
    getGradientsAtQPs(vec, elInfo, subAssembler, quad, gradAtQPs);
417
418
419
420
421
422
423
  }

  void MatrixGradient_SOT::initElement(const ElInfo* smallElInfo, 
				       const ElInfo* largeElInfo,
				       SubAssembler* subAssembler,
				       Quadrature *quad) 
  {
424
    getGradientsAtQPs(vec, smallElInfo, largeElInfo, subAssembler, quad, gradAtQPs);
425
426
  }

427
428
  void MatrixGradient_SOT::getLALt(const ElInfo *elInfo, 
				   vector<mtl::dense2D<double> > &LALt) const 
429
  {
430
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
431
432
    const int nPoints = static_cast<int>(LALt.size());

433
    for (int iq = 0; iq < nPoints; iq++)
434
      lalt(grdLambda, (*f)(gradAtQPs[iq]), LALt[iq], symmetric, 1.0);
435
436
437
  }

  void MatrixGradient_SOT::eval(int nPoints,
438
439
440
441
				const mtl::dense_vector<double>& uhAtQP,
				const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
				const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
				mtl::dense_vector<double>& result,
442
443
444
445
446
447
448
449
450
				double factor) 
  {
    int dow = Global::getGeo(WORLD);

    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;

      WorldMatrix<double> A = (*f)(gradAtQPs[iq]);

451
      if (num_rows(D2UhAtQP) > 0)
452
453
454
455
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];

456
      if (num_rows(grdUhAtQP) > 0)
457
458
459
460
461
462
463
464
465
466
467
468
469
	resultQP += (*divFct)(A) * grdUhAtQP[iq];      

      result[iq] += resultQP * factor;
    }
  }


  void MatrixGradient_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				    std::vector<WorldVector<double> > &result)
  {
    FUNCNAME("MatrixGradient_SOT::weakEval()");

    TEST_EXIT_DBG(f)("No function f!\n");
470
471
    TEST_EXIT_DBG(num_rows(gradAtQPs))
      ("Operator was not initialized correctly!\n");
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      tmpMat = (*f)(gradAtQPs[iq]);
      result[iq] += tmpMat * grdUhAtQP[iq];    
    }
  }


  // ========== FctGradient_SOT ==========

  FctGradient_SOT::FctGradient_SOT(DOFVectorBase<double> *dv,
				   AbstractFunction<double, WorldVector<double> > *af)
    : SecondOrderTerm(af->getDegree()), vec(dv), f(af)
  {
    TEST_EXIT(dv)("No vector!\n");

489
    auxFeSpaces.insert(dv->getFeSpace());
490
491
492
493
494
495
  }

  void FctGradient_SOT::initElement(const ElInfo* elInfo, 
				    SubAssembler* subAssembler,
				    Quadrature *quad) 
  {
496
    getGradientsAtQPs(vec, elInfo, subAssembler, quad, gradAtQPs);
497
498
  }

499
500
  void FctGradient_SOT::getLALt(const ElInfo *elInfo, 
				vector<mtl::dense2D<double> > &LALt) const 
501
  {
502
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
503
504
    const int nPoints = static_cast<int>(LALt.size());

505
    for (int iq = 0; iq < nPoints; iq++)
506
      l1lt(grdLambda, LALt[iq], (*f)(gradAtQPs[iq]));
507
508
  }

509
510
511
512
513
514
  void FctGradient_SOT::eval( int nPoints,
			      const mtl::dense_vector<double>& uhAtQP,
			      const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			      const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			      mtl::dense_vector<double>& result,
			      double fac) 
515
516
517
  {
    int dow = Global::getGeo(WORLD);

518
    if (num_rows(D2UhAtQP) > 0) {
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*f)(gradAtQPs[iq]);
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  resultQP += D2UhAtQP[iq][i][i];	
	result[iq] += fac * factor * resultQP;
      }
    }
  }

  void FctGradient_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				 std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      double factor = (*f)(gradAtQPs[iq]);
      axpy(factor, grdUhAtQP[iq], result[iq]);      
    }
  }


  // ========== VecMatrixGradientAtQP_SOT ==========
  
  VecMatrixGradientAtQP_SOT::VecMatrixGradientAtQP_SOT(DOFVectorBase<double> *dv,
						       BinaryAbstractFunction<WorldMatrix<double>, double, WorldVector<double> > *af,
						       AbstractFunction<WorldVector<double>, WorldMatrix<double> > *divAf,
						       bool symm)
    : SecondOrderTerm(af->getDegree()), vec(dv), f(af), 
      divFct(divAf), symmetric(symm)
  {
    setSymmetric(symmetric);

    TEST_EXIT(dv)("No vector!\n");

553
    auxFeSpaces.insert(dv->getFeSpace());
554
555
556
557
558
559
  }

  void VecMatrixGradientAtQP_SOT::initElement(const ElInfo* elInfo, 
					      SubAssembler* subAssembler,
					      Quadrature *quad) 
  {
560
    getVectorAtQPs(vec, elInfo, subAssembler, quad, vecAtQPs);
561
    getGradientsAtQPs(vec, elInfo, subAssembler, quad, gradAtQPs);
562
563
  }

564
565
  void VecMatrixGradientAtQP_SOT::getLALt(const ElInfo *elInfo,
					  vector<mtl::dense2D<double> > &LALt) const 
566
  {
567
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
568
569
    const int nPoints = static_cast<int>(LALt.size());

570
    for (int iq = 0; iq < nPoints; iq++)
571
      lalt(grdLambda, (*f)(vecAtQPs[iq], gradAtQPs[iq]), LALt[iq], symmetric, 1.0);
572
573
  }

574
575
576
577
578
579
  void VecMatrixGradientAtQP_SOT::eval( int nPoints,
					const mtl::dense_vector<double>& uhAtQP,
					const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
					const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
					mtl::dense_vector<double>& result,
				        double factor) 
580
581
582
583
584
585
586
587
  {
    int dow = Global::getGeo(WORLD);
    
    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;
      
      WorldMatrix<double> A = (*f)(vecAtQPs[iq], gradAtQPs[iq]);
      
588
      if (num_rows(D2UhAtQP) > 0)
589
590
591
592
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];
      
593
      if (num_rows(grdUhAtQP) > 0)
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
	resultQP += (*divFct)(A) * grdUhAtQP[iq];      
      
      result[iq] += resultQP * factor;
    }
  }

  void VecMatrixGradientAtQP_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
					   std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    WorldMatrix<double> A;
    for (int iq = 0; iq < nPoints; iq++) {
      A = (*f)(vecAtQPs[iq], gradAtQPs[iq]);
      result[iq] += A * grdUhAtQP[iq];      
    }
  }


  // ========== VecGradCoordsAtQP_SOT ==========

  VecGradCoordsAtQP_SOT::VecGradCoordsAtQP_SOT(DOFVectorBase<double> *dv,
					       TertiaryAbstractFunction<double, double,
					       WorldVector<double>, WorldVector<double> > *af)
    : SecondOrderTerm(af->getDegree()), vec(dv), f(af)
  {
    TEST_EXIT(dv)("No vector!\n");

621
    auxFeSpaces.insert(dv->getFeSpace());
622
623
624
625
626
627
  }

  void VecGradCoordsAtQP_SOT::initElement(const ElInfo* elInfo, 
					  SubAssembler* subAssembler,
					  Quadrature *quad) 
  {
628
    getVectorAtQPs(vec, elInfo, subAssembler, quad, vecAtQPs);
629
    getGradientsAtQPs(vec, elInfo, subAssembler, quad, gradAtQPs);
630
631
632
    coordsAtQPs = subAssembler->getCoordsAtQPs(elInfo, quad);  
  }

633
634
  void VecGradCoordsAtQP_SOT::getLALt(const ElInfo *elInfo, 
				      vector<mtl::dense2D<double> > &LALt) const 
635
  {
636
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
637
638
    const int nPoints = static_cast<int>(LALt.size());

639
    for (int iq = 0; iq < nPoints; iq++)
640
      l1lt(grdLambda, LALt[iq], (*f)(vecAtQPs[iq], gradAtQPs[iq], coordsAtQPs[iq]));
641
642
  }

643
644
645
646
647
648
  void VecGradCoordsAtQP_SOT::eval( int nPoints,
				    const mtl::dense_vector<double>& uhAtQP,
				    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
				    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
				    mtl::dense_vector<double>& result,
				    double fac) 
649
650
651
  {
    int  dow = Global::getGeo(WORLD);

652
    if (num_rows(D2UhAtQP) > 0) {
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*f)(vecAtQPs[iq], gradAtQPs[iq], coordsAtQPs[iq]);
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  resultQP += D2UhAtQP[iq][i][i];	
	result[iq] += fac * factor * resultQP;
      }
    }
  }

  void VecGradCoordsAtQP_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				       std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      double factor = (*f)(vecAtQPs[iq], gradAtQPs[iq], coordsAtQPs[iq]);
      axpy(factor, grdUhAtQP[iq], result[iq]);    
    }
  }


  // ========== VecAndCoordsAtQP_SOT ==========

  VecAndCoordsAtQP_SOT::VecAndCoordsAtQP_SOT(DOFVectorBase<double> *dv, 
					     BinaryAbstractFunction<double, double, WorldVector<double> > *af)
    : SecondOrderTerm(af->getDegree()), vec(dv), f(af)
  {
    setSymmetric(true);

    TEST_EXIT(dv)("No vector!\n");

684
    auxFeSpaces.insert(dv->getFeSpace());
685
686
687
688
689
690
  }

  void VecAndCoordsAtQP_SOT::initElement(const ElInfo* elInfo, 
					 SubAssembler* subAssembler,
					 Quadrature *quad) 
  {
691
    getVectorAtQPs(vec, elInfo, subAssembler, quad, vecAtQPs);
692
693
694
    coordsAtQPs = subAssembler->getCoordsAtQPs(elInfo, quad);
  }

695
696
  void VecAndCoordsAtQP_SOT::getLALt(const ElInfo *elInfo, 
				     vector<mtl::dense2D<double> > &LALt) const 
697
  { 
698
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
699
700
    const int nPoints = static_cast<int>(LALt.size());

701
    for (int iq = 0; iq < nPoints; iq++)
702
      l1lt(grdLambda, LALt[iq], (*f)(vecAtQPs[iq], coordsAtQPs[iq]));
703
704
705
  }

  void VecAndCoordsAtQP_SOT::eval(int nPoints,
706
707
708
709
				  const mtl::dense_vector<double>& uhAtQP,
				  const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
				  const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
				  mtl::dense_vector<double>& result,
710
711
712
713
				  double fac) 
  {
    int dow = Global::getGeo(WORLD);

714
    if (num_rows(D2UhAtQP) > 0) {
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*f)(vecAtQPs[iq], coordsAtQPs[iq]);
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  resultQP += D2UhAtQP[iq][i][i];	
	result[iq] += fac * factor * resultQP;
      }
    }
  }

  void VecAndCoordsAtQP_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				      std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      double factor = (*f)(vecAtQPs[iq], coordsAtQPs[iq]);  
      axpy(factor, grdUhAtQP[iq], result[iq]);    
    }
  }


  // ========== MatrixGradientAndCoords_SOT ==========
  
  MatrixGradientAndCoords_SOT::MatrixGradientAndCoords_SOT(DOFVectorBase<double> *dv,
							   BinaryAbstractFunction<WorldMatrix<double>,
							   WorldVector<double>, WorldVector<double> > *af,
							   AbstractFunction<WorldVector<double>,
							   WorldMatrix<double> > *divAf,
							   bool symm) 
    : SecondOrderTerm(af->getDegree()), vec(dv), f(af), divFct(divAf), symmetric(symm)
  {
    setSymmetric(symmetric);

    TEST_EXIT(dv)("No vector!\n");

750
    auxFeSpaces.insert(dv->getFeSpace());
751
752
753
754
755
756
  }

  void MatrixGradientAndCoords_SOT::initElement(const ElInfo* elInfo, 
						SubAssembler* subAssembler,
						Quadrature *quad) 
  {
757
    getGradientsAtQPs(vec, elInfo, subAssembler, quad, gradAtQPs);
758
759
760
    coordsAtQPs = subAssembler->getCoordsAtQPs(elInfo, quad);  
  }

761
762
  void MatrixGradientAndCoords_SOT::getLALt(const ElInfo *elInfo, 
					    vector<mtl::dense2D<double> > &LALt) const 
763
  {
764
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
765
766
    const int nPoints = static_cast<int>(LALt.size());

767
    for (int iq = 0; iq < nPoints; iq++)
768
      lalt(grdLambda, (*f)(gradAtQPs[iq], coordsAtQPs[iq]), LALt[iq], symmetric, 1.0);
769
770
  }

771
772
773
774
775
776
  void MatrixGradientAndCoords_SOT::eval( int nPoints,
					  const mtl::dense_vector<double>& uhAtQP,
					  const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
					  const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
					  mtl::dense_vector<double>& result,
					  double factor) 
777
778
779
780
781
782
783
784
  {
    int dow = Global::getGeo(WORLD);

    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;

      WorldMatrix<double> A = (*f)(gradAtQPs[iq], coordsAtQPs[iq]);

785
      if (num_rows(D2UhAtQP) > 0)
786
787
788
789
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];

790
      if (num_rows(grdUhAtQP) > 0)
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
	resultQP += (*divFct)(A) * grdUhAtQP[iq];
      
      result[iq] += resultQP * factor;
    }
  }

  void MatrixGradientAndCoords_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
					     std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    WorldMatrix<double> A;
    for (int iq = 0; iq < nPoints; iq++) {
      A = (*f)(gradAtQPs[iq], coordsAtQPs[iq]);
      result[iq] += A * grdUhAtQP[iq];      
    }
  }

  
  // ========== MatrixVec2_SOT ==========

  MatrixVec2_SOT::MatrixVec2_SOT(DOFVectorBase<double> *dv1, DOFVectorBase<double> *dv2, 
			       BinaryAbstractFunction<double, double, double> *f,
			       WorldMatrix<double> Af,
			       bool sym)
    : SecondOrderTerm(f->getDegree()), 
      vec1(dv1),
      vec2(dv2), 
      fct(f), 
      A(Af),
      symmetric(sym)
  {
    FUNCNAME("MatrixVec2_SOT::MatrixVec2_SOT()");

    setSymmetric(symmetric);

    TEST_EXIT_DBG(dv1)("No vector!\n");
    TEST_EXIT_DBG(dv2)("No vector!\n");
    
829
830
    auxFeSpaces.insert(dv1->getFeSpace());
    auxFeSpaces.insert(dv2->getFeSpace());
831
832
833
834
835
836
  }

  void MatrixVec2_SOT::initElement(const ElInfo* elInfo, 
				  SubAssembler* subAssembler,
				  Quadrature *quad) 
  {
837
838
    getVectorAtQPs(vec1, elInfo, subAssembler, quad, vec1AtQPs);
    getVectorAtQPs(vec2, elInfo, subAssembler, quad, vec2AtQPs);
839
840
  }
  
841
842
  void MatrixVec2_SOT::getLALt(const ElInfo *elInfo, 
			       vector<mtl::dense2D<double> > &LALt) const 
843
  {      
844
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
845
846
    const int nPoints = static_cast<int>(LALt.size());

847
    for (int iq = 0; iq < nPoints; iq++)
848
      lalt(grdLambda, A, LALt[iq], symmetric, (*fct)(vec1AtQPs[iq], vec2AtQPs[iq]));
849
850
851
  }

  void MatrixVec2_SOT::eval(int nPoints,
852
853
854
855
			    const mtl::dense_vector<double>& uhAtQP,
			    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			    mtl::dense_vector<double>& result,
856
857
858
859
860
861
862
			    double factor) 
  {
    int dow = Global::getGeo(WORLD);
    
    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;

863
      if (num_rows(D2UhAtQP) > 0)
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];
    
      result[iq] += resultQP * factor * (*fct)(vec1AtQPs[iq], vec2AtQPs[iq]);
    }
  }

  void MatrixVec2_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				std::vector<WorldVector<double> > &result)
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++)
      result[iq] += A * grdUhAtQP[iq] * (*fct)(vec1AtQPs[iq], vec2AtQPs[iq]);
  }

  
  // ========== General_SOT ==========

  General_SOT::General_SOT(std::vector<DOFVectorBase<double>*> vecs,
			   std::vector<DOFVectorBase<double>*> grads,
			   TertiaryAbstractFunction<WorldMatrix<double>, 
  			     WorldVector<double>,
			     std::vector<double>, 
			     std::vector<WorldVector<double> > > *f,
			   AbstractFunction<WorldVector<double>, 
			     WorldMatrix<double> > *divFct,
			     bool symmetric)
      : SecondOrderTerm(f->getDegree()),
	vecs_(vecs),
	grads_(grads),
	f_(f),
	divFct_(divFct),
	symmetric_(symmetric)
  {
899
    vecsAtQPs.resize(vecs_.size());
900
901
902
903
904
    gradsAtQPs_.resize(grads_.size());

    for (int i = 0; i < static_cast<int>(vecs.size()); i++) {
      TEST_EXIT(vecs[i])("One vector is NULL!\n");

905
      auxFeSpaces.insert(vecs[i]->getFeSpace());
906
907
908
909
910
    }   

    for (int i = 0; i < static_cast<int>(grads.size()); i++) {
      TEST_EXIT(grads[i])("One gradient vector is NULL!\n");

911
      auxFeSpaces.insert(grads[i]->getFeSpace());
912
913
914
915
916
917
918
919
920
921
922
923
924
    }
  }

  void General_SOT::initElement(const ElInfo* elInfo, 
				SubAssembler* subAssembler,
				Quadrature *quad)
  {
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());

    coordsAtQPs_ = subAssembler->getCoordsAtQPs(elInfo, quad);

    for (int i = 0; i < nVecs; i++)
925
      getVectorAtQPs(vecs_[i], elInfo, subAssembler, quad, vecsAtQPs[i]);
926
    for (int i = 0; i < nGrads; i++)
927
      getGradientsAtQPs(grads_[i], elInfo, subAssembler, quad, gradsAtQPs_[i]);
928
929
930
  }

  void General_SOT::getLALt(const ElInfo *elInfo, 
931
			    vector<mtl::dense2D<double> > &LALt) const
932
933
934
  {
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());
935
    const int nPoints = static_cast<int>(LALt.size());
936

937
938
    vector<double> vecsArg(nVecs);
    vector<WorldVector<double> > gradsArg(nGrads);
939

940
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
941
942
943

    for (int iq = 0; iq < nPoints; iq++) {
      for (int i = 0; i < nVecs; i++)
944
	vecsArg[i] = vecsAtQPs[i][iq];      
945
946
947
      for (int i = 0; i < nGrads; i++) 
	gradsArg[i] = gradsAtQPs_[i][iq];      

948
      lalt(grdLambda, (*f_)(coordsAtQPs_[iq], vecsArg, gradsArg), 
949
	   LALt[iq], symmetric_, 1.0);
950
951
952
    }
  }

953
954
955
956
957
958
  void General_SOT::eval( int nPoints,
			  const mtl::dense_vector<double>& uhAtQP,
			  const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			  const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			  mtl::dense_vector<double>& result,
			  double factor) 
959
960
961
962
963
964
965
966
967
968
969
970
  {
    int dow = Global::getGeo(WORLD);
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());

    std::vector<double> vecsArg(nVecs);
    std::vector<WorldVector<double> > gradsArg(nGrads);

    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;

      for (int i = 0; i < nVecs; i++)
971
	vecsArg[i] = vecsAtQPs[i][iq];
972
973
974
975
976
      for (int i = 0; i < nGrads; i++)
	gradsArg[i] = gradsAtQPs_[i][iq];

      WorldMatrix<double> A = (*f_)(coordsAtQPs_[iq], vecsArg, gradsArg);

977
      if (num_rows(D2UhAtQP) > 0) {
978
979
980
981
982
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];
      }

983
      if (num_rows(grdUhAtQP) > 0)
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
	resultQP += (*divFct_)(A) * grdUhAtQP[iq];      

      result[iq] += resultQP * factor;
    }
  }

  void General_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
			     std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());

    std::vector<double> vecsArg(nVecs);
    std::vector<WorldVector<double> > gradsArg(nGrads);

    WorldMatrix<double> A;
    for (int iq = 0; iq < nPoints; iq++) {
      for (int i = 0; i < nVecs; i++)
1003
	vecsArg[i] = vecsAtQPs[i][iq];	
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
      for (int i = 0; i < nGrads; i++)
	gradsArg[i] = gradsAtQPs_[i][iq];
      
      A = (*f_)(coordsAtQPs_[iq], vecsArg, gradsArg);
      result[iq] += A * grdUhAtQP[iq];      
    }
  }


  // ========== GeneralParametric_SOT ==========

  GeneralParametric_SOT::GeneralParametric_SOT(std::vector<DOFVectorBase<double>*> vecs,
					       std::vector<DOFVectorBase<double>*> grads,
					       QuartAbstractFunction<WorldMatrix<double>, 
					         WorldVector<double>,
					         WorldVector<double>,
					         std::vector<double>, 
					         std::vector<WorldVector<double> > > *f,
					       AbstractFunction<WorldVector<double>, 
					         WorldMatrix<double> > *divFct,
					       bool symmetric)
    : SecondOrderTerm(f->getDegree()),
      vecs_(vecs),
      grads_(grads),
      f_(f),
      divFct_(divFct),
      symmetric_(symmetric)
  {
1032
    vecsAtQPs.resize(vecs_.size());
1033
1034
1035
1036
1037
    gradsAtQPs_.resize(grads_.size());

    for (int i = 0; i < static_cast<int>(vecs.size()); i++) {
      TEST_EXIT(vecs[i])("One vector is NULL!\n");

1038
      auxFeSpaces.insert(vecs[i]->getFeSpace());
1039
1040
1041
1042
1043
    }   

    for (int i = 0; i < static_cast<int>(grads.size()); i++) {
      TEST_EXIT(grads[i])("One gradient vector is NULL!\n");

1044
      auxFeSpaces.insert(grads[i]->getFeSpace());
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
    }
  }

  void GeneralParametric_SOT::initElement(const ElInfo* elInfo, 
					  SubAssembler* subAssembler,
					  Quadrature *quad)
  {
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());

    elInfo->getElementNormal(elementNormal_);
    coordsAtQPs_ = subAssembler->getCoordsAtQPs(elInfo, quad);

    for (int i = 0; i < nVecs; i++)
1059
      getVectorAtQPs(vecs_[i], elInfo, subAssembler, quad, vecsAtQPs[i]);
1060
    for (int i = 0; i < nGrads; i++)
1061
      getGradientsAtQPs(grads_[i], elInfo, subAssembler, quad, gradsAtQPs_[i]);
1062
1063
  }

1064
1065
  void GeneralParametric_SOT::getLALt(const ElInfo *elInfo, 
				      vector<mtl::dense2D<double> > &LALt) const
1066
1067
1068
  {
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());
1069
    const int nPoints = static_cast<int>(LALt.size());
1070

1071
1072
    vector<double> vecsArg(nVecs);
    vector<WorldVector<double> > gradsArg(nGrads);
1073

1074
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
1075
1076
1077

    for (int iq = 0; iq < nPoints; iq++) {
      for (int i = 0; i < nVecs; i++)
1078
	vecsArg[i] = vecsAtQPs[i][iq];      
1079
1080
      for (int i = 0; i < nGrads; i++)
	gradsArg[i] = gradsAtQPs_[i][iq];      
1081
      lalt(grdLambda, (*f_)(coordsAtQPs_[iq], elementNormal_, vecsArg, gradsArg), 
1082
	   LALt[iq], symmetric_, 1.0);
1083
1084
1085
    }
  }

1086
1087
1088
1089
1090
1091
  void GeneralParametric_SOT::eval( int nPoints,
				    const mtl::dense_vector<double>& uhAtQP,
				    const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
				    const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
				    mtl::dense_vector<double>& result,
				    double factor) 
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
  {
    int dow = Global::getGeo(WORLD);
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());

    std::vector<double> vecsArg(nVecs);
    std::vector<WorldVector<double> > gradsArg(nGrads);

    for (int iq = 0; iq < nPoints; iq++) {
      double resultQP = 0.0;

      for (int i = 0; i < nVecs; i++)
1104
	vecsArg[i] = vecsAtQPs[i][iq];      
1105
1106
1107
1108
1109
1110
      for (int i = 0; i < nGrads; i++)
	gradsArg[i] = gradsAtQPs_[i][iq];      

      WorldMatrix<double> A = 
	(*f_)(coordsAtQPs_[iq],  elementNormal_, vecsArg, gradsArg);

1111
      if (num_rows(D2UhAtQP) > 0) {
1112
1113
1114
1115
1116
	for (int i = 0; i < dow; i++)
	  for (int j = 0; j < dow; j++)
	    resultQP += A[i][j] * D2UhAtQP[iq][j][i];
      }

1117
      if (num_rows(grdUhAtQP) > 0)
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
	resultQP += (*divFct_)(A) * grdUhAtQP[iq];      

      result[iq] += resultQP * factor;
    }
  }

  void GeneralParametric_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				       std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    int nVecs = static_cast<int>(vecs_.size());
    int nGrads = static_cast<int>(grads_.size());

    std::vector<double> vecsArg(nVecs);
    std::vector<WorldVector<double> > gradsArg(nGrads);

    WorldMatrix<double> A;
    for (int iq = 0; iq < nPoints; iq++) {
      for (int i = 0; i < nVecs; i++)
1137
	vecsArg[i] = vecsAtQPs[i][iq];	
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
      for (int i = 0; i < nGrads; i++)
	gradsArg[i] = gradsAtQPs_[i][iq];
      
      A = (*f_)(coordsAtQPs_[iq],  elementNormal_, vecsArg, gradsArg);
      result[iq] += A * grdUhAtQP[iq];    
    }
  }


  // ========== VecAndGradAtQP_SOT ==========

  VecAndGradAtQP_SOT::VecAndGradAtQP_SOT(DOFVectorBase<double> *dv,
					 BinaryAbstractFunction<double, double, WorldVector<double> > *af)
    : SecondOrderTerm(af->getDegree()), vec(dv), f(af) 
  {
    TEST_EXIT(dv)("No vector!\n");

1155
    auxFeSpaces.insert(dv->getFeSpace());
1156
1157
1158
1159
1160
1161
  }

  void VecAndGradAtQP_SOT::initElement(const ElInfo* elInfo, 
				       SubAssembler* subAssembler,
				       Quadrature *quad) 
  {
1162
    getVectorAtQPs(vec, elInfo, subAssembler, quad, vecAtQPs);
1163
    getGradientsAtQPs(vec, elInfo, subAssembler, quad, gradAtQPs);
1164
1165
  }

1166
1167
  void VecAndGradAtQP_SOT::getLALt(const ElInfo *elInfo, 
				   vector<mtl::dense2D<double> > &LALt) const 
1168
  {
1169
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
1170
1171
    const int nPoints = static_cast<int>(LALt.size());

1172
    for (int iq = 0; iq < nPoints; iq++)
1173
      l1lt(grdLambda, LALt[iq], (*f)(vecAtQPs[iq], gradAtQPs[iq]));
1174
1175
1176
  }

  void VecAndGradAtQP_SOT::eval(int nPoints,
1177
1178
1179
1180
				const mtl::dense_vector<double>& uhAtQP,
				const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
				const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
				mtl::dense_vector<double>& result,
1181
1182
1183
1184
				double fac) 
  {
    int dow = Global::getGeo(WORLD);

1185
    if (num_rows(D2UhAtQP) > 0) {
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*f)(vecAtQPs[iq], gradAtQPs[iq]);
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  resultQP += D2UhAtQP[iq][i][i];
	
	result[iq] += fac * resultQP * factor;
      }
    }
  }

  void VecAndGradAtQP_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				    std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      double factor = (*f)(vecAtQPs[iq], gradAtQPs[iq]);  
      axpy(factor, grdUhAtQP[iq], result[iq]);      
    }
  }

  
  // ========== VecGrad_SOT ==========

  void VecGrad_SOT::initElement(const ElInfo* elInfo, 
				SubAssembler* subAssembler,
				Quadrature *quad) 
  {
1214
    subAssembler->getVectorAtQPs(vec1, elInfo, quad, vecAtQPs);
1215
    subAssembler->getGradientsAtQPs(vec2, elInfo, quad, gradAtQPs);
1216
1217
  }

1218
1219
  void VecGrad_SOT::getLALt(const ElInfo *elInfo,
			    vector<mtl::dense2D<double> > &LALt) const 
1220
  {
1221
    const DimVec<WorldVector<double> > &grdLambda = elInfo->getGrdLambda();
1222
1223
    const int nPoints = static_cast<int>(LALt.size());

1224
    for (int iq = 0; iq < nPoints; iq++)
1225
      l1lt(grdLambda, LALt[iq], (*f)(vecAtQPs[iq], gradAtQPs[iq]));    
1226
1227
  }

1228
1229
1230
1231
1232
1233
  void VecGrad_SOT::eval( int nPoints,
			  const mtl::dense_vector<double>& uhAtQP,
			  const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
			  const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
			  mtl::dense_vector<double>& result,
			  double fac) 
1234
1235
1236
  {
    int dow = Global::getGeo(WORLD);

1237
    if (num_rows(D2UhAtQP) > 0) {
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*f)(vecAtQPs[iq], gradAtQPs[iq]);
	double resultQP = 0.0;
	for (int i = 0; i < dow; i++)
	  resultQP += D2UhAtQP[iq][i][i];	
	result[iq] += fac * resultQP * factor;
      }
    }
  }

  void VecGrad_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
			     std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      double factor = (*f)(vecAtQPs[iq], gradAtQPs[iq]);  
      axpy(factor, grdUhAtQP[iq], result[iq]);
    }
  }

  
  // ========== CoordsAtQP_IJ_SOT ==========

  void CoordsAtQP_IJ_SOT::initElement(const ElInfo* elInfo, 
				      SubAssembler* subAssembler,
				      Quadrature *quad)
  {
    coordsAtQPs = subAssembler->getCoordsAtQPs(elInfo, quad);
  }

  void CoordsAtQP_IJ_SOT::getLALt(const ElInfo *elInfo, 
1269
				  vector<mtl::dense2D<double> > &LALt) const
1270
1271
  {
    const DimVec<WorldVector<double> > Lambda = elInfo->getGrdLambda();
1272
1273
    const int nPoints = static_cast<int>(LALt.size());

1274
    for (int iq = 0; iq < nPoints; iq++)
1275
      lalt_kl(Lambda, xi, xj, LALt[iq], (*g)(coordsAtQPs[iq]));
1276
1277
  }

1278
1279
1280
1281
1282
1283
  void CoordsAtQP_IJ_SOT::eval( int nPoints,
				const mtl::dense_vector<double>& uhAtQP,
				const mtl::dense_vector<WorldVector<double> >& grdUhAtQP,
				const mtl::dense_vector<WorldMatrix<double> >& D2UhAtQP,
				mtl::dense_vector<double>& result,
			        double f) 
1284
  {
1285
    if (num_rows(D2UhAtQP) > 0) {
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
      for (int iq = 0; iq < nPoints; iq++) {
	double factor = (*g)(coordsAtQPs[iq]);
	result[iq] += D2UhAtQP[iq][xi][xj] * factor * f;
      }
    }
  }

  void CoordsAtQP_IJ_SOT::weakEval(const std::vector<WorldVector<double> > &grdUhAtQP,
				   std::vector<WorldVector<double> > &result) 
  {
    int nPoints = grdUhAtQP.size();
    for (int iq = 0; iq < nPoints; iq++) {
      double factor = (*g)(coordsAtQPs[iq]);  
      result[iq][xi] += grdUhAtQP[iq][xj] * factor;      
    }
  }

  
  // ========== VecAtQP_IJ_SOT ==========
  
  VecAtQP_IJ_SOT::VecAtQP_IJ_SOT(DOFVectorBase<double> *dv, 
				 AbstractFunction<double, double> *af,
				 int x_i, int x_j)
1309
    : SecondOrderTerm(af ? af->getDegree() : dv->getFeSpace()->getBasisFcts()->getDegree()), vec(dv), f(af), xi(x_i), xj(x_j)
1310
1311
1312
1313
1314
  {
    setSymmetric(xi == xj);

    TEST_EXIT(dv)("No vector!\n");

1315
    auxFeSpaces.insert(dv->getFeSpace());
Thomas Witkowski's avatar