-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorkshop.jl
1403 lines (1168 loc) · 38.2 KB
/
Workshop.jl
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
using CirculatorySystemModels, ModelingToolkit, DifferentialEquations, Plots
## Go through basic equations of CirculatorySystemModels ##
## Two element Windkessel ##
@parameters t
begin
function heaviside(t)
0.5 * (sign(t) + 1)
end
function I_sin(t)
sin(2pi*(t))^2 * (heaviside(mod(t,1)) - heaviside(mod(t,1)-0.5))
end
end
plot(I_sin, xlim=[0,2], label="I", xlabel="time", ylabel="I [ml/s]")
@named source = DrivenFlow(Q=-100.0, fun=I_sin)
@named R = Resistor(R = 1.0)
@named C = Capacitor(C = 1.3)
@named ground = Ground()
circ_eqs = [
connect(source.out, C.in, R.in)
connect(R.out, source.in, C.out, ground.g)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
##
@named circ_model = compose(_circ_model,[source, ground, C, R])
## And simplify it
@time circ_sys = structural_simplify(circ_model)
# Examine equation
# give mean pressures as IC
u0 = [10.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 10.0))
@time sol = solve(prob)
plot(sol)
# Two element Windkessel compliance #
@parameters t
begin
function heaviside(t)
0.5 * (sign(t) + 1)
end
function I_sin(t)
sin(2pi*(t))^2 * (heaviside(mod(t,1)) - heaviside(mod(t,1)-0.5))
end
end
plot(I_sin, xlim=[0,2], label="I", xlabel="time", ylabel="I [ml/s]")
@named source = DrivenFlow(Q=100.0, fun=I_sin)
@named R = Resistor(R = 1.0)
@named C = Compliance(C = 1.3)
@named ground = Ground()
circ_eqs = [
connect(source.out, C.in)
connect(C.out, R.in)
connect(R.out, source.in, ground.g)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
##
@named circ_model = compose(_circ_model,[source, ground, C, R])
## And simplify it
@time circ_sys = structural_simplify(circ_model)
# Examine Equation
# give mean pressures as IC
u0 = [9.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 10.0))
@time sol = solve(prob)
plot(sol)
# Three element windkessel #
@parameters t
begin
function heaviside(t)
0.5 * (sign(t) + 1)
end
function I_sin(t)
sin(2pi*(t))^2 * (heaviside(mod(t,1)) - heaviside(mod(t,1)-0.5))
end
end
plot(I_sin, xlim=[0,2], label="I", xlabel="time", ylabel="I [ml/s]")
@named source = DrivenFlow(Q=100.0, fun=I_sin)
@named Rp = Resistor(R = 0.1)
@named Rc = Resistor(R = 1.0)
@named C = Compliance(C = 1.3)
@named ground = Ground()
circ_eqs = [
connect(source.out, Rp.in)
connect(Rp.out, C.in)
connect(C.out, Rc.in)
connect(Rc.out, source.in, ground.g)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
##
@named circ_model = compose(_circ_model,[source, ground, C, Rp, Rc])
## And simplify it
@time circ_sys = structural_simplify(circ_model)
# Examine Equation
# give mean pressures as IC
u0 = [9.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 10.0))
@time sol = solve(prob)
plot(sol)
plot(sol, idxs = Rp.q)
## 4 Element Windkessel series ##
@parameters t
begin
function heaviside(t)
0.5 * (sign(t) + 1)
end
function I_sin(t)
sin(2pi*(t))^2 * (heaviside(mod(t,1)) - heaviside(mod(t,1)-0.5))
end
end
plot(I_sin, xlim=[0,2], label="I", xlabel="time", ylabel="I [ml/s]")
@named source = DrivenFlow(Q=100.0, fun=I_sin)
@named L = Inductance(L = 1e-2)
@named Rp = Resistor(R = 0.1)
@named Rc = Resistor(R = 1.0)
@named C = Compliance(C = 1.3)
@named ground = Ground()
circ_eqs = [
connect(source.out, L.in)
connect(L.out, Rp.in)
connect(Rp.out, C.in)
connect(C.out, Rc.in)
connect(Rc.out, source.in, ground.g)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
##
@named circ_model = compose(_circ_model,[source, ground, C, Rp, Rc, L])
## And simplify it
@time circ_sys = structural_simplify(circ_model)
# Examine Equation
# give mean pressures as IC
u0 = [9.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 10.0))
@time sol = solve(prob)
plot(sol)
plot(sol, idxs = L.q)
plot(sol, idxs = C.V)
# - Ply for 20 minutes examining different elements etc
# - Jump to presentation about valves and cardiac chambers
## Nikolai ODE model
# Function for the valves
function Valve(R, deltaP)
q = 0.0
if (-deltaP) < 0.0
q = deltaP/R
else
q = 0.0
end
return q
end
# Functions for the Ventricle
function ShiElastance(t, Eₘᵢₙ, Eₘₐₓ, τ, τₑₛ, τₑₚ, Eshift)
τₑₛ = τₑₛ*τ
τₑₚ = τₑₚ*τ
#τ = 4/3(τₑₛ+τₑₚ)
tᵢ = rem(t + (1 - Eshift) * τ, τ)
Eₚ = (tᵢ <= τₑₛ) * (1 - cos(tᵢ / τₑₛ * pi)) / 2 +
(tᵢ > τₑₛ) * (tᵢ <= τₑₚ) * (1 + cos((tᵢ - τₑₛ) / (τₑₚ - τₑₛ) * pi)) / 2 +
(tᵢ <= τₑₚ) * 0
E = Eₘᵢₙ + (Eₘₐₓ - Eₘᵢₙ) * Eₚ
return E
end
function DShiElastance(t, Eₘᵢₙ, Eₘₐₓ, τ, τₑₛ, τₑₚ, Eshift)
τₑₛ = τₑₛ*τ
τₑₚ = τₑₚ*τ
#τ = 4/3(τₑₛ+τₑₚ)
tᵢ = rem(t + (1 - Eshift) * τ, τ)
DEₚ = (tᵢ <= τₑₛ) * pi / τₑₛ * sin(tᵢ / τₑₛ * pi) / 2 +
(tᵢ > τₑₛ) * (tᵢ <= τₑₚ) * pi / (τₑₚ - τₑₛ) * sin((τₑₛ - tᵢ) / (τₑₚ - τₑₛ) * pi) / 2
(tᵢ <= τₑₚ) * 0
DE = (Eₘₐₓ - Eₘᵢₙ) * DEₚ
return DE
end
#Parameters
Eshift = 0.0
Eₘᵢₙ = 0.03
τₑₛ = 0.3
τₑₚ = 0.45
Eₘₐₓ = 1.5
Rmv = 0.06
τ = 1.0
function NIK!(du, u, p, t)
pLV, psa, psv, Vlv, Qav, Qmv, Qs = u # Model variables
τₑₛ, τₑₚ, Rmv, Zao, Rs, Csa, Csv, Eₘₐₓ, Eₘᵢₙ = p # Model parameters
# the differential equations
du[1] = (Qmv - Qav) * ShiElastance(t, Eₘᵢₙ, Eₘₐₓ, τ, τₑₛ, τₑₚ, Eshift) + pLV / ShiElastance(t, Eₘᵢₙ, Eₘₐₓ, τ, τₑₛ, τₑₚ, Eshift) * DShiElastance(t, Eₘᵢₙ, Eₘₐₓ, τ, τₑₛ, τₑₚ, Eshift)
# 1 Left Ventricle
du[2] = (Qav - Qs ) / Csa #Systemic arteries
du[3] = (Qs - Qmv) / Csv # Venous
du[4] = Qmv - Qav # volume
du[5] = Valve(Zao, (pLV - psa)) - Qav # AV
du[6] = Valve(Rmv, (psv - pLV)) - Qmv # MV
du[7] = (du[2] - du[3]) / Rs # Systemic flow
nothing
end
# Mass Matrix as ODAE problem
M = [1. 0 0 0 0 0 0
0 1. 0 0 0 0 0
0 0 1. 0 0 0 0
0 0 0 1. 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 1. ]
Nik_ODE = ODEFunction(NIK!,mass_matrix=M)
# Inintal conditions
u0 = [8.0, 8.0, 8.0, 265.0, 0.0, 0.0, 0.0]
# Model Parameters
p = [0.3, 0.45, 0.06, 0.033, 1.11, 1.13, 11.0, 1.5, 0.03]
# Time span we solve over
tspan = (0, 20)
# Define ODE problem
prob = ODEProblem(Nik_ODE, u0, tspan, p)
# Solve the problem
# ODAE problem use Rodas4 solver
# autidiff = false due to undifferentiable valve statements
@time sol = solve(prob, Rodas4(autodiff = false), reltol = 1e-10, abstol = 1e-10)
# Plot solution
plot(sol)
# Nikolai Symbolic model
Emin = 0.03
τ = 1
τes = 0.3*τ
τep = 0.45*τ
Emax = 1.5
Rmv = 0.006
Zao = 0.033
Rs = 1.11
Rab = 1.2
Cao = 1.13
Csv = 11.0
@parameters t
# Define elements of the model
# Ventricle
@named LV = ShiChamber(V₀ = 20.0, p₀ = 1.0, Eₘᵢₙ = Emin, Eₘₐₓ = Emax, τ = τ, τₑₛ = τes, τₑₚ = τep, Eshift=0.0)
#Valves
@named AVa = ResistorDiode(R = Zao)
@named MV = ResistorDiode(R = Rmv)
#Chambers
@named AO = Compliance(C = Cao) # Aorta
@named VS = Compliance(C = Csv) # venous System
@named SR = Resistor(R = Rs) # Systemic Resistance
# Connect the equations given the direction of the flow
circ_eqs = [
connect(LV.out, AVa.in)
connect(AVa.out, AO.in)
connect(AO.out, SR.in)
connect(SR.out, VS.in)
connect(VS.out, MV.in)
connect(MV.out, LV.in)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
# Assign variable names
@named circ_model = compose(_circ_model,[LV, AVa, MV, AO, SR, VS])
# equations of the model
equations(circ_model) #unusable in this form
## Simplfy equations - eliminating variables
@time circ_sys = structural_simplify(circ_model)
# Print equations
equations(circ_sys) # Same as what would have given we formed by hand - note LV.V
observed(circ_sys) # Gives equations for whole system
# notice now all algebraic variables have been eliminated as we left them in previosly
# Inital conditions
u0 = [100.0, 90.0, 8.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 20.0))
# Vern9 is a high order ode solv er not compatible with algebraic equations
@time sol = solve(prob, Vern9(), reltol=1e-10, abstol=1e-10)
plot(sol)
# We now plot specifc variables symbolically
# Aortic Valve flow
plot(sol, idxs = AVa.q)
# Venous volume
plot(sol, idxs = VS.V)
# Aortic Pressure
plot(sol, idxs = AO.p)
# Nik DH elstance
Emin = 0.03
τ = 1
τ1 = 0.303*τ
τ2 = τ*0.508
n1 = 1.32
n2 = 21.9
nstep = 1000
t = LinRange(0, τ, nstep)
k = 1 / maximum((t ./ τ1).^n1 ./ (1 .+ (t ./ τ1).^n1) .* 1 ./ (1 .+ (t ./ τ2).^n2))
Emax = 1.5
Rmv = 0.006
Zao = 0.033
Rs = 1.11
Rab = 1.2
Cao = 1.13
Csv = 11.0
@parameters t
# Define elements of the model
# Ventricle
@named LV = DHChamber(V₀ = 0.0, Eₘᵢₙ = Emin, Eₘₐₓ = Emax, n₁ = n1, n₂ = n2, τ = τ, τ₁ = τ1, τ₂ = τ2, k = k)
#Valves
@named AVa = ResistorDiode(R = Zao)
@named MV = ResistorDiode(R = Rmv)
#Chambers
@named AO = Compliance(C = Cao) # Aorta
@named VS = Compliance(C = Csv) # venous System
@named SR = Resistor(R = Rs) # Systemic Resistance
# Connect the equations given the direction of the flow
circ_eqs = [
connect(LV.out, AVa.in)
connect(AVa.out, AO.in)
connect(AO.out, SR.in)
connect(SR.out, VS.in)
connect(VS.out, MV.in)
connect(MV.out, LV.in)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
# Assign variable names
@named circ_model = compose(_circ_model,[LV, AVa, MV, AO, SR, VS])
## Simplfy equations - eliminating variables
@time circ_sys = structural_simplify(circ_model)
# Print equations
equations(circ_sys) # Same as what would have given we formed by hand - note LV.p now in pressure
observed(circ_sys) # Gives equations for whole system
# notice now all algebraic variables have been eliminated as we left them in previosly
# Inital conditions
u0 = [5.0, 70.0, 6.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 20.0))
# Vern9 is a high order ode solv er not compatible with algebraic equations
@time sol = solve(prob, Vern9(), reltol=1e-10, abstol=1e-10)
plot(sol)
# We now plot specifc variables symbolically
# Aortic Valve flow
plot(sol, idxs = AVa.q)
# Venous volume
plot(sol, idxs = VS.V)
# Aortic Pressure
plot(sol, idxs = AO.p)
## More physiological adaptation constant pressure on venous base plate
Emin = 0.03
τ = 1
τes = 0.3*τ
τep = 0.45*τ
Emax = 1.5
Rmv = 0.006
Zao = 0.033
Rs = 1.11
Rab = 1.2
Cao = 1.13
Csv = 11.0
@parameters t
# Define elements of the model
# Ventricle
@named LV = ShiChamber(V₀ = 20.0, p₀ = 1.0, Eₘᵢₙ = Emin, Eₘₐₓ = Emax, τ = τ, τₑₛ = τes, τₑₚ = τep, Eshift=0.0)
#Valves
@named AVa = ResistorDiode(R = Zao)
@named MV = ResistorDiode(R = Rmv)
#Chambers
@named AO = Compliance(C = Cao) # Aorta
@named VS = Compliance(C = Csv) # venous System
@named SR = Resistor(R = Rs) # Systemic Resistance
@named BP = ConstantPressure(P = 6.0)
@named ground = Ground()
# Connect the equations given the direction of the flow
circ_eqs = [
connect(LV.out, AVa.in)
connect(AVa.out, AO.in)
connect(AO.out, SR.in)
connect(SR.out, VS.in)
connect(BP.out, VS.in)
connect(VS.out, MV.in)
connect(MV.out, LV.in)
connect(BP.in, ground.g)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
# Assign variable names
@named circ_model = compose(_circ_model,[LV, AVa, MV, AO, SR, VS, BP, ground])
# equations of the model
equations(circ_model) #unusable in this form
## Simplfy equations - eliminating variables
@time circ_sys = structural_simplify(circ_model)
# Print equations
equations(circ_sys) # Same as what would have given we formed by hand - note LV.V
observed(circ_sys) # Gives equations for whole system
# notice now all algebraic variables have been eliminated as we left them in previosly
# Inital conditions
u0 = [100.0, 90.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 20.0))
# Vern9 is a high order ode solv er not compatible with algebraic equations
@time sol = solve(prob, Vern9(), reltol=1e-10, abstol=1e-10)
plot(sol)
# We now plot specifc variables symbolically
# Aortic Valve flow
plot(sol, idxs = AVa.q)
# Venous volume
plot(sol, idxs = VS.V)
# Aortic Pressure
plot(sol, idxs = AO.p)
# Left Ventricular Pressure
plot(sol, idxs = LV.p)
### Have a play with different elements trying different things then we move onto a more complex model ###
## Combine circulation NIK
function CRC(; name, C=1.0, R=1.0, C1=1.0)
@named in = Pin()
@named out = Pin()
sts = @variables Δp(t) = 0.0 q(t) = 0.0
ps = []
# These are the components the subsystem is made of:
@named C = Compliance(C=C)
@named R = Resistor(R=R)
@named C1 = Compliance(C = C1)
# The equations for the subsystem are created by
# 'connect'-ing the components
eqs = [
Δp ~ out.p - in.p
q ~ in.q
connect(in, C.in)
connect(C.out, R.in)
connect(R.out, C1.in)
connect(C1.out, out)
]
# and finaly compose the system
compose(ODESystem(eqs, t, sts, ps; name=name), in, out, C, R, C1)
end
Emin = 0.03
τ = 1
τes = 0.3*τ
τep = 0.45*τ
Emax = 1.5
Rmv = 0.006
Zao = 0.033
Rs = 1.11
Rab = 1.2
Cao = 1.13
Csv = 11.0
@parameters t
# Define elements of the model
# Ventricle
@named LV = ShiChamber(V₀ = 20.0, p₀ = 1.0, Eₘᵢₙ = Emin, Eₘₐₓ = Emax, τ = τ, τₑₛ = τes, τₑₚ = τep, Eshift=0.0)
#Valves
@named AVa = ResistorDiode(R = Zao)
@named MV = ResistorDiode(R = Rmv)
#Systemic circulation
@named sys_circ = CRC(C = Cao, R = Rs, C1 = Csv)
# Connect the equations given the direction of the flow
circ_eqs = [
connect(LV.out, AVa.in)
connect(AVa.out, sys_circ.in)
connect(sys_circ.out, MV.in)
connect(MV.out, LV.in)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
# Assign variable names
@named circ_model = compose(_circ_model,[LV, AVa, MV, sys_circ])
# equations of the model
equations(circ_model) #unusable in this form
## Simplfy equations - eliminating variables
@time circ_sys = structural_simplify(circ_model)
# Print equations
equations(circ_sys) # Same as what would have given we formed by hand - note LV.V
observed(circ_sys) # Gives equations for whole system
# notice now all algebraic variables have been eliminated as we left them in previosly
# Inital conditions
u0 = [100.0, 90.0, 8.0]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 20.0))
# Vern9 is a high order ode solv er not compatible with algebraic equations
@time sol = solve(prob, Vern9(), reltol=1e-10, abstol=1e-10)
plot(sol)
# We now plot specifc variables symbolically
# Aortic Valve flow
plot(sol, idxs = AVa.q)
# Venous volume
plot(sol, idxs = sys_circ.C1.V)
# Aortic Pressure
plot(sol, idxs = sys_circ.C.p)
## Shi systemic model Diode Valves ##
begin
τ = 1.0
v0_lv = 5.0
p0_lv = 1.0
Emin_lv = 0.1
Emax_lv = 2.5
τes_lv = 0.3
τed_lv = 0.45
Eshift_lv = 0.0
LV_Vt0 = 500
### LA Atrium Parameters #### Checked
v0_la = 4.0
p0_la = 1.0
Emin_la = 0.15
Emax_la = 0.25
τpwb_la = 0.92
τpww_la = 0.09
τes_la = τpww_la/2
τed_la = τpww_la
Eshift_la = τpwb_la
LA_Vt0 = 20
#####
Csas = 0.08
Rsas = 0.003
Lsas = 6.2e-5
pt0sas = 100.0
qt0sas = 0.0
## Systemic Artery #### Checked
Csat = 1.6
Rsat = 0.05
Lsat = 0.0017
pt0sat = 100.0
qt0sat = 0.0
## Systemic Arteriole #### Checked
Rsar = 0.5
## Systemic Capillary #### Checked
Rscp = 0.52
## Systemic Vein #### Checked
Csvn = 20.5
Rsvn = 0.075
pt0svn = 0.0
qt0svn = 0.0
## Valve Parameters
Zao = 0.033
Rmv = 0.06
end
@parameters t
@named LV = ShiChamber(V₀=v0_lv, p₀ = p0_lv, Eₘᵢₙ=Emin_lv, Eₘₐₓ=Emax_lv, τ=τ, τₑₛ=τes_lv, τₑₚ=τed_lv, Eshift=0.0)
@named LA = ShiChamber(V₀=v0_la, p₀ = p0_la, Eₘᵢₙ=Emin_la, Eₘₐₓ=Emax_la, τ=τ, τₑₛ=τpww_la/2, τₑₚ=τpww_la, Eshift=τpwb_la)
# Examine how atrium is different - Show there is an explicit element but can use same one
@named AV = ResistorDiode(R = Zao)
@named MV = ResistorDiode(R = Rmv)
####### Systemic Loop #######
## Systemic Aortic Sinus ##
@named SAS = CRL(C=Csas, R=Rsas, L=Lsas)
## Systemic Artery ##
@named SAT = CRL(C=Csat, R=Rsat, L=Lsat)
## Systemic Arteriole ##
@named SAR = Resistor(R=Rsar)
## Systemic Capillary ##
@named SCP = Resistor(R=Rscp)
## Systemic Vein ##
@named SVN = CR(R=Rsvn, C=Csvn)
circ_eqs = [
connect(LV.out, AV.in)
connect(AV.out, SAS.in)
connect(SAS.out, SAT.in)
connect(SAT.out, SAR.in)
connect(SAR.out, SCP.in)
connect(SCP.out, SVN.in)
connect(SVN.out, LA.in)
connect(LA.out, MV.in)
connect(MV.out, LV.in)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
##
@named circ_model = compose(_circ_model,
[LV, LA, AV, MV, SAS, SAT, SAR, SCP, SVN])
## And simplify it
@time circ_sys = structural_simplify(circ_model)
u0 = [LV_Vt0, LA_Vt0, pt0sas, qt0sas , pt0sat, qt0sat, pt0svn]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 20.0))
@time sol = solve(prob, Vern7(), reltol = 1e-9, abstol = 1e-9)
plot(sol, tspan = (15, 16))
plot(sol, idxs = [LV.p, SAS.C.p], tspan = (15, 16))
# Shi with systemic simplified
begin
τ = 1.0
v0_lv = 5.0
p0_lv = 1.0
Emin_lv = 0.1
Emax_lv = 2.5
τes_lv = 0.3
τed_lv = 0.45
Eshift_lv = 0.0
LV_Vt0 = 500
### LA Atrium Parameters #### Checked
v0_la = 4.0
p0_la = 1.0
Emin_la = 0.15
Emax_la = 0.25
τpwb_la = 0.92
τpww_la = 0.09
τes_la = τpww_la/2
τed_la = τpww_la
Eshift_la = τpwb_la
LA_Vt0 = 20
#####
Csas = 0.08
Rsas = 0.003
Lsas = 6.2e-5
pt0sas = 100.0
qt0sas = 0.0
## Systemic Artery #### Checked
Csat = 1.6
Rsat = 0.05
Lsat = 0.0017
pt0sat = 100.0
qt0sat = 0.0
## Systemic Arteriole #### Checked
Rsar = 0.5
## Systemic Capillary #### Checked
Rscp = 0.52
## Systemic Vein #### Checked
Csvn = 20.5
Rsvn = 0.075
pt0svn = 0.0
qt0svn = 0.0
## Valve Parameters
Zao = 0.033
Rmv = 0.06
end
@parameters t
@named LV = ShiChamber(V₀=v0_lv, p₀ = p0_lv, Eₘᵢₙ=Emin_lv, Eₘₐₓ=Emax_lv, τ=τ, τₑₛ=τes_lv, τₑₚ=τed_lv, Eshift=0.0)
@named LA = ShiChamber(V₀=v0_la, p₀ = p0_la, Eₘᵢₙ=Emin_la, Eₘₐₓ=Emax_la, τ=τ, τₑₛ=τpww_la/2, τₑₚ=τpww_la, Eshift=τpwb_la)
# Examine how atrium is different
@named AV = ResistorDiode(R = Zao)
@named MV = ResistorDiode(R = Rmv)
@named Sys_loop = ShiSystemicLoop(SAS_C=Csas, SAS_R=Rsas, SAS_L=Lsas, SAT_C=Csat, SAT_R=Rsat, SAT_L=Lsat, SAR_R=Rsar, SCP_R=Rscp, SVN_C=Csvn, SVN_R=Rsvn)
function ShiSystemicLoop(; name, SAS_C, SAS_R, SAS_L, SAT_C, SAT_R, SAT_L, SAR_R, SCP_R, SVN_C, SVN_R)
@named in = Pin()
@named out = Pin()
sts = @variables Δp(t) = 0.0 q(t) = 0.0
ps = []
# No parameters in this function
# These are the components the subsystem is made of:
## Systemic Aortic Sinus ##
@named SAS = CRL(C=SAS_C, R=SAS_R, L=SAS_L)
## Systemic Artery ##
@named SAT = CRL(C=SAT_C, R=SAT_R, L=SAT_L)
## Systemic Arteriole ##
@named SAR = Resistor(R=SAR_R)
## Systemic Capillary ##
@named SCP = Resistor(R=SCP_R)
## Systemic Vein ##
@named SVN = CR(C=SVN_C, R=SVN_R)
# The equations for the subsystem are created by
# 'connect'-ing the components
eqs = [
Δp ~ out.p - in.p
q ~ in.q
connect(in, SAS.in)
connect(SAS.out, SAT.in)
connect(SAT.out, SAR.in)
connect(SAR.out, SCP.in)
connect(SCP.out, SVN.in)
connect(SVN.out, out)
]
# and finaly compose the system
compose(ODESystem(eqs, t, sts, ps; name=name), in, out, SAS, SAT, SAR, SCP, SVN)
end
circ_eqs = [
connect(LV.out, AV.in)
connect(AV.out, Sys_loop.in)
connect(Sys_loop.out, LA.in)
connect(LA.out, MV.in)
connect(MV.out, LV.in)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
##
@named circ_model = compose(_circ_model,[LV, LA, AV, MV, Sys_loop ])
## And simplify it
@time circ_sys = structural_simplify(circ_model)
u0 = [LV_Vt0, LA_Vt0, pt0sas, qt0sas , pt0sat, qt0sat, pt0svn]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 20.0))
@time sol = solve(prob, Vern7(), reltol = 1e-9, abstol = 1e-9)
plot(sol, tspan = (15, 16))
plot(sol, idxs = [LV.p, Sys_loop.SAS.C.p], tspan = (15, 16))
### Shi Valve with CallBacks - Systemic Loop
function ShiValve(; name, CQ, Kp, Kf, Kb, Kv, θmax, θmin)
@named oneport = OnePort()
@unpack Δp, q = oneport
ps = @parameters CQ = CQ Kp = Kp Kf = Kf Kb = Kb Kv = Kv θmax = θmax θmin = θmin
sts = @variables θ(t) = 0.0 ω(t) = 0.0 AR(t) = 0.0 Fp(t) = 0.0 Ff(t) = 0.0 Fb(t) = 0.0 Fv(t) = 0.0 F(t) = 0.0
D = Differential(t)
limits = [
[(θ ~ θmax)] => [ω ~ 0]
[(θ ~ θmin)] => [ω ~ 0]
]
# make θmax the real opening angle and define a θmaxopen for a healthy valve
# that means we can use θmax as a stenosis parameter
θmaxopen = 75 * pi / 180
eqs = [
# Forces/Moments
Fp ~ Kp * -Δp * cos(θ) # pressure
Ff ~ -Kf * ω # friction
Fb ~ Kb * q * cos(θ) # Fluid Velocity
Fv ~ -Kv * q * (q > 0) * sin(2θ) # vortex behind leaflets
F ~ Fp + Ff + Fb + Fv # total force/moment on leaflets
#ODEs
D(θ) ~ ω
D(ω) ~ F * ((θ < θmax) * (F > 0) + (θ > θmin) * (F < 0))
# Opening ratio
#AR ~ ((1 - cos(θ))^2) / ((1 - cos(θmax))^2)
AR ~ ((1 - cos(θ))^2) / ((1 - cos(θmaxopen))^2)
# Flow equation
q ~ -sign(Δp) * CQ * AR * sqrt(abs(Δp))
]
# include the `continuous_events` definition `limits` in the ODE system
# this is the MTK equivalent to callbacks
extend(ODESystem(eqs, t, sts, ps; name=name, continuous_events=limits), oneport)
end
begin
τ = 1.0
v0_lv = 5.0
p0_lv = 1.0
Emin_lv = 0.1
Emax_lv = 2.5
τes_lv = 0.3
τed_lv = 0.45
Eshift_lv = 0.0
LV_Vt0 = 500
### LA Atrium Parameters #### Checked
v0_la = 4.0
p0_la = 1.0
Emin_la = 0.15
Emax_la = 0.25
τpwb_la = 0.92
τpww_la = 0.09
τes_la = τpww_la/2
τed_la = τpww_la
Eshift_la = τpwb_la
LA_Vt0 = 20
#####
Csas = 0.08
Rsas = 0.003
Lsas = 6.2e-5
pt0sas = 100.0
qt0sas = 0.0
## Systemic Artery #### Checked
Csat = 1.6
Rsat = 0.05
Lsat = 0.0017
pt0sat = 100.0
qt0sat = 0.0
## Systemic Arteriole #### Checked
Rsar = 0.5
## Systemic Capillary #### Checked
Rscp = 0.52
## Systemic Vein #### Checked
Csvn = 20.5
Rsvn = 0.075
pt0svn = 0.0
qt0svn = 0.0
## Valve Parameters
Zao = 0.033
Rmv = 0.06
end
θmax = 75.0*pi/180
θmin = 5.0*pi/180
Kp_av = 5500.0
Kf_av = 50.0
Kb_av = 2.0
Kv_av = 7.0
Kf_mv = 50.0
Kp_mv = 5500.0
Kb_mv = 2.0
Kv_mv = 3.5
CQ_AV = 350.0
CQ_MV = 400.0
@parameters t
@named LV = ShiChamber(V₀=v0_lv, p₀ = p0_lv, Eₘᵢₙ=Emin_lv, Eₘₐₓ=Emax_lv, τ=τ, τₑₛ=τes_lv, τₑₚ=τed_lv, Eshift=0.0)
@named LA = ShiChamber(V₀=v0_la, p₀ = p0_la, Eₘᵢₙ=Emin_la, Eₘₐₓ=Emax_la, τ=τ, τₑₛ=τpww_la/2, τₑₚ=τpww_la, Eshift=τpwb_la)
@named AV = ShiValve(CQ = CQ_AV, Kp = Kp_av, Kf = Kf_av, Kb = Kb_av, Kv = Kv_av, θmax = θmax, θmin = θmin)
@named MV = ShiValve(CQ = CQ_MV, Kp = Kp_mv, Kf = Kf_mv, Kb = Kb_mv, Kv = Kv_mv, θmax = θmax, θmin = θmin)
@named Sys_loop = ShiSystemicLoop(SAS_C=Csas, SAS_R=Rsas, SAS_L=Lsas, SAT_C=Csat, SAT_R=Rsat, SAT_L=Lsat, SAR_R=Rsar, SCP_R=Rscp, SVN_C=Csvn, SVN_R=Rsvn)
circ_eqs = [
connect(LV.out, AV.in)
connect(AV.out, Sys_loop.in)
connect(Sys_loop.out, LA.in)
connect(LA.out, MV.in)
connect(MV.out, LV.in)
]
## Compose the whole ODAE system
@named _circ_model = ODESystem(circ_eqs, t)
##
@named circ_model = compose(_circ_model,[LV, LA, AV, MV, Sys_loop ])
## And simplify it
@time circ_sys = structural_simplify(circ_model)
u0 = [LV_Vt0, LA_Vt0, 0.0, 0.0, 0.0, 0.0, pt0sas, qt0sas , pt0sat, qt0sat, pt0svn]
@time prob = ODAEProblem(circ_sys, u0, (0.0, 20.0))
@time sol = solve(prob, Vern7(), reltol = 1e-9, abstol = 1e-9)
plot(sol, idxs = [LV.p, Sys_loop.SAS.C.p], tspan = (15, 16))
# Don't have to write new ode systems can focus on a single element
## Add pulmonary Loop
begin
τ = 1.0
Eshift=0.0
Ev=Inf
#### LV chamber parameters #### Checked
v0_lv = 5.0
p0_lv = 1.0
Emin_lv = 0.1
Emax_lv = 2.5
τes_lv = 0.3
τed_lv = 0.45
Eshift_lv = 0.0
#### RV Chamber parameters #### Checked
v0_rv = 10.0
p0_rv = 1.0
Emin_rv = 0.1
Emax_rv = 1.15
τes_rv = 0.3
τed_rv = 0.45
Eshift_rv = 0.0
### LA Atrium Parameters #### Checked
v0_la = 4.0
p0_la = 1.0