forked from MJ0706/HCM-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoops_objects_MRC2.py
1701 lines (1241 loc) · 55.2 KB
/
oops_objects_MRC2.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 dolfin import *
from edgetypebc import *
import numpy as np
import os as os
import pdb
import vtk_py as vtk_py
#from oops_ConstantDefinitions import Constant_definitions
#from oops_printout import printout
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def printout(statement, mpi_comm):
if(MPI.rank(mpi_comm) == 0):
print statement
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def update_mesh(mesh, displacement, boundaries):
# https://fenicsproject.org/qa/13470/ale-move-class-and-meshes
new_mesh = Mesh(mesh)
new_boundaries = MeshFunction("size_t", new_mesh, 2)
new_boundaries.set_values(boundaries.array())
ALE.move(new_mesh, displacement)
return new_mesh, new_boundaries
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def defCPP_LBBB_Matprop(mesh, mId, meshname = "CRT27_AS_smooth_fine"):
cppcode = """
class K : public Expression
{
public:
void eval(Array<double>& values,
const Array<double>& x,
const ufc::cell& cell) const
{
if ((*materials)[cell.index] == 14 || (*materials)[cell.index] == 15 || (*materials)[cell.index] == 16 || (*materials)[cell.index] == 24 || (*materials)[cell.index] == 25 || (*materials)[cell.index] == 26 || (*materials)[cell.index] == 27 )
values[0] = k_0;
else
values[0] = k_1;
}
std::shared_ptr<MeshFunction<std::size_t>> materials;
double k_0;
double k_1;
};
"""
kappa = Expression(cppcode=cppcode, degree=0)
kappa.materials = mId
kappa.k_0 = 0.0
kappa.k_1 = 1.0
return kappa
#dolfin.File(meshname+"_matProp2_LBBB"+".pvd") << interpolate(kappa, FunctionSpace(mesh,'DG', 0))
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
class Constant_definitions(object):
'''
- - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
Constant definitions for the Nash Panfilov problem
The default constants in this class has to be changed in order to change the constants.
The point is to keep constant definitions away from the sciprt and solver
So options to change params has NOT been given
- - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
'''
def __init__(self):
self.parameters = self.default_parameters()
def default_parameters(self):
d1 = 0.2 # diffusion constant for action potential 0.2 in Nash Panfilov
return {"alpha" : Constant(0.01), # 0.1 in 'a' in NashPanfilov, 0.01 in Gok_Kuhl
"gamma" : Constant(0.002), # 0.01 'epsilon' in NasPanlov, 0.002 in Gok_Kuhl
"b" : Constant(0.15), # 0.1 in Gok_Kuhl
"c" : Constant(8), # 'k' is NashPanfilov, 8 in Gok_Kuhl
"mu1" : Constant(0.2), # 0.12 in NP ,# 0.2 in Gok_Kuhl
"mu2" : Constant(0.3),
"D1" : Constant(((d1,'0.0', '0.0'),('0.0',d1, '0.0'), ('0.0', '0.0', d1))),
"B" : Constant((0.0, 0.0, 0.0)),
"T" : Constant((0.0, 0.0, 0.0))
}
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
class biventricle_mesh(object):
"""
object for biventricle mesh
input: mesh, facet, edge, matid, fibre file
output: mesh
"""
def default_parameters(self):
return {"directory" : "../CRT27/",
"casename" : "CRT27",
"fibre_quad_degree" : 4,
"outputfolder" : "../Outputs/",
"topid": 4,
"LVendoid": 2,
"RVendoid": 3,
"epiid": 1,
}
def update_parameters(self, params):
self.parameters.update(params)
def __init__(self, params, SimDet):
self.mesh = Mesh()
self.parameters = self.default_parameters()
self.parameters.update(params)
directory = self.parameters["directory"]
casename = self.parameters["casename"]
outputfolder = self.parameters["outputfolder"]
folderName = self.parameters["foldername"]
meshfilename = directory + casename + ".hdf5"
f = HDF5File(mpi_comm_world(), meshfilename, 'r')
f.read(self.mesh, casename, False)
self.facetboundaries = MeshFunction("size_t", self.mesh, 2)
f.read(self.facetboundaries, casename+"/"+"facetboundaries")
self.edgeboundaries = MeshFunction("size_t", self.mesh, 1)
f.read(self.edgeboundaries, casename+"/"+"edgeboundaries")
deg = self.parameters["fibre_quad_degree"]
VQuadelem = VectorElement("Quadrature",
self.mesh.ufl_cell(),
degree=deg,
quad_scheme="default")
VQuadelem._quad_scheme = 'default'
self.fiberFS = FunctionSpace(self.mesh, VQuadelem)
self.f0 = Function(self.fiberFS)
self.s0 = Function(self.fiberFS)
self.n0 = Function(self.fiberFS)
#f.read(self.f0, casename+"/"+"eF")
#f.read(self.s0, casename+"/"+"eS")
#f.read(self.n0, casename+"/"+"eN")
if SimDet["DTI_ME"] is True :
f.read(self.f0, casename+"/"+"eF_proj_DTI")
f.read(self.s0, casename+"/"+"eS_proj_DTI")
f.read(self.n0, casename+"/"+"eN_proj_DTI")
else:
f.read(self.f0, casename+"/"+"eF")
f.read(self.s0, casename+"/"+"eS")
f.read(self.n0, casename+"/"+"eN")
#self.displacement = Function(VectorFunctionSpace(self.mesh,'CG',1))
#f.read(self.displacement, casename+"/disp/vector_0")
self.f0 = self.f0/sqrt(inner(self.f0, self.f0))
self.s0 = self.s0/sqrt(inner(self.s0, self.s0))
self.n0 = self.n0/sqrt(inner(self.n0, self.n0))
self.matid = CellFunction('size_t', self.mesh)
if(f.has_dataset(casename+"/"+"matid")):
f.read(self.matid, casename+"/"+"matid")
else:
self.matid.set_all(0)
self.AHAid = CellFunction('size_t', self.mesh)
if(f.has_dataset(casename+"/"+"AHAid")):
f.read(self.AHAid, casename+"/"+"AHAid")
else:
self.AHAid.set_all(0)
EpiBCid = FacetFunction('size_t', self.mesh)
if(f.has_dataset(casename+"/"+"EpiBCid_Corr")):
f.read(EpiBCid, casename+"/"+"EpiBCid_Corr")
else:
EpiBCid.set_all(0)
self.EpiBCid_me = EpiBCid
f.close()
self.topid = self.parameters["topid"]
self.LVendoid = self.parameters["LVendoid"]
self.RVendoid = self.parameters["RVendoid"]
self.epiid = self.parameters["epiid"]
dx = dolfin.dx(self.mesh, subdomain_data=self.matid)
#dx = dolfin.dx(self.mesh, subdomain_data=self.AHAid)
ds = dolfin.ds(self.mesh, subdomain_data=self.facetboundaries)
self.dx = dx
self.ds = ds
print 'Mesh size is : %f '%(self.mesh.num_cells())
r_qmin, r_qmax = MeshQuality.radius_ratio_min_max(self.mesh)
print('Minimal radius ratio:', r_qmin)
print('Maximal radius ratio:', r_qmax)
'''
# Show histogram using matplotlib
hist = MeshQuality.radius_ratio_matplotlib_histogram(mesh)
hist = hist.replace(' import matplotlib.pylab', ' import matplotlib\n matplotlib.use(\'Agg\')\n import matplotlib.pylab\n')
hist = hist.replace('matplotlib.pylab.show()', 'matplotlib.pylab.savefig("mesh-quality.pdf")')
print(hist)
#exec(hist)
'''
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
class lv_mesh(object):
"""
object for lv mesh
input: mesh, facet, edge, matid, fibre file
output: mesh
"""
def default_parameters(self):
return {"directory" : "../CRT27/",
"casename" : "CRT27",
"fibre_quad_degree" : 4,
"outputfolder" : "../Outputs/",
"topid": 4,
"LVendoid": 3,
"epiid": 2,
}
def update_parameters(self, params):
self.parameters.update(params)
def __init__(self, params, SimDet):
self.mesh = Mesh()
self.parameters = self.default_parameters()
self.parameters.update(params)
directory = self.parameters["directory"]
casename = self.parameters["casename"]
outputfolder = self.parameters["outputfolder"]
#comm_common = self.parameters["common_communicator"]
folderName = self.parameters["foldername"]
meshfilename = directory + casename + ".hdf5"
f = HDF5File(mpi_comm_world(), meshfilename, 'r')
f.read(self.mesh, casename, False)
self.facetboundaries = MeshFunction("size_t", self.mesh, 2)
f.read(self.facetboundaries, casename+"/"+"facetboundaries")
self.edgeboundaries = MeshFunction("size_t", self.mesh, 1)
f.read(self.edgeboundaries, casename+"/"+"edgeboundaries")
deg = self.parameters["fibre_quad_degree"]
VQuadelem = VectorElement("Quadrature",
self.mesh.ufl_cell(),
degree=deg,
quad_scheme="default")
VQuadelem._quad_scheme = 'default'
self.fiberFS = FunctionSpace(self.mesh, VQuadelem)
self.f0 = Function(self.fiberFS)
self.s0 = Function(self.fiberFS)
self.n0 = Function(self.fiberFS)
f00 = Function(self.fiberFS)
s00 = Function(self.fiberFS)
n00 = Function(self.fiberFS)
if SimDet["DTI_ME"] is True :
f.read(self.f0, casename+"/"+"eF_proj_DTI")
f.read(self.s0, casename+"/"+"eS_proj_DTI")
f.read(self.n0, casename+"/"+"eN_proj_DTI")
else:
f.read(self.f0, casename+"/"+"eF")
f.read(self.s0, casename+"/"+"eS")
f.read(self.n0, casename+"/"+"eN")
self.f0_ori = self.f0
self.s0_ori = self.s0
self.n0_ori = self.n0
#print self.f0.vector().array()
File(outputfolder + folderName +"/Mesh/mesh.pvd") << self.mesh
File(outputfolder + folderName +"/Mesh/facetbound.pvd") << self.facetboundaries
File(outputfolder + folderName +"/Mesh/edgebound.pvd") << self.edgeboundaries
outfolder = outputfolder + folderName +"Mesh/"
outdirectory = ""
vtkoutfile = outfolder+"_e0_fiber"
vtk_py.convertQuadDataToVTK(self.mesh, self.fiberFS, self.f0, vtkoutfile, outdirectory)
#self.f0 = project(f00, VectorFunctionSpace(self.mesh, "CG", 1))
#self.n0 = project(n00, VectorFunctionSpace(self.mesh, "CG", 1))
#self.s0 = project(s00, VectorFunctionSpace(self.mesh, "CG", 1))
self.f0 = self.f0/sqrt(inner(self.f0, self.f0))
self.s0 = self.s0/sqrt(inner(self.s0, self.s0))
self.n0 = self.n0/sqrt(inner(self.n0, self.n0))
self.matid = CellFunction('size_t', self.mesh)
if(f.has_dataset(casename+"/"+"matid")):
f.read(self.matid, casename+"/"+"matid")
else:
self.matid.set_all(0)
self.AHAid = CellFunction('size_t', self.mesh)
if(f.has_dataset(casename+"/"+"AHAid")):
f.read(self.AHAid, casename+"/"+"AHAid")
else:
self.AHAid.set_all(0)
EpiBCid = FacetFunction('size_t', self.mesh)
if(f.has_dataset(casename+"/"+"EpiBCid_Corr")):
f.read(EpiBCid, casename+"/"+"EpiBCid_Corr")
else:
EpiBCid.set_all(0)
self.EpiBCid_me = EpiBCid
f.close()
self.topid = self.parameters["topid"]
self.LVendoid = self.parameters["LVendoid"]
self.epiid = self.parameters["epiid"]
dx = dolfin.dx(self.mesh, subdomain_data=self.matid)
#dx = dolfin.dx(self.mesh, subdomain_data=self.AHAid)
ds = dolfin.ds(self.mesh, subdomain_data=self.facetboundaries)
self.dx = dx
self.ds = ds
print 'Mesh size is : %f '%(self.mesh.num_cells())
r_qmin, r_qmax = MeshQuality.radius_ratio_min_max(self.mesh)
print('Minimal radius ratio:', r_qmin)
print('Maximal radius ratio:', r_qmax)
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
class DiffusingMedium(object):
"""
Object for anisotropic diffuion in FHN, and Monodomain equation
Having a seperate object allows to add effects of Purkinjee and all that
Input: anisotropic and isotropic diffusion coefficients could be updated
Myofibre angles
Output: Diffusion tensor (degree and family depends on the myofibre vectors)
"""
def __init__(self, params, mesh, mId, isLBBB):
self.parameters = self.default_parameters()
self.parameters.update(params)
self.mesh = mesh
self.mId = mId
self.isLBBB = isLBBB
def default_parameters(self):
return {"D_iso" : Constant((('0.2','0.0', '0.0'),('0.0','0.2', '0.0'), ('0.0', '0.0', '0.2')))}
def Dmat(self):
f0 = self.parameters["fiber"]
s0 = self.parameters["sheet"]
n0 = self.parameters["sheet-normal"]
#D_tensor = self.parameters["Dmat"]
D_iso = self.parameters["D_iso"]
d_ani = self.parameters["d_ani"]
if self.isLBBB == True:
d_ani = defCPP_LBBB_Matprop(mesh = self.mesh, mId = self.mId)
Dij = d_ani*f0[i]*f0[j] + D_iso[i,j]
D_tensor = as_tensor(Dij, (i,j))
return D_tensor
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
class PV_Elas(object):
"""
Elasticity equations object
"""
def default_parameters(self):
return {"outputfolder" : "../Outputs/",
"foldername": "NashPanfilov_BiV_04"}
def update_parameters(self, params):
self.parameters.update(params)
#def __init__(self, biVMesh, params, phi_ref, phi_space):
def __init__(self, biVMesh, params, SimDet):
self.parameters = self.default_parameters()
self.parameters.update(params)
self.deg = self.parameters["degree"]
deg = self.deg
self.isincomp = self.parameters["is_incompressible"]
isincomp = self.isincomp
mesh = biVMesh.mesh
self.isLV = SimDet["isLV"]
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
#V = VectorFunctionSpace(mesh, 'CG', 2)
Q = FunctionSpace(mesh,'CG',1)
Velem = VectorElement("CG", mesh.ufl_cell(), 2, quad_scheme="default")
#Velem = VectorElement("CG", mesh.ufl_cell(), 1, quad_scheme="default")
#Velem._quad_scheme = 'default'
Qelem = FiniteElement("CG", mesh.ufl_cell(), 1, quad_scheme="default")
Qelem._quad_scheme = 'default'
Relem = FiniteElement("Real", mesh.ufl_cell(), 0, quad_scheme="default")
Relem._quad_scheme = 'default'
Quadelem = FiniteElement("Quadrature", mesh.ufl_cell(), degree=deg, quad_scheme="default")
Quadelem._quad_scheme = 'default'
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
Telem2 = TensorElement("Quadrature", mesh.ufl_cell(), degree=deg, shape=2*(3,), quad_scheme='default')
Telem2._quad_scheme = 'default'
for e in Telem2.sub_elements():
e._quad_scheme = 'default'
Telem4 = TensorElement("Quadrature", mesh.ufl_cell(), degree=deg, shape=4*(3,), quad_scheme='default')
Telem4._quad_scheme = 'default'
for e in Telem4.sub_elements():
e._quad_scheme = 'default'
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# Mixed Element for rigid body motion
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
VRelem = MixedElement([Relem, Relem, Relem, Relem, Relem])
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
if(isincomp):
if(self.isLV):
W = FunctionSpace(mesh, MixedElement([Velem, Qelem, Relem, VRelem]))
else:
W = FunctionSpace(mesh, MixedElement([Velem, Qelem, Relem, Relem, VRelem]))
#W = FunctionSpace(mesh, MixedElement([(Velem+Belem), Qelem, Relem, Relem, VRelem]))
else:
if(self.isLV):
W = FunctionSpace(mesh, MixedElement([Velem, Relem, VRelem]))
else:
W = FunctionSpace(mesh, MixedElement([Velem, Relem, Relem, VRelem]))
Quad = FunctionSpace(mesh, Quadelem)
TF = FunctionSpace(mesh, Telem2)
self.W = W
self.Q = Q
self.TF = TF
# this displacement is for FHN
#W_n = Function(W)
#Ve = VectorFunctionSpace(mesh, 'CG', 2)
#self.we_n = Function(Ve)
self.we_n = Function(W.sub(0).collapse())
#phi_me = Function(Q)
#self.phi_me = phi_me
#self.phi_ref = phi_ref
#self.phi_ref_space = phi_space
'''
def interpolate_potential_ep2me(self, v_epMesh):
lp = LagrangeInterpolator()
#lp.interpolate(V_me, v_epMesh)
lp.interpolate(self.phi_ref, v_epMesh)
def tranfer_potential_vertexValues_ref2me(self):
phi_ref_array = self.phi_ref.vector().array()
phi_me_array = self.phi_me.vector().array()
phi_me_new_array = phi_me_array
d2v_me = dof_to_vertex_map(self.Q)
d2v_ref = dof_to_vertex_map(self.phi_ref_space)
print d2v_me, d2v_ref
for idx, (p_me, p_ref) in enumerate(zip(phi_me_array, phi_ref_array)):
phi_me_new_array[d2v_me[idx]] = phi_ref_array[d2v_ref[idx]]
self.phi_me.vector()[:] = phi_me_new_array
def tranfer_potential_ref2me(self):
#print len(self.phi_me.vector()[:]), len(self.phi_ref.vector().array())
phi_ref_array = self.phi_ref.vector().array()
phi_me_array = self.phi_me.vector().array()
phi_me_new_array = phi_me_array
# zip acts as a set filter?
# https://www.programiz.com/python-programming/methods/built-in/zip
# No ....
for idx, (p_me, p_ref) in enumerate(zip(phi_me_array, phi_ref_array)):
phi_me_new_array[idx] = phi_ref_array[idx]
self.phi_me.vector()[:] = phi_me_new_array
#self.phi_ref = phi_ref
#self.phi_me = phi_me
'''
def get_Jn_Cn(self):
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# This is for operator split based coupling
we_n = self.we_n
#we_n = u
d_n = we_n.ufl_domain().geometric_dimension()
I_n = Identity(d_n)
F_n = I_n + grad(we_n)
C_n = F_n.T*F_n
Ic_n = tr(C_n)
J_n = det(F_n)
return (J_n, C_n)
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def get_Fn(self):
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# This is for operator split based coupling
we_n = self.we_n
#we_n = u
d_n = we_n.ufl_domain().geometric_dimension()
I_n = Identity(d_n)
F_n = I_n + grad(we_n)
return F_n
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def set_BCs(self, bivMesh_o, SimDet):
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# Using bubble element
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
#baseconstraint = project(Expression(("0.0"), degree=2), W.sub(0).sub(2).collapse())
#bctop = DirichletBC(W.sub(0).sub(2), baseconstraint, facetboundaries, topid)
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
facetboundaries = bivMesh_o.facetboundaries
edgeboundaries = bivMesh_o.edgeboundaries
topid = bivMesh_o.topid
W = self.W
bctop = DirichletBC(W.sub(0).sub(2), Expression(("0.0"), degree = 2), facetboundaries, topid)
endoring = pick_endoring_bc(method="cpp")(edgeboundaries, 1)
bcedge = DirichletBC(W.sub(0), Expression(("0.0", "0.0", "0.0"), degree = 0), endoring, method="pointwise")
if("springbc" in SimDet.keys() and SimDet["springbc"]):
bcs = [bctop]
else:
bcs = [bctop]#, bcedge]
#bcs = [] # changing top constraint
return bcs
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
class FHN(object):
"""
FHN equations object
Essentially three variables
phi : action potential, normalized, PDE
r : inhibitory variable, ODE
Ta : active stress, ODE
Better to have a seperate object for active stress Ta
"""
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def default_parameters(self):
return {"outputfolder" : "../Outputs/",
"foldername": "NashPanfilov_BiV_04"}
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def update_parameters(self, params):
self.parameters.update(params)
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def __init__(self, biVMesh, params):
self.parameters = self.default_parameters()
self.parameters.update(params)
mesh = biVMesh.mesh
dx = biVMesh.dx
ds = biVMesh.ds
P1_fhn = FiniteElement('CG', mesh.ufl_cell(), 1, quad_scheme="default")
P1_fhn._quad_scheme = 'default'
W_fhn = FunctionSpace(mesh, MixedElement([P1_fhn, P1_fhn, P1_fhn]))
# FHN test and trial functions
w_fhn = Function(W_fhn)
dw_fhn = TrialFunction(W_fhn)
wtest_fhn = TestFunction(W_fhn)
self.W_fhn = W_fhn
self.w_fhn = w_fhn
self.dw_fhn = dw_fhn
self.wtest_fhn = wtest_fhn
w_n_fhn = self.set_ICs()
self.w_n_fhn = w_n_fhn
phi_n, r_n, Ta_n_fhn = split(self.w_n_fhn)
phi, r, Ta_fhn = split(w_fhn)
bcs_FHN = self.set_BCs()
self.bcs_FHN = bcs_FHN
self.phi = phi
self.r = r
self.Ta_fhn = Ta_fhn
self.phi_n = phi_n
self.r_n = r_n
self.Ta_n_fhn = Ta_n_fhn
phi_test, r_test, Ta_test_fhn = split(wtest_fhn)
self.phi_test = phi_test
self.r_test = r_test
self.Ta_test_fhn = Ta_test_fhn
gradPhi_testFunc = VectorFunctionSpace(mesh, "CG", 1)
gradPhi_test = Function(gradPhi_testFunc)
self.gradPhi_test = gradPhi_test
folderName = self.parameters["foldername"]
outputfolder = self.parameters["outputfolder"]
caseID = self.parameters["caseID"]
vtkfile_phi = File(outputfolder + folderName + caseID + 'solution_phi.pvd')
vtkfile_r = File(outputfolder + folderName + caseID + 'solution_r.pvd')
vtkfile_Ta_fhn = File(outputfolder + folderName + caseID + 'solution_Ta_fhn.pvd')
vtkfile_q = File(outputfolder + folderName + caseID + 'solution_q.pvd')
fdataECG = open(outputfolder + folderName + caseID + 'IntegralCharge_.txt', "w", 0)
self.fdataECG = fdataECG
#fdataECG2 = open(outputfolder + folderName + caseID + 'IntegralCharge2_.txt', "w", 0)
#self.fdataECG2 = fdataECG2
self.vtkfile_phi = vtkfile_phi
self.vtkfile_r = vtkfile_r
self.vtkfile_Ta_fhn = vtkfile_Ta_fhn
self.Dmat = self.parameters["Diffusion_Tensor"]
stimPoint = self.parameters["Stimulus_Point"]
#stimuLusString = '( (x[0] > -0.2 + {x0} ) && (x[0] < 0.2 + {x0}) && (x[1] > -0.2 + {x1}) && (x[1] < 0.2 + {x1}) && (x[2] > - 0.2 + {x2}) && (x[2] < 0.2 + {x2}) ) ? 0.3*isStim : 0.0'.format(x0= stimPoint[0], x1= stimPoint[1], x2= stimPoint[2])
stimCondition = self.parameters["Stimulus_Condition"]
if stimCondition == 'apex':
stimuLusString = '(x[2] <= -6.44120193470301 ) ? 1*isStim : 0.0'
elif stimCondition == 'LVFW':
stimuLusString = '(x[0] >= 19.199910386985) ? 1*isStim : 0.0'
else:
#stimuLusString = '(x[0] >= 13.3) && (x[0] <= 13.6) && (x[1] >= 15.4) && (x[1] <= 15.8) && (x[2] >= -0.1) ? 30.0*isStim : 0.0'
stimuLusString = '( (x[0] > -0.3 + {x0} ) && (x[0] < 0.3 + {x0}) && (x[1] > -0.3 + {x1}) && (x[1] < 0.3 + {x1}) && (x[2] > - 0.3 + {x2}) && (x[2] < 0.3 + {x2}) ) ? 10.0*isStim : 0.0'.format(x0= stimPoint[0], x1= stimPoint[1], x2= stimPoint[2])
print stimuLusString
#pdb.set_trace()
stimuLusExpression = stimuLusString
#stimuLusExpression = self.parameters["Stimulus_Expression"]
self.stimuLus = Expression(stimuLusExpression, degree = 1, isStim = 1.0)
self.f_1 = self.stimuLus
#self.stimuLus = Expression('x[2] < -5.6 ? 3.0*isStim : 0.0', degree = 1, isStim = 1)
# why not to interpolate or project
# https://fenicsproject.org/qa/13372/update-expression-and-time-dependent-assembly
self.mesh = mesh
self.dx = dx
self.ds = ds
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def update_state(self, w_new):
self.w_n_fhn = w_new
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def set_BCs(self):
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# Boundary conditions
W_fhn = self.W_fhn
#mesh = self.mesh
#FHN1_bot = Expression(("0.4*isBC"), degree = 1, isBC = 1.0)
#FHN2_bot = Expression(("0.0*isBC"), degree = 1, isBC = 1.0)
#bcBot_FHN1 = DirichletBC(W_fhn.sub(0), FHN1_bot, bcBottom)
#bcBot_FHN2 = DirichletBC(W_fhn.sub(1), FHN2_bot, bcBottom)
#bcs_FHN = [bcBot_FHN1, bcBot_FHN2]
bcs_FHN = []
return bcs_FHN
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def set_ICs(self):
W_fhn = self.W_fhn
# Set initial conditions
phi0 = interpolate(Expression('0.0', degree = 0), W_fhn.sub(0).collapse())
r0 = interpolate(Expression('0.0', degree = 0), W_fhn.sub(1).collapse())
Ta0_fhn = interpolate(Expression('0.0', degree = 0), W_fhn.sub(2).collapse())
# Define test, trial, and required functions
w_n_fhn = Function(W_fhn)
assign(w_n_fhn, [phi0, r0, Ta0_fhn])
return w_n_fhn
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def update_state_w_n(self, w_n_fhn):
self.w_n_fhn = w_n_fhn
phi_n, r_n, Ta_n_fhn = split(self.w_n_fhn)
self.phi_n = phi_n
self.r_n = r_n
self.Ta_n_fhn = Ta_n_fhn
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def FHN_F_J(self, J_fhn=None, C_fhn=None):
phi = self.phi
r = self.r
Ta_fhn = self.Ta_fhn
phi_n = self.phi_n
r_n = self.r_n
Ta_n_fhn = self.Ta_n_fhn
phi_test = self.phi_test
r_test = self.r_test
Ta_test_fhn = self.Ta_test_fhn
mesh = self.mesh
#print mesh.ufl_cell().geometric_dimension()
if J_fhn == None:
J_n = Constant(1.0)
else:
J_n = J_fhn
if C_fhn == None:
C_n = Identity(mesh.ufl_cell().geometric_dimension())
else:
C_n = C_fhn
myC = Constant_definitions()
alpha = myC.parameters['alpha']
g = myC.parameters['gamma']
b = myC.parameters['b']
c = myC.parameters['c']
mu1 = myC.parameters['mu1']
mu2 = myC.parameters['mu2']
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def eps_FHN(phi, r):
return (g + (mu1*r)/(mu2 + phi))
def f_phi(phi, r):
return -c*phi*(phi - alpha)*(phi - 1) - r*phi
def f_r(phi, r):
return eps_FHN(phi,r)*(- r -c*phi*(phi - b - 1.))
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
f_phi = f_phi(phi,r)
f_r = f_r(phi,r)
# Parameters for active stress
e0 = Constant(1.0)
phi_mid = Constant(0.05)
ephi = conditional(gt(phi, phi_mid), e0, 10*e0)
kTa = Constant(2*84000.0)
f_1 = self.f_1
timeDt = self.parameters["time_step"]
k = Constant(timeDt)
Dmat = self.parameters["Diffusion_Tensor"]
#isLBBB = self.parameters["LBBB"]
dx = self.dx
F_FHN = ( (phi - phi_n) / k)*phi_test*dx + 1/J_n*dot(J_n*inv(C_n)*Dmat*grad(phi), grad(phi_test))*dx \
- f_phi*phi_test*dx \
+ ( (r - r_n) / k)*r_test*dx \
- f_r*r_test*dx \
+ ( (Ta_fhn - Ta_n_fhn) / k)*Ta_test_fhn*dx \
- ephi*( kTa*phi - Ta_fhn )*Ta_test_fhn*dx \
- f_1*phi_test*dx
J_FHN = derivative(F_FHN, self.w_fhn, self.dw_fhn)
return (F_FHN, J_FHN)
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
def write_state(self, t, tau):
outputfolder = self.parameters["outputfolder"]
foldername = self.parameters["foldername"]
vtkfile_phi = self.vtkfile_phi
vtkfile_r = self.vtkfile_r
vtkfile_Ta_fhn = self.vtkfile_Ta_fhn
#vtkfile_q = self.vtkfile_q
w_n_fhn = self.w_n_fhn
phi_, r_, Ta_fhn_ = w_n_fhn.split(deepcopy=True)
print('Time = : %0.2f, tau = : %0.2f' % (t, tau ) )
print('Tau is arbitrary' )
vtkfile_phi << phi_
vtkfile_r << r_
vtkfile_Ta_fhn << Ta_fhn_
#https://fenicsproject.org/qa/10159/evaluating-integrals-on-domain
qVec = [assemble(grad(phi_)[i]*dx) for i in range(3)]
print qVec[:]
qVecNorm = np.linalg.norm(qVec)
print >> self.fdataECG, t, qVec[0], qVec[1], qVec[2], qVecNorm
def advance_timeStepssolver_FHN(self, solver_FHN, fhn_steps):
#pdb.set_trace()
for ii in range(0, fhn_steps):
print 'Solving FHN'
print 'Solving ...'
solver_FHN.solvenonlinear()
print 'FHN equations solved'
#fhn_obj.write_state(state_obj.t, tau)
self.w_n_fhn.assign(self.w_fhn)
self.update_state_w_n(self.w_n_fhn)
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
# - - - - - - - - - - - -- - - - - - - - - - - - - - - -- - - - - - -
class State_Variables(object):
"""
State variables for LV and RV, ejecting, relaxing, and filling
"""
def __init__(self, mpi_comm, SimDet):
self.isLV = SimDet["isLV"]
# Systole
self.isLVeject = 0
self.isLVfill = 0
self.isLVfilling = False
self.isLVejecting = False
self.isLVrelaxing = False
if(not self.isLV):
self.isRVeject = 0
self.isRVfill = 0
self.isRVfilling = False
self.isRVejecting = False
self.isRVrelaxing = False
self.tstep = 0
self.BCL = SimDet["HeartBeatLength"]
self.cycle = 0.0
self.t = 0
self.tstep = 0
self.dt = Expression(("dt"), dt=0.0, degree=1)
self.comm = mpi_comm
def check_isnot_isovolumic(self):
if(self.isLV):
return bool(self.isLVeject) or bool(self.isLVfill)
else:
return bool(self.isLVeject) or bool(self.isRVeject) or bool(self.isLVfill) or bool(self.isRVfill)
def update_state(self, pv_o, circuit_o, t_a):
if(self.isLVejecting and circuit_o.Qlv < 0.005):
self.isLVeject = 0
self.isLVejecting = False
self.isLVrelaxing = True
pv_o.LVESV = pv_o.LVV_cav
if(pv_o.LVP_cav*0.0075 > circuit_o.Pao and not(self.isLVrelaxing)):
self.isLVeject = 1
self.isLVejecting = True
if(not self.isLV):
if(self.isRVejecting and circuit_o.Qrv < 0.005):
self.isRVeject = 0
self.isRVejecting = False
self.isRVrelaxing = True
pv_o.RVESV = pv_o.RVV_cav
if(pv_o.RVP_cav*0.0075 > circuit_o.Ppu and not(self.isRVrelaxing)):
self.isRVeject = 1
self.isRVejecting = True
if(pv_o.LVP_cav*0.0075 < circuit_o.Pmv and self.isLVrelaxing and pv_o.LVPfilling_step == 0):
self.isLVfill = 1
self.isLVfilling = True
#self.dt.dt = 10.0#1.0
#LVVfilling_step = (LVEDV - LVESV)/(BCL - t_a.t_a)#*dt.dt
pv_o.LVPfilling_step = (pv_o.LVEDP - pv_o.LVP_cav)/(self.BCL - t_a.t_a)*self.dt.dt
pv_o.LVVfilling_step = (pv_o.LVEDV - pv_o.LVESV)/(self.BCL - t_a.t_a)*self.dt.dt
if(not self.isLV):
if(pv_o.RVP_cav*0.0075 < circuit_o.Ptr and self.isRVrelaxing and pv_o.RVPfilling_step == 0):
self.isRVfill = 1
self.isRVfilling = True
#self.dt.dt = 10.0#1.0
#RVVfilling_step = (RVEDV - RVESV)/(BCL - t_a.t_a)#*dt.dt
pv_o.RVPfilling_step = (pv_o.RVEDP - pv_o.RVP_cav)/(self.BCL - t_a.t_a)*self.dt.dt
pv_o.RVVfilling_step = (pv_o.RVEDV - pv_o.RVESV)/(self.BCL - t_a.t_a)*self.dt.dt
def print_time_details(self):