-
Notifications
You must be signed in to change notification settings - Fork 5
/
Poisson.C
677 lines (544 loc) · 16.9 KB
/
Poisson.C
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
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
// $Id$
//==============================================================================
//!
//! \file Poisson.C
//!
//! \date Apr 16 2010
//!
//! \author Einar Christensen / SINTEF
//!
//! \brief Integrand implementations for Poisson problems.
//!
//==============================================================================
#include "Fields.h"
#include "Poisson.h"
#include "AnaSol.h"
#include "ElmMats.h"
#include "ElmNorm.h"
#include "ExprFunctions.h"
#include "Function.h"
#include "FiniteElement.h"
#include "GlobalIntegral.h"
#include "Vec3Oper.h"
#include "VTF.h"
// TODO: Are all these really necessary??
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ext/alloc_traits.h>
#include <iostream>
#include <strings.h>
#include "tinyxml2.h"
#include <utility>
Poisson::Poisson (unsigned short int n) : IntegrandBase(n)
{
kappaC = 1.0;
fluxFld = heatSrc = kappaF = nullptr;
tracFld = nullptr;
reacInt = nullptr;
dualRHS = nullptr;
extEner = false;
normIntegrandType = ELEMENT_CORNERS;
}
bool Poisson::parse (const tinyxml2::XMLElement* elem)
{
if (!elem) return false;
if (elem->FirstChildElement("residual"))
normIntegrandType |= SECOND_DERIVATIVES;
else if (strcasecmp(elem->Value(),"galerkin"))
return false;
else if (elem->FirstChild() && elem->FirstChild()->Value())
galerkin.push_back(new VecFuncExpr(elem->FirstChild()->Value()));
return true;
}
double Poisson::getHeat (const Vec3& X) const
{
return heatSrc ? (*heatSrc)(X) : 0.0;
}
double Poisson::getFlux (const Vec3& X, const Vec3& n) const
{
if (fluxFld)
return (*fluxFld)(X);
else if (tracFld)
return (*tracFld)(X)*n;
else
return 0.0;
}
void Poisson::setMode (SIM::SolutionMode mode)
{
m_mode = mode;
if (mode == SIM::STATIC || mode == SIM::NORMS)
primsol.resize(1+dualFld.size());
else if (mode >= SIM::RHS_ONLY)
primsol.resize(1);
else
primsol.clear();
#ifdef INT_DEBUG
std::cout <<"Poisson::setMode: "<< m_mode
<<" size(primsol) = "<< primsol.size() << std::endl;
#endif
}
void Poisson::addExtrFunction (FunctionBase* extr)
{
if (dynamic_cast<RealFunc*>(extr))
{
dualFld.push_back(extr);
if (!dualRHS)
dualRHS = extr;
}
else
{
dualFld.clear();
dualRHS = nullptr;
}
}
bool Poisson::initElement (const std::vector<int>& MNPC,
const FiniteElement&, const Vec3& XC,
size_t, LocalIntegral& elmInt)
{
if (!this->IntegrandBase::initElement(MNPC,elmInt))
return false;
for (size_t i = 1; i < elmInt.vec.size(); i++)
{
FunctionBase* extrFunc = nullptr;
if (i == 1 && m_mode == SIM::STATIC)
extrFunc = dualRHS;
else if (i <= dualFld.size() && m_mode >= SIM::RECOVERY)
extrFunc = dualFld[i-1];
if (extrFunc && !extrFunc->inDomain(XC))
elmInt.vec[i].clear(); // Erase extraction field values if outside domain
#ifdef INT_DEBUG
else
std::cout <<"Poisson::initElement: Point "<< XC
<<" is inside domain "<< i << std::endl;
#endif
}
return true;
}
void Poisson::setSecondaryInt (GlobalIntegral* gq)
{
delete reacInt;
reacInt = gq;
}
GlobalIntegral& Poisson::getGlobalInt (GlobalIntegral* gq) const
{
if (m_mode == SIM::RHS_ONLY && reacInt)
return *reacInt;
return this->IntegrandBase::getGlobalInt(gq);
}
LocalIntegral* Poisson::getLocalIntegral (size_t nen, size_t,
bool neumann) const
{
ElmMats* result = new ElmMats();
switch (m_mode)
{
case SIM::STATIC:
result->rhsOnly = neumann;
result->withLHS = !neumann;
result->resize(neumann ? 0 : 1,
neumann ? 1 : (dualRHS ? 2 : 1+galerkin.size()));
break;
case SIM::VIBRATION:
result->resize(1,0);
break;
case SIM::RHS_ONLY:
result->resize(neumann ? 0 : 1, 1);
case SIM::RECOVERY:
result->rhsOnly = true;
result->withLHS = false;
break;
default:
;
}
result->redim(nen);
return result;
}
bool Poisson::evalInt (LocalIntegral& elmInt, const FiniteElement& fe,
const Vec3& X) const
{
ElmMats& elMat = static_cast<ElmMats&>(elmInt);
// Conductivity scaled by integration point weight at this point
double cw = this->getMaterial(X)*fe.detJxW;
if (!elMat.A.empty())
// Integrate the coefficient matrix // EK += kappa * dNdX * dNdX^T * |J|*w
elMat.A.front().multiply(fe.dNdX,fe.dNdX,false,true,true,cw);
// Lambda function for integration of the internal force vector
auto&& evalIntForce = [cw,fe](Vector& S, const Vector& eV)
{
Vector tmp;
// S -= dNdX * (dNdX^t * eV) * cw
return fe.dNdX.multiply(eV,tmp,true) && fe.dNdX.multiply(tmp,S,-cw,1.0);
};
if (!elMat.b.empty())
{
// Integrate heat source, if defined
if (heatSrc)
elMat.b.front().add(fe.N,(*heatSrc)(X)*fe.detJxW); // EV += N*h(x)*|J|*w
if (m_mode == SIM::RHS_ONLY && !elmInt.vec.empty())
// Integrate the internal forces based on current solution
if (!evalIntForce(elMat.b.front(),elmInt.vec.front()))
return false;
}
if (dualRHS && elmInt.vec.size() > 1)
{
if (elmInt.vec[1].empty())
return true; // the extraction function is zero in this element
// Integrate the dual load vector
return evalIntForce(elMat.b[1],elmInt.vec[1]);
}
// Galerkin projections a(u^h,v^h) = a(Pu,v^h) = a(w,v^h)
bool ok = true;
for (size_t a = 1; a <= galerkin.size() && a < elMat.b.size() && ok; a++)
{
Vec3 Gw = (*galerkin[a-1])(X) * fe.detJxW;
ok = fe.dNdX.multiply(Gw.vec(nsd),elMat.b[a],false,true); // b += dNdX * Gw
}
return ok;
}
bool Poisson::evalBou (LocalIntegral& elmInt, const FiniteElement& fe,
const Vec3& X, const Vec3& normal) const
{
if (!tracFld && !fluxFld)
{
std::cerr <<" *** Poisson::evalBou: No heat flux."<< std::endl;
return false;
}
ElmMats& elMat = static_cast<ElmMats&>(elmInt);
if (elMat.b.empty())
{
std::cerr <<" *** Poisson::evalBou: No load vector."<< std::endl;
return false;
}
// Evaluate the Neumann value h = -q(X)*n
double h = -this->getFlux(X,normal);
if (h == 0.0) return true; // Skip for homogeneous Neumann
// Store flux value for visualization
if (fe.iGP < fluxVal.size() && fabs(h) > 1.0e-8)
{
fluxVal[fe.iGP].first = X;
fluxVal[fe.iGP].second += h*normal;
}
// Integrate the Neumann value
elMat.b.front().add(fe.N,h*fe.detJxW);
return true;
}
void Poisson::initIntegration (size_t, size_t nBp)
{
fluxVal.clear();
fluxVal.resize(nBp,std::make_pair(Vec3(),Vec3()));
}
bool Poisson::writeGlvT (VTF* vtf, int iStep, int& geoBlk, int& nBlock) const
{
if (fluxVal.empty())
return true;
else if (!vtf)
return false;
// Write boundary heat flux as discrete point vectors to the VTF-file
return vtf->writeVectors(fluxVal,geoBlk,++nBlock,"Heat flux",iStep);
}
bool Poisson::evalSol2 (Vector& q, const Vectors& eV,
const FiniteElement& fe, const Vec3& X) const
{
// Evaluate the heat flux vector, q = -kappa*du/dX = -kappa*dNdX^T*eV
if (eV.empty() || !fe.dNdX.multiply(eV.front(),q,true))
{
std::cerr <<" *** Poisson::evalSol: Invalid solution vector."<< std::endl;
return false;
}
q *= -this->getMaterial(X);
return true;
}
Vector* Poisson::getExtractionField (size_t ifield)
{
return dualRHS && ifield < primsol.size() ? &primsol[ifield] : nullptr;
}
std::string Poisson::getField1Name (size_t, const char* prefix) const
{
if (!prefix) return "u";
return prefix + std::string(" u");
}
std::string Poisson::getField2Name (size_t i, const char* prefix) const
{
if (i >= nsd) return "";
static const char* s[3] = { "q_x","q_y","q_z" };
if (!prefix) return s[i];
return prefix + std::string(" ") + s[i];
}
double Poisson::getMaterial (const Vec3& X) const
{
return kappaF ? (*kappaF)(X) : kappaC;
}
/*!
\note The Integrand object is allocated dynamically and has to be deleted
manually when leaving the scope of the pointer variable receiving the
returned pointer value.
*/
NormBase* Poisson::getNormIntegrand (AnaSol* asol) const
{
if (asol)
return new PoissonNorm(*const_cast<Poisson*>(this), normIntegrandType,
asol->getScalarSecSol());
else
return new PoissonNorm(*const_cast<Poisson*>(this), normIntegrandType);
}
void Poisson::clearGalerkinProjections ()
{
for (VecFunc* f : galerkin) delete f;
galerkin.clear();
}
PoissonNorm::PoissonNorm (Poisson& p, int integrandType, VecFunc* a) :
NormBase(p), anasol(a), integrdType(integrandType)
{
nrcmp = myProblem.getNoFields(2);
projBou = true;
}
PoissonNorm::~PoissonNorm() = default;
bool PoissonNorm::evalInt (LocalIntegral& elmInt, const FiniteElement& fe,
const Vec3& X) const
{
const Poisson& problem = static_cast<const Poisson&>(myProblem);
ElmNorm& pnorm = static_cast<ElmNorm&>(elmInt);
// Evaluate the finite element and dual solution gradient fields
Vectors epsh(1+problem.numExtrFunction());
for (size_t i = 0; i < elmInt.vec.size() && i < epsh.size(); i++)
if (!elmInt.vec[i].empty())
if (!fe.dNdX.multiply(elmInt.vec[i],epsh[i],true))
{
std::cerr <<" *** PoissonNorm::evalInt: Invalid solution vector "
<< i+1 << std::endl;
return false;
}
// Evaluate the temperature field
double u = pnorm.vec.front().dot(fe.N);
// Evaluate the heat source field
double h = problem.getHeat(X);
// Evaluate the heat conductivity
double kappa = problem.getMaterial(X);
// Evaluate the inverse conductivity scaled by the integration point weight
double cwInv = fe.detJxW / kappa;
// Integrate the energy norm a(u^h,u^h)
pnorm[0] += kappa*epsh.front().dot(epsh.front())*fe.detJxW;
// Integrate the external energy (h,u^h)
if (problem.extEner)
pnorm[1] += h*u*fe.detJxW;
Vector sigma, error;
size_t ip = 2;
if (anasol)
{
// Evaluate the analytical heat flux
sigma.fill((*anasol)(X).ptr(),nrcmp);
// Integrate the energy norm a(u,u)
pnorm[ip++] += sigma.dot(sigma)*cwInv;
// Integrate the error in energy norm a(u-u^h,u-u^h)
error = sigma + kappa*epsh.front();
pnorm[ip++] += error.dot(error)*cwInv;
}
// Integrate the volume
pnorm[ip++] += fe.detJxW;
for (size_t j = 1; j < epsh.size(); j++)
{
// Evaluate the variational-consistent postprocessing quantity, a(u^h,w)
pnorm[ip++] -= kappa*epsh.front().dot(epsh[j])*fe.detJxW;
if (anasol) // Evaluate the corresponding exact quantity, a(u,w)
pnorm[ip++] += sigma.dot(epsh[j])*fe.detJxW;
}
#if INT_DEBUG > 3
std::cout <<"\nPoissonNorm::evalInt(X = "<< X <<")";
if (anasol) std::cout <<"\nsigma ="<< sigma;
std::cout <<"epsh ="<< epsh.front();
for (size_t i = 1; i < epsh.size(); i++)
std::cout <<"epsz("<< i <<") ="<< epsh[i] <<"a(u^h,w"<< i
<<"): "<< pnorm[ip+2*(i-epsh.size())] << std::endl;
#endif
size_t pi = 0;
for (const Vector& psol : pnorm.psol) {
if (!psol.empty() || (projFields.size() > pi && projFields[pi]))
{
// Evaluate projected heat flux field and projected heat flux gradient
Vector sigmar(nrcmp);
Matrix dSigmadX;
if (!projFields.empty() && projFields[pi]) {
projFields[pi]->gradFE(fe, dSigmadX);
projFields[pi]->valueFE(fe, sigmar);
} else {
for (size_t j = 0; j < nrcmp; j++)
sigmar[j] = psol.dot(fe.N,j,nrcmp);
// Evaluate the projected heat flux gradient.
// Notice that the matrix multiplication method used here treats
// the element vector (psol) as a matrix whose number of columns
// equals the number of rows in the matrix fe.dNdX.
if (!dSigmadX.multiplyMat(psol,fe.dNdX)) // dSigmadX = psol*dNdX
return false;
}
// Integrate the energy norm a(u^r,u^r)
pnorm[ip++] += sigmar.dot(sigmar)*cwInv;
// Integrate the estimated error in energy norm a(u^r-u^h,u^r-u^h)
error = sigmar + kappa*epsh.front();
pnorm[ip++] += error.dot(error)*cwInv;
// Evaluate the interior residual of the projected solution
double Res = h - dSigmadX.trace();
// Integrate the residual error in the projected solution
pnorm[ip++] += fe.h*fe.h*Res*Res*fe.detJxW;
if (anasol)
{
// Integrate the error in the projected solution a(u-u^r,u-u^r)
error = sigma - sigmar;
pnorm[ip++] += error.dot(error)*cwInv;
ip += 2; // Make room for the local effectivity indices here
}
}
else if (integrdType & SECOND_DERIVATIVES)
{
// Integrate the residual error in the FE solution
double Res = h;
for (size_t j = 1; j <= fe.N.size(); j++)
Res += fe.d2NdX2.trace(j)*elmInt.vec.front()(j);
pnorm[ip+1] += fe.h*fe.h*Res*Res*fe.detJxW;
ip += anasol ? 6 : 3; // Dummy entries in order to get norm in right place
}
++pi;
}
return true;
}
bool PoissonNorm::evalBou (LocalIntegral& elmInt, const FiniteElement& fe,
const Vec3& X, const Vec3& normal) const
{
const Poisson& problem = static_cast<const Poisson&>(myProblem);
ElmNorm& pnorm = static_cast<ElmNorm&>(elmInt);
// Evaluate the surface heat flux
double h = -problem.getFlux(X,normal);
// Evaluate the temperature field
double u = elmInt.vec.front().dot(fe.N);
// Integrate the external energy (h,u^h)
if (problem.extEner)
pnorm[1] += h*u*fe.detJxW;
size_t ip = this->getNoFields(1) + 2;
size_t pi = 0;
for (const Vector& psol : pnorm.psol) {
if (!psol.empty() || (projFields.size() > pi && projFields[pi]))
{
// Evaluate projected heat flux field
Vector sigmar(nrcmp);
if (!projFields.empty() && projFields[pi])
projFields[pi]->valueFE(fe, sigmar);
else
for (size_t j = 0; j < nrcmp; j++)
sigmar[j] = psol.dot(fe.N,j,nrcmp);
// Evaluate the boundary jump term
double Jump = h + sigmar*normal;
// Integrate the residual error in the projected solution
pnorm[ip] += 0.5*fe.h*Jump*Jump*fe.detJxW;
ip += anasol ? 6 : 3;
}
else if (integrdType & SECOND_DERIVATIVES)
ip += anasol ? 6 : 3; // TODO: Add residual jump terms?
++pi;
}
return true;
}
bool PoissonNorm::finalizeElement (LocalIntegral& elmInt)
{
if (!anasol) return true;
ElmNorm& pnorm = static_cast<ElmNorm&>(elmInt);
// Evaluate local effectivity indices as a(e^r,e^r)/a(e,e)
// with e^r = u^r - u^h and e = u - u^h
for (size_t ip = this->getNoFields(1)+1; ip+4 < pnorm.size(); ip += 6)
{
pnorm[ip+3] = pnorm[ip] / pnorm[3];
pnorm[ip+4] = (pnorm[ip]+pnorm[ip+1]) / pnorm[3];
}
return true;
}
size_t PoissonNorm::getNoFields (int group) const
{
if (group < 1)
return this->NormBase::getNoFields();
else if (group > 1)
return anasol ? 6 : 3;
size_t nExt = static_cast<Poisson&>(myProblem).numExtrFunction();
return anasol ? 5 + 2*nExt : 3 + nExt;
}
std::string PoissonNorm::getName (size_t i, size_t j, const char* prefix) const
{
size_t nx = i > 1 ? 1 : static_cast<Poisson&>(myProblem).numExtrFunction();
if (i == 1 && anasol) nx *= 2;
if (i == 0 || j == 0 || j > 5+nx)
return this->NormBase::getName(i,j,prefix);
static const char* s[15] = {
"a(u^h,u^h)^0.5",
"(h,u^h)^0.5",
"a(u,u)^0.5",
"a(e,e)^0.5, e=u-u^h",
"volume",
"a(u^h,w)",
"a(u,w)",
"a(u^r,u^r)^0.5",
"a(e,e)^0.5, e=u^r-u^h",
"res(u^r)^0.5",
"a(e,e)^0.5, e=u-u^r",
"effectivity index^*",
"effectivity index^RES",
"a(z^h,z^h)^0.5",
"(E(u)*E(z))^0.5, E(v)=a(e,e), e=v^r-v^h"
};
size_t k = j + 6;
if (i == 1)
{
if (anasol)
nx /= 2;
else if (j > 2 && j < 4)
j += 2;
if (j > 5 && nx > 1)
{
j -= 5;
char comp[32];
if (!anasol)
sprintf(comp,"a(u^h,w%zu)",j);
else if (j%2)
sprintf(comp,"a(u^h,w%zu)",j/2+1);
else
sprintf(comp,"a(u,w%zu)",j/2);
return comp;
}
else if (j <= 2 && prefix)
{
if (!strncmp(prefix,"Dual",4))
return s[12+j];
}
else
k = j - 1;
}
if (!prefix)
return s[k];
return prefix + std::string(" ") + s[k];
}
bool PoissonNorm::hasElementContributions (size_t i, size_t j) const
{
return i > 1 || j != 2;
}
int PoissonNorm::getIntegrandType () const
{
return integrdType | myProblem.getIntegrandType();
}
void PoissonNorm::setProjectedFields(Fields* field, size_t idx)
{
if (idx >= projFields.size())
projFields.resize(idx+1);
projFields[idx].reset(field);
}
Poisson::Robin::Robin (unsigned short int n, const Poisson& itg) :
IntegrandBase(n), integrand(itg)
{
alpha = nullptr;
g = nullptr;
}
bool Poisson::Robin::evalBou (LocalIntegral& elmInt, const FiniteElement& fe,
const Vec3& X, const Vec3& normal) const
{
ElmMats& elMat = static_cast<ElmMats&>(elmInt);
Vec3 ax = alpha ? (*alpha)(X) : Vec3(1.0, 1.0, 1.0);
if (g) ax.y = (*g)(X);
elMat.A.front().outer_product(fe.N, fe.N, true, fe.detJxW * ax.x); // mass
elMat.b.front().add(fe.N, fe.detJxW * ax.y); // source
return true;
}