-
Notifications
You must be signed in to change notification settings - Fork 54
/
MCorr3P.py
1462 lines (1387 loc) · 62.6 KB
/
MCorr3P.py
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import absolute_import
from __future__ import division
from builtins import zip
from builtins import range
from past.utils import old_div
import proteus
import numpy
from proteus import *
from proteus.Transport import *
from proteus.Transport import OneLevelTransport
import os
from proteus import cfemIntegrals, Quadrature, Norms, Comm
from proteus.NonlinearSolvers import NonlinearEquation
from proteus.FemTools import (DOFBoundaryConditions,
FluxBoundaryConditions,
C0_AffineLinearOnSimplexWithNodalBasis)
from proteus.Comm import (globalMax,
globalSum)
from proteus.Profiling import memory
from proteus.Profiling import logEvent as log
from proteus.Transport import OneLevelTransport
from proteus.TransportCoefficients import TC_base
from proteus.SubgridError import SGE_base
from proteus.ShockCapturing import ShockCapturing_base
from . import cMCorr3P
class Coefficients(proteus.TransportCoefficients.TC_base):
from proteus.ctransportCoefficients import levelSetConservationCoefficientsEvaluate
from proteus.ctransportCoefficients import levelSetConservationCoefficientsEvaluate_sd
def __init__(
self,
applyCorrection=True,
epsFactHeaviside=0.0,
epsFactDirac=1.0,
epsFactDiffusion=2.0,
LS_model=None,
V_model=None,
ME_model=None,
VOS_model=None,
VOF_model=None,
checkMass=True,
sd=True,
nd=None,
applyCorrectionToDOF=True,
useMetrics=0.0,
useConstantH=False,
set_vos=None):
self.set_vos = set_vos
self.useConstantH = useConstantH
self.useMetrics = useMetrics
self.sd = sd
self.checkMass = checkMass
self.variableNames = ['phiCorr']
nc = 1
mass = {}
advection = {}
hamiltonian = {}
diffusion = {0: {0: {0: 'constant'}}}
potential = {0: {0: 'u'}}
reaction = {0: {0: 'nonlinear'}}
# reaction={}
if self.sd:
assert nd is not None, "You must set the number of dimensions to use sparse diffusion in LevelSetConservationCoefficients"
sdInfo = {(0,
0): (numpy.arange(start=0,
stop=nd + 1,
step=1,
dtype='i'),
numpy.arange(start=0,
stop=nd,
step=1,
dtype='i'))}
else:
sdInfo = {}
TC_base.__init__(self,
nc,
mass,
advection,
diffusion,
potential,
reaction,
hamiltonian,
self.variableNames,
sparseDiffusionTensors=sdInfo,
useSparseDiffusion=sd)
self.LS_model = LS_model
self.V_model = V_model
self.epsFactHeaviside = epsFactHeaviside
self.epsFactDirac = epsFactDirac
self.epsFactDiffusion = epsFactDiffusion
self.ME_model = ME_model
self.VOF_model = VOF_model
self.VOS_model = VOS_model
self.useC = True
self.applyCorrection = applyCorrection
if self.applyCorrection:
self.applyCorrectionToDOF = applyCorrectionToDOF
else:
self.applyCorrectionToDOF = False
self.massConservationError = 0.0
def initializeMesh(self, mesh):
self.h = mesh.h
self.epsHeaviside = self.epsFactHeaviside * mesh.h
self.epsDirac = self.epsFactDirac * mesh.h
self.epsDiffusion = self.epsFactDiffusion * mesh.h
def attachModels(self, modelList):
import copy
log("Attaching models in LevelSetConservation")
# level set
self.lsModel = modelList[self.LS_model]
self.q_u_ls = modelList[self.LS_model].q[('u', 0)]
self.q_n_ls = modelList[self.LS_model].q[('grad(u)', 0)]
self.ebqe_u_ls = modelList[self.LS_model].ebqe[('u', 0)]
self.ebqe_n_ls = modelList[
self.LS_model].ebqe[
('grad(u)', 0)]
if ('u', 0) in modelList[self.LS_model].ebq:
self.ebq_u_ls = modelList[self.LS_model].ebq[('u', 0)]
else:
self.ebq_u_ls = None
# volume of fluid
self.vofModel = modelList[self.VOF_model]
self.q_H_vof = modelList[self.VOF_model].q[('u', 0)]
if self.VOS_model is not None:
self.q_vos = modelList[self.VOS_model].q[('u', 0)]
else:
self.q_vos = self.vofModel.coefficients.q_vos
self.ebqe_H_vof = modelList[self.VOF_model].ebqe[('u', 0)]
if ('u', 0) in modelList[self.VOF_model].ebq:
self.ebq_H_vof = modelList[self.VOF_model].ebq[('u', 0)]
else:
self.ebq_H_vof = None
# correction
self.massCorrModel = modelList[self.ME_model]
self.vofModel.q[('m_last', 0)][:] = self.vofModel.q[('m', 0)]
if self.checkMass:
self.m_tmp = copy.deepcopy(self.massCorrModel.q[('r', 0)])
if self.checkMass:
# self.vofGlobalMass = Norms.scalarDomainIntegral(self.vofModel.q['dV'],
# self.vofModel.q[('u',0)],
# self.massCorrModel.mesh.nElements_owned)
# self.lsGlobalMass = Norms.scalarHeavisideDomainIntegral(self.vofModel.q['dV'],
# self.lsModel.q[('u',0)],
# self.massCorrModel.mesh.nElements_owned)
#self.vofGlobalMass = 0.0
#self.lsGlobalMass = self.massCorrModel.calculateMass(self.lsModel.q[('u',0)])
# log("Attach Models MCorr3P: mass correction %12.5e" % (Norms.scalarDomainIntegral(self.vofModel.q['dV'],
# self.massCorrModel.q[('r',0)],
# self.massCorrModel.mesh.nElements_owned),),level=2)
self.fluxGlobal = 0.0
self.totalFluxGlobal = 0.0
self.vofGlobalMassArray = [] # self.vofGlobalMass]
self.lsGlobalMassArray = [] # self.lsGlobalMass]
# self.vofGlobalMass - self.vofGlobalMassArray[0]]# +
# self.vofModel.timeIntegration.dt*self.vofModel.coefficients.fluxIntegral]
self.vofGlobalMassErrorArray = []
# self.lsGlobalMass - self.lsGlobalMassArray[0]]# +
# self.vofModel.timeIntegration.dt*self.vofModel.coefficients.fluxIntegral]
self.lsGlobalMassErrorArray = []
self.fluxArray = [] # 0.0]#self.vofModel.coefficients.fluxIntegral]
self.timeArray = [] # self.vofModel.timeIntegration.t]
#log("Attach Models MCorr3P: Phase 0 mass after mass correction (VOF) %12.5e" % (self.vofGlobalMass,),level=2)
#log("Attach Models MCorr3P: Phase 0 mass after mass correction (LS) %12.5e" % (self.lsGlobalMass,),level=2)
#log("Attach Models MCorr3P: Phase 0 mass conservation (VOF) after step = %12.5e" % (self.vofGlobalMass - self.vofModel.coefficients.m_pre + self.vofModel.timeIntegration.dt*self.vofModel.coefficients.fluxIntegral,),level=2)
#log("Attach Models MCorr3P: Phase 0 mass conservation (LS) after step = %12.5e" % (self.lsGlobalMass - self.lsModel.coefficients.m_pre + self.vofModel.timeIntegration.dt*self.vofModel.coefficients.fluxIntegral,),level=2)
def initializeElementQuadrature(self, t, cq):
if self.sd and ('a', 0, 0) in cq:
cq[('a', 0, 0)].fill(self.epsDiffusion)
def initializeElementBoundaryQuadrature(self, t, cebq, cebq_global):
if self.sd and ('a', 0, 0) in cebq:
cebq[('a', 0, 0)].fill(self.epsDiffusion)
def initializeGlobalExteriorElementBoundaryQuadrature(self, t, cebqe):
if self.sd and ('a', 0, 0) in cebqe:
cebqe[('a', 0, 0)].fill(self.epsDiffusion)
def preStep(self, t, firstStep=False):
if self.checkMass:
log(
"Phase 0 mass before mass correction (VOF) %12.5e" %
(Norms.scalarDomainIntegral(
self.vofModel.q['dV'], self.vofModel.q[
('m', 0)], self.massCorrModel.mesh.nElements_owned),), level=2)
log(
"Phase 0 mass (primitive) before mass correction (LS) %12.5e" %
(Norms.scalarSmoothedHeavisideDomainIntegral(
self.epsFactHeaviside,
self.massCorrModel.elementDiameter,
self.vofModel.q['dV'],
self.lsModel.q[
('m',
0)],
self.massCorrModel.mesh.nElements_owned),
),
level=2)
log("Phase 0 mass (consistent) before mass correction (LS) %12.5e" % (
self.massCorrModel.calculateMass(self.lsModel.q[('m', 0)]),), level=2)
copyInstructions = {'clear_uList': True}
return copyInstructions
def postStep(self, t, firstStep=False):
if self.applyCorrection:
# ls
self.lsModel.u[0].dof += self.massCorrModel.u[0].dof
self.lsModel.q[('u', 0)] += self.massCorrModel.q[('u', 0)]
self.lsModel.ebqe[('u', 0)] += self.massCorrModel.ebqe[('u', 0)]
self.lsModel.q[
('grad(u)', 0)] += self.massCorrModel.q[('grad(u)', 0)]
self.lsModel.ebqe[
('grad(u)', 0)] += self.massCorrModel.ebqe[('grad(u)', 0)]
# vof
self.massCorrModel.setMassQuadrature()
self.vofModel.q[('m_tmp',0)][:] = (1.0 - self.vofModel.coefficients.q_vos)*self.vofModel.q[('u',0)]
#self.vofModel.q[('u',0)] += self.massCorrModel.q[('r',0)]
# print "********************max
# VOF************************",max(self.vofModel.q[('u',0)].flat[:])
if self.checkMass:
log(
"Phase 0 mass after mass correction (VOF) %12.5e" %
(Norms.scalarDomainIntegral(
self.vofModel.q['dV'], self.vofModel.q[
('m', 0)], self.massCorrModel.mesh.nElements_owned),), level=2)
log(
"Phase 0 mass (primitive) after mass correction (LS) %12.5e" %
(Norms.scalarSmoothedHeavisideDomainIntegral(
self.epsFactHeaviside,
self.massCorrModel.elementDiameter,
self.vofModel.q['dV'],
self.lsModel.q[
('m',
0)],
self.massCorrModel.mesh.nElements_owned),
),
level=2)
log("Phase 0 mass (consistent) after mass correction (LS) %12.5e" % (
self.massCorrModel.calculateMass(self.lsModel.q[('m', 0)]),), level=2)
copyInstructions = {}
# get the waterline on the obstacle if option set in NCLS (boundary==7)
self.lsModel.computeWaterline(t)
return copyInstructions
def evaluate(self, t, c):
import math
if c[('u', 0)].shape == self.q_u_ls.shape:
u_ls = self.q_u_ls
H_vof = self.q_H_vof
elif c[('u', 0)].shape == self.ebqe_u_ls.shape:
u_ls = self.ebqe_u_ls
H_vof = self.ebqe_H_vof
elif self.ebq_u_ls is not None and c[('u', 0)].shape == self.ebq_u_ls.shape:
u_ls = self.ebq_u_ls
H_vof = self.ebq_H_vof
else:
#\todo trap errors in TransportCoefficients.py
u_ls = None
H_vof = None
if u_ls is not None and H_vof is not None:
if self.useC:
if self.sd:
self.levelSetConservationCoefficientsEvaluate_sd(
self.epsHeaviside, self.epsDirac, u_ls, H_vof, c[
('u', 0)], c[
('r', 0)], c[
('dr', 0, 0)])
else:
self.levelSetConservationCoefficientsEvaluate(
self.epsHeaviside, self.epsDirac, self.epsDiffusion, u_ls, H_vof, c[
('u', 0)], c[
('r', 0)], c[
('dr', 0, 0)], c[
('a', 0, 0)])
if (self.checkMass and c[('u', 0)].shape == self.q_u_ls.shape):
self.m_tmp[:] = H_vof
self.m_tmp += self.massCorrModel.q[('r', 0)]
log("mass correction during Newton %12.5e" % (Norms.scalarDomainIntegral(self.vofModel.q[
'dV'], self.massCorrModel.q[('r', 0)], self.massCorrModel.mesh.nElements_owned),), level=2)
log("Phase 0 mass during Newton %12.5e" % (Norms.scalarDomainIntegral(
self.vofModel.q['dV'], self.m_tmp, self.massCorrModel.mesh.nElements_owned),), level=2)
class LevelModel(proteus.Transport.OneLevelTransport):
nCalls = 0
def __init__(self,
uDict,
phiDict,
testSpaceDict,
matType,
dofBoundaryConditionsDict,
dofBoundaryConditionsSetterDict,
coefficients,
elementQuadrature,
elementBoundaryQuadrature,
fluxBoundaryConditionsDict=None,
advectiveFluxBoundaryConditionsSetterDict=None,
diffusiveFluxBoundaryConditionsSetterDictDict=None,
stressTraceBoundaryConditionsSetterDict=None,
stabilization=None,
shockCapturing=None,
conservativeFluxDict=None,
numericalFluxType=None,
TimeIntegrationClass=None,
massLumping=False,
reactionLumping=False,
options=None,
name='defaultName',
reuse_trial_and_test_quadrature=True,
sd=True,
movingDomain=False,
bdyNullSpace=False):
self.bdyNullSpace = bdyNullSpace
self.useConstantH = coefficients.useConstantH
#
# set the objects describing the method and boundary conditions
#
self.movingDomain = movingDomain
self.tLast_mesh = None
#
self.name = name
self.sd = sd
self.Hess = False
self.lowmem = True
self.timeTerm = True # allow turning off the time derivative
# self.lowmem=False
self.testIsTrial = True
self.phiTrialIsTrial = True
self.u = uDict
self.ua = {} # analytical solutions
self.phi = phiDict
self.dphi = {}
self.matType = matType
# mwf try to reuse test and trial information across components if
# spaces are the same
self.reuse_test_trial_quadrature = reuse_trial_and_test_quadrature # True#False
if self.reuse_test_trial_quadrature:
for ci in range(1, coefficients.nc):
assert self.u[ci].femSpace.__class__.__name__ == self.u[
0].femSpace.__class__.__name__, "to reuse_test_trial_quad all femSpaces must be the same!"
# Simplicial Mesh
# assume the same mesh for all components for now
self.mesh = self.u[0].femSpace.mesh
self.testSpace = testSpaceDict
self.dirichletConditions = dofBoundaryConditionsDict
# explicit Dirichlet conditions for now, no Dirichlet BC constraints
self.dirichletNodeSetList = None
self.coefficients = coefficients
self.coefficients.initializeMesh(self.mesh)
self.nc = self.coefficients.nc
self.stabilization = stabilization
self.shockCapturing = shockCapturing
# no velocity post-processing for now
self.conservativeFlux = conservativeFluxDict
self.fluxBoundaryConditions = fluxBoundaryConditionsDict
self.advectiveFluxBoundaryConditionsSetterDict = advectiveFluxBoundaryConditionsSetterDict
self.diffusiveFluxBoundaryConditionsSetterDictDict = diffusiveFluxBoundaryConditionsSetterDictDict
# determine whether the stabilization term is nonlinear
self.stabilizationIsNonlinear = False
# cek come back
if self.stabilization is not None:
for ci in range(self.nc):
if ci in coefficients.mass:
for flag in list(coefficients.mass[ci].values()):
if flag == 'nonlinear':
self.stabilizationIsNonlinear = True
if ci in coefficients.advection:
for flag in list(coefficients.advection[ci].values()):
if flag == 'nonlinear':
self.stabilizationIsNonlinear = True
if ci in coefficients.diffusion:
for diffusionDict in list(coefficients.diffusion[ci].values()):
for flag in list(diffusionDict.values()):
if flag != 'constant':
self.stabilizationIsNonlinear = True
if ci in coefficients.potential:
for flag in list(coefficients.potential[ci].values()):
if flag == 'nonlinear':
self.stabilizationIsNonlinear = True
if ci in coefficients.reaction:
for flag in list(coefficients.reaction[ci].values()):
if flag == 'nonlinear':
self.stabilizationIsNonlinear = True
if ci in coefficients.hamiltonian:
for flag in list(coefficients.hamiltonian[ci].values()):
if flag == 'nonlinear':
self.stabilizationIsNonlinear = True
# determine if we need element boundary storage
self.elementBoundaryIntegrals = {}
for ci in range(self.nc):
self.elementBoundaryIntegrals[ci] = (
(self.conservativeFlux is not None) or (
numericalFluxType is not None) or (
self.fluxBoundaryConditions[ci] == 'outFlow') or (
self.fluxBoundaryConditions[ci] == 'mixedFlow') or (
self.fluxBoundaryConditions[ci] == 'setFlow'))
#
# calculate some dimensions
#
# assume same space dim for all variables
self.nSpace_global = self.u[0].femSpace.nSpace_global
self.nDOF_trial_element = [
u_j.femSpace.max_nDOF_element for u_j in list(self.u.values())]
self.nDOF_phi_trial_element = [
phi_k.femSpace.max_nDOF_element for phi_k in list(self.phi.values())]
self.n_phi_ip_element = [
phi_k.femSpace.referenceFiniteElement.interpolationConditions.nQuadraturePoints for phi_k in list(self.phi.values())]
self.nDOF_test_element = [
femSpace.max_nDOF_element for femSpace in list(self.testSpace.values())]
self.nFreeDOF_global = [
dc.nFreeDOF_global for dc in list(self.dirichletConditions.values())]
self.nVDOF_element = sum(self.nDOF_trial_element)
self.nFreeVDOF_global = sum(self.nFreeDOF_global)
#
NonlinearEquation.__init__(self, self.nFreeVDOF_global)
#
# build the quadrature point dictionaries from the input (this
# is just for convenience so that the input doesn't have to be
# complete)
#
elementQuadratureDict = {}
elemQuadIsDict = isinstance(elementQuadrature, dict)
if elemQuadIsDict: # set terms manually
for I in self.coefficients.elementIntegralKeys:
if I in elementQuadrature:
elementQuadratureDict[I] = elementQuadrature[I]
else:
elementQuadratureDict[I] = elementQuadrature['default']
else:
for I in self.coefficients.elementIntegralKeys:
elementQuadratureDict[I] = elementQuadrature
if self.stabilization is not None:
for I in self.coefficients.elementIntegralKeys:
if elemQuadIsDict:
if I in elementQuadrature:
elementQuadratureDict[
('stab',) + I[1:]] = elementQuadrature[I]
else:
elementQuadratureDict[
('stab',) + I[1:]] = elementQuadrature['default']
else:
elementQuadratureDict[
('stab',) + I[1:]] = elementQuadrature
if self.shockCapturing is not None:
for ci in self.shockCapturing.components:
if elemQuadIsDict:
if ('numDiff', ci, ci) in elementQuadrature:
elementQuadratureDict[('numDiff', ci, ci)] = elementQuadrature[
('numDiff', ci, ci)]
else:
elementQuadratureDict[('numDiff', ci, ci)] = elementQuadrature[
'default']
else:
elementQuadratureDict[
('numDiff', ci, ci)] = elementQuadrature
if massLumping:
for ci in list(self.coefficients.mass.keys()):
elementQuadratureDict[('m', ci)] = Quadrature.SimplexLobattoQuadrature(
self.nSpace_global, 1)
for I in self.coefficients.elementIntegralKeys:
elementQuadratureDict[
('stab',) + I[1:]] = Quadrature.SimplexLobattoQuadrature(self.nSpace_global, 1)
if reactionLumping:
for ci in list(self.coefficients.mass.keys()):
elementQuadratureDict[('r', ci)] = Quadrature.SimplexLobattoQuadrature(
self.nSpace_global, 1)
for I in self.coefficients.elementIntegralKeys:
elementQuadratureDict[
('stab',) + I[1:]] = Quadrature.SimplexLobattoQuadrature(self.nSpace_global, 1)
elementBoundaryQuadratureDict = {}
if isinstance(elementBoundaryQuadrature, dict): # set terms manually
for I in self.coefficients.elementBoundaryIntegralKeys:
if I in elementBoundaryQuadrature:
elementBoundaryQuadratureDict[
I] = elementBoundaryQuadrature[I]
else:
elementBoundaryQuadratureDict[
I] = elementBoundaryQuadrature['default']
else:
for I in self.coefficients.elementBoundaryIntegralKeys:
elementBoundaryQuadratureDict[I] = elementBoundaryQuadrature
#
# find the union of all element quadrature points and
# build a quadrature rule for each integral that has a
# weight at each point in the union
# mwf include tag telling me which indices are which quadrature rule?
(self.elementQuadraturePoints, self.elementQuadratureWeights,
self.elementQuadratureRuleIndeces) = Quadrature.buildUnion(elementQuadratureDict)
self.nQuadraturePoints_element = self.elementQuadraturePoints.shape[0]
self.nQuadraturePoints_global = self.nQuadraturePoints_element * \
self.mesh.nElements_global
#
# Repeat the same thing for the element boundary quadrature
#
(self.elementBoundaryQuadraturePoints, self.elementBoundaryQuadratureWeights,
self.elementBoundaryQuadratureRuleIndeces) = Quadrature.buildUnion(elementBoundaryQuadratureDict)
self.nElementBoundaryQuadraturePoints_elementBoundary = self.elementBoundaryQuadraturePoints.shape[
0]
self.nElementBoundaryQuadraturePoints_global = (
self.mesh.nElements_global *
self.mesh.nElementBoundaries_element *
self.nElementBoundaryQuadraturePoints_elementBoundary)
#
# simplified allocations for test==trial and also check if space is mixed or not
#
self.q = {}
self.ebq = {}
self.ebq_global = {}
self.ebqe = {}
self.phi_ip = {}
# mesh
#self.q['x'] = numpy.zeros((self.mesh.nElements_global,self.nQuadraturePoints_element,3),'d')
self.q[('u', 0)] = numpy.zeros(
(self.mesh.nElements_global, self.nQuadraturePoints_element), 'd')
self.q[
('grad(u)',
0)] = numpy.zeros(
(self.mesh.nElements_global,
self.nQuadraturePoints_element,
self.nSpace_global),
'd')
self.q[('r', 0)] = numpy.zeros(
(self.mesh.nElements_global, self.nQuadraturePoints_element), 'd')
self.ebqe[
('u',
0)] = numpy.zeros(
(self.mesh.nExteriorElementBoundaries_global,
self.nElementBoundaryQuadraturePoints_elementBoundary),
'd')
self.ebqe[
('grad(u)',
0)] = numpy.zeros(
(self.mesh.nExteriorElementBoundaries_global,
self.nElementBoundaryQuadraturePoints_elementBoundary,
self.nSpace_global),
'd')
self.points_elementBoundaryQuadrature = set()
self.scalars_elementBoundaryQuadrature = set(
[('u', ci) for ci in range(self.nc)])
self.vectors_elementBoundaryQuadrature = set()
self.tensors_elementBoundaryQuadrature = set()
log(memory("element and element boundary Jacobians",
"OneLevelTransport"), level=4)
self.inflowBoundaryBC = {}
self.inflowBoundaryBC_values = {}
self.inflowFlux = {}
for cj in range(self.nc):
self.inflowBoundaryBC[cj] = numpy.zeros(
(self.mesh.nExteriorElementBoundaries_global,), 'i')
self.inflowBoundaryBC_values[cj] = numpy.zeros(
(self.mesh.nExteriorElementBoundaries_global, self.nDOF_trial_element[cj]), 'd')
self.inflowFlux[cj] = numpy.zeros(
(self.mesh.nExteriorElementBoundaries_global,
self.nElementBoundaryQuadraturePoints_elementBoundary),
'd')
self.internalNodes = set(range(self.mesh.nNodes_global))
# identify the internal nodes this is ought to be in mesh
# \todo move this to mesh
for ebNE in range(self.mesh.nExteriorElementBoundaries_global):
ebN = self.mesh.exteriorElementBoundariesArray[ebNE]
eN_global = self.mesh.elementBoundaryElementsArray[ebN, 0]
ebN_element = self.mesh.elementBoundaryLocalElementBoundariesArray[
ebN, 0]
for i in range(self.mesh.nNodes_element):
if i != ebN_element:
I = self.mesh.elementNodesArray[eN_global, i]
self.internalNodes -= set([I])
self.nNodes_internal = len(self.internalNodes)
self.internalNodesArray = numpy.zeros((self.nNodes_internal,), 'i')
for nI, n in enumerate(self.internalNodes):
self.internalNodesArray[nI] = n
#
del self.internalNodes
self.internalNodes = None
log("Updating local to global mappings", 2)
self.updateLocal2Global()
log("Building time integration object", 2)
log(memory("inflowBC, internalNodes,updateLocal2Global",
"OneLevelTransport"), level=4)
# mwf for interpolating subgrid error for gradients etc
if self.stabilization and self.stabilization.usesGradientStabilization:
self.timeIntegration = TimeIntegrationClass(
self, integrateInterpolationPoints=True)
else:
self.timeIntegration = TimeIntegrationClass(self)
if options is not None:
self.timeIntegration.setFromOptions(options)
log(memory("TimeIntegration", "OneLevelTransport"), level=4)
log("Calculating numerical quadrature formulas", 2)
self.calculateQuadrature()
self.setupFieldStrides()
comm = Comm.get()
self.comm = comm
if comm.size() > 1:
assert numericalFluxType is not None and numericalFluxType.useWeakDirichletConditions, "You must use a numerical flux to apply weak boundary conditions for parallel runs"
# STIFFNESS MATRIX #
self.interface_lumpedMassMatrix = numpy.zeros(self.u[0].dof.shape,'d')
self.stiffness_matrix_array = None
self.stiffness_matrix = None
log(memory("stride+offset", "OneLevelTransport"), level=4)
if numericalFluxType is not None:
if options is None or options.periodicDirichletConditions is None:
self.numericalFlux = numericalFluxType(
self,
dofBoundaryConditionsSetterDict,
advectiveFluxBoundaryConditionsSetterDict,
diffusiveFluxBoundaryConditionsSetterDictDict)
else:
self.numericalFlux = numericalFluxType(
self,
dofBoundaryConditionsSetterDict,
advectiveFluxBoundaryConditionsSetterDict,
diffusiveFluxBoundaryConditionsSetterDictDict,
options.periodicDirichletConditions)
else:
self.numericalFlux = None
# set penalty terms
# cek todo move into numerical flux initialization
if 'penalty' in self.ebq_global:
for ebN in range(self.mesh.nElementBoundaries_global):
for k in range(
self.nElementBoundaryQuadraturePoints_elementBoundary):
self.ebq_global['penalty'][ebN, k] = old_div(self.numericalFlux.penalty_constant, (
self.mesh.elementBoundaryDiametersArray[ebN]**self.numericalFlux.penalty_power))
# penalty term
# cek move to Numerical flux initialization
if 'penalty' in self.ebqe:
for ebNE in range(self.mesh.nExteriorElementBoundaries_global):
ebN = self.mesh.exteriorElementBoundariesArray[ebNE]
for k in range(
self.nElementBoundaryQuadraturePoints_elementBoundary):
self.ebqe['penalty'][ebNE, k] = old_div(self.numericalFlux.penalty_constant, \
self.mesh.elementBoundaryDiametersArray[ebN]**self.numericalFlux.penalty_power)
log(memory("numericalFlux", "OneLevelTransport"), level=4)
self.elementEffectiveDiametersArray = self.mesh.elementInnerDiametersArray
# use post processing tools to get conservative fluxes, None by default
from proteus import PostProcessingTools
self.velocityPostProcessor = PostProcessingTools.VelocityPostProcessingChooser(
self)
log(memory("velocity postprocessor", "OneLevelTransport"), level=4)
# helper for writing out data storage
from proteus import Archiver
self.elementQuadratureDictionaryWriter = Archiver.XdmfWriter()
self.elementBoundaryQuadratureDictionaryWriter = Archiver.XdmfWriter()
self.exteriorElementBoundaryQuadratureDictionaryWriter = Archiver.XdmfWriter()
self.globalResidualDummy = None
compKernelFlag = 0
if self.coefficients.useConstantH:
self.elementDiameter = self.mesh.elementDiametersArray.copy()
self.elementDiameter[:] = max(self.mesh.elementDiametersArray)
else:
self.elementDiameter = self.mesh.elementDiametersArray
self.mcorr3p = cMCorr3P.MCorr3P(
self.nSpace_global,
self.nQuadraturePoints_element,
self.u[0].femSpace.elementMaps.localFunctionSpace.dim,
self .u[0].femSpace.referenceFiniteElement.localFunctionSpace.dim,
self.testSpace[0].referenceFiniteElement.localFunctionSpace.dim,
self.nElementBoundaryQuadraturePoints_elementBoundary,
compKernelFlag)
# mwf these are getting called by redistancing classes,
def calculateCoefficients(self):
pass
def calculateElementResidual(self):
if self.globalResidualDummy is not None:
self.getResidual(self.u[0].dof, self.globalResidualDummy)
def getResidual(self, u, r):
import pdb
import copy
"""
Calculate the element residuals and add in to the global residual
"""
if self.coefficients.set_vos:
self.coefficients.set_vos(self.q['x'], self.coefficients.q_vos)
r.fill(0.0)
self.interface_lumpedMassMatrix.fill(0.0)
# Load the unknowns into the finite element dof
self.setUnknowns(u)
# no flux boundary conditions
self.mcorr3p.calculateResidual( # element
self.u[0].femSpace.elementMaps.psi,
self.u[0].femSpace.elementMaps.grad_psi,
self.mesh.nodeArray,
self.mesh.elementNodesArray,
self.elementQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
# element boundary
self.u[0].femSpace.elementMaps.psi_trace,
self.u[0].femSpace.elementMaps.grad_psi_trace,
self.elementBoundaryQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.elementMaps.boundaryNormals,
self.u[0].femSpace.elementMaps.boundaryJacobians,
# physics
self.mesh.nElements_global,
self.coefficients.useMetrics,
self.coefficients.epsFactHeaviside,
self.coefficients.epsFactDirac,
self.coefficients.epsFactDiffusion,
self.u[0].femSpace.dofMap.l2g,
self.elementDiameter, # self.mesh.elementDiametersArray,
self.mesh.nodeDiametersArray,
self.u[0].dof,
self.coefficients.q_u_ls,
self.coefficients.q_n_ls,
self.coefficients.ebqe_u_ls,
self.coefficients.ebqe_n_ls,
self.coefficients.q_H_vof,
self.q[('u', 0)],
self.q[('grad(u)', 0)],
self.ebqe[('u', 0)],
self.ebqe[('grad(u)', 0)],
self.q[('r', 0)],
self.coefficients.q_vos,
self.offset[0], self.stride[0],
r,
self.mesh.nExteriorElementBoundaries_global,
self.mesh.exteriorElementBoundariesArray,
self.mesh.elementBoundaryElementsArray,
self.mesh.elementBoundaryLocalElementBoundariesArray,
# FOR FAST ASSEMBLY of Jacobian matrix
self.interface_lumpedMassMatrix)
log("Global residual", level=9, data=r)
self.coefficients.massConservationError = fabs(globalSum(r[:self.mesh.nNodes_owned].sum()))
log(" Mass Conservation Error", level=3,
data=self.coefficients.massConservationError)
self.nonlinear_function_evaluations += 1
if self.globalResidualDummy is None:
self.globalResidualDummy = numpy.zeros(r.shape, 'd')
def getStiffnessMatrix(self):
rowptr, colind, nzval = self.jacobian.getCSRrepresentation()
nnz = nzval.shape[-1] # number of non-zero entries in sparse matrix
self.stiffness_matrix_array = nzval.copy()
self.stiffness_matrix = SparseMat(self.nFreeDOF_global[0],
self.nFreeDOF_global[0],
nnz,
self.stiffness_matrix_array,
colind,
rowptr)
cfemIntegrals.zeroJacobian_CSR(self.nNonzerosInJacobian,
self.stiffness_matrix)
self.mcorr3p.calculateStiffnessMatrix(
self.u[0].femSpace.elementMaps.psi,
self.u[0].femSpace.elementMaps.grad_psi,
self.mesh.nodeArray,
self.mesh.elementNodesArray,
self.elementQuadratureWeights[('u', 0)],
self.u[0].femSpace.grad_psi,
self.mesh.nElements_global,
self.csrRowIndeces[(0, 0)], self.csrColumnOffsets[(0, 0)],
self.stiffness_matrix,
self.coefficients.useMetrics,
self.coefficients.epsFactDiffusion,
self.elementDiameter,
self.mesh.nodeDiametersArray)
def getJacobian(self, jacobian):
cfemIntegrals.zeroJacobian_CSR(self.nNonzerosInJacobian, jacobian)
if self.stiffness_matrix is None:
self.getStiffnessMatrix()
self.mcorr3p.calculateJacobian(# element
self.u[0].femSpace.elementMaps.psi,
self.u[0].femSpace.elementMaps.grad_psi,
self.mesh.nodeArray,
self.mesh.elementNodesArray,
self.elementQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
# element boundary
self.u[0].femSpace.elementMaps.psi_trace,
self.u[0].femSpace.elementMaps.grad_psi_trace,
self.elementBoundaryQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.elementMaps.boundaryNormals,
self.u[0].femSpace.elementMaps.boundaryJacobians,
self.mesh.nElements_global,
self.coefficients.useMetrics,
self.coefficients.epsFactHeaviside,
self.coefficients.epsFactDirac,
self.coefficients.epsFactDiffusion,
self.u[0].femSpace.dofMap.l2g,
self.elementDiameter, # self.mesh.elementDiametersArray,
self.mesh.nodeDiametersArray,
self.u[0].dof,
self.coefficients.q_u_ls,
self.coefficients.q_n_ls,
self.coefficients.q_H_vof,
self.coefficients.q_vos,
self.csrRowIndeces[(0, 0)], self.csrColumnOffsets[(0, 0)],
jacobian,
# FAST ASSEMBLY
len(self.u[0].dof),
self.rowptr,
self.colind,
self.stiffness_matrix_array,
self.interface_lumpedMassMatrix)
log("Jacobian ", level=10, data=jacobian)
# mwf decide if this is reasonable for solver statistics
self.nonlinear_function_jacobian_evaluations += 1
return jacobian
def elementSolve(self, u, r):
import pdb
import copy
"""
Calculate the element residuals and add in to the global residual
"""
r.fill(0.0)
# Load the unknowns into the finite element dof
self.setUnknowns(u)
# no flux boundary conditions
self.mcorr3p.elementSolve( # element
self.u[0].femSpace.elementMaps.psi,
self.u[0].femSpace.elementMaps.grad_psi,
self.mesh.nodeArray,
self.mesh.elementNodesArray,
self.elementQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
# element boundary
self.u[0].femSpace.elementMaps.psi_trace,
self.u[0].femSpace.elementMaps.grad_psi_trace,
self.elementBoundaryQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.elementMaps.boundaryNormals,
self.u[0].femSpace.elementMaps.boundaryJacobians,
# physics
self.mesh.nElements_global,
self.coefficients.useMetrics,
self.coefficients.epsFactHeaviside,
self.coefficients.epsFactDirac,
self.coefficients.epsFactDiffusion,
self.u[0].femSpace.dofMap.l2g,
self.elementDiameter, # self.mesh.elementDiametersArray,
self.mesh.nodeDiametersArray,
self.u[0].dof,
self.coefficients.q_u_ls,
self.coefficients.q_n_ls,
self.coefficients.ebqe_u_ls,
self.coefficients.ebqe_n_ls,
self.coefficients.q_H_vof,
self.q[('u', 0)],
self.q[('grad(u)', 0)],
self.ebqe[('u', 0)],
self.ebqe[('grad(u)', 0)],
self.q[('r', 0)],
self.coefficients.q_vos,
self.offset[0], self.stride[0],
r,
self.mesh.nExteriorElementBoundaries_global,
self.mesh.exteriorElementBoundariesArray,
self.mesh.elementBoundaryElementsArray,
self.mesh.elementBoundaryLocalElementBoundariesArray,
self.maxIts,
self.atol)
def elementConstantSolve(self, u, r):
import pdb
import copy
"""
Calculate the element residuals and add in to the global residual
"""
r.fill(0.0)
# Load the unknowns into the finite element dof
self.setUnknowns(u)
# no flux boundary conditions
self.mcorr3p.elementConstantSolve( # element
self.u[0].femSpace.elementMaps.psi,
self.u[0].femSpace.elementMaps.grad_psi,
self.mesh.nodeArray,
self.mesh.elementNodesArray,
self.elementQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
# element boundary
self.u[0].femSpace.elementMaps.psi_trace,
self.u[0].femSpace.elementMaps.grad_psi_trace,
self.elementBoundaryQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.elementMaps.boundaryNormals,
self.u[0].femSpace.elementMaps.boundaryJacobians,
# physics
self.mesh.nElements_global,
self.coefficients.useMetrics,
self.coefficients.epsFactHeaviside,
self.coefficients.epsFactDirac,
self.coefficients.epsFactDiffusion,
self.u[0].femSpace.dofMap.l2g,
self.elementDiameter, # self.mesh.elementDiametersArray,
self.mesh.nodeDiametersArray,
self.u[0].dof,
self.coefficients.q_u_ls,
self.coefficients.q_n_ls,
self.coefficients.ebqe_u_ls,
self.coefficients.ebqe_n_ls,
self.coefficients.q_H_vof,
self.q[('u', 0)],
self.q[('grad(u)', 0)],
self.ebqe[('u', 0)],
self.ebqe[('grad(u)', 0)],
self.q[('r', 0)],
self.coefficients.q_vos,
self.offset[0], self.stride[0],
r,
self.mesh.nExteriorElementBoundaries_global,
self.mesh.exteriorElementBoundariesArray,
self.mesh.elementBoundaryElementsArray,
self.mesh.elementBoundaryLocalElementBoundariesArray,
self.maxIts,
self.atol)
def globalConstantRJ(self, u, r, U):
import pdb
import copy
"""
Calculate the element residuals and add in to the global residual
"""
r.fill(0.0)
# Load the unknowns into the finite element dof
self.setUnknowns(u)
# no flux boundary conditions
(R, J) = self.mcorr3p.globalConstantRJ( # element
self.u[0].femSpace.elementMaps.psi,
self.u[0].femSpace.elementMaps.grad_psi,
self.mesh.nodeArray,
self.mesh.elementNodesArray,
self.elementQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
self.u[0].femSpace.psi,
self.u[0].femSpace.grad_psi,
# element boundary
self.u[0].femSpace.elementMaps.psi_trace,
self.u[0].femSpace.elementMaps.grad_psi_trace,
self.elementBoundaryQuadratureWeights[('u', 0)],
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.psi_trace,
self.u[0].femSpace.grad_psi_trace,
self.u[0].femSpace.elementMaps.boundaryNormals,
self.u[0].femSpace.elementMaps.boundaryJacobians,
# physics
self.mesh.nElements_owned,
self.coefficients.useMetrics,
self.coefficients.epsFactHeaviside,
self.coefficients.epsFactDirac,
self.coefficients.epsFactDiffusion,
self.u[0].femSpace.dofMap.l2g,
self.elementDiameter,
self.mesh.elementDiametersArray,
self.mesh.nodeDiametersArray,
self.u[0].dof,
self.coefficients.q_u_ls,
self.coefficients.q_n_ls,
self.coefficients.ebqe_u_ls,