-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptiveODE.m
1308 lines (1282 loc) · 47.3 KB
/
adaptiveODE.m
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
% Generation of Fig 4 in the paper 'Numerical integration of mechanical forces in
% center-based models for biological cell populations' by P Lotstedt and S Mathias
spheroid=1;
% initalize force parameters
[s,a,ra,mu]=initiasph(1);
% load center coordinates (xc00(1:nc00),yc00(1:nc00),zc00(1:nc00))
% spheroid coordinates
load coords_spheroid_217cells.txt
% number of cells
nc00=217;
xc00=coords_spheroid_217cells(:,1);
yc00=coords_spheroid_217cells(:,2);
zc00=coords_spheroid_217cells(:,3);
% dt0 is initial provisional time step
ntim=100; dt0=0.0001; kmax=10;
dte12=zeros(ntim,kmax);
% number of function evaluations for the methods
fevfEconst=zeros(kmax,1); % fEc
fev12=zeros(kmax,1); % fE
fev12m=fev12; % mfE
fev23=fev12; % RK2
fevi12=fev12; % bE
fevi23=fev12; % TM
% number of proliferations for each \Delta t_{div}
ndtmax=10;
% number of \Delta t_{div}
ndelmax=14;
% total cpu time for each \Delta t_{div} for 6 methods
cputot=zeros(ndelmax,6);
fevtot=zeros(ndelmax,6);
cput=zeros(ndtmax,6);
fev=zeros(ndtmax,6);
tedim=100*(ndtmax+1);
% local error bound
eps=0.005;
% time between proliferations, abscissa in Fig 4
tdel=zeros(ndelmax,1);
tdeltot=zeros(ndelmax,1);
tdel(1)=0.02; tdel(2)=0.05; tdel(3)=0.1; tdel(4)=0.15; tdel(5)=0.2;
tdel(6)=0.25; tdel(7)=0.3; tdel(8)=0.4; tdel(9)=0.5; tdel(10)=0.7;
tdel(11)=1.0; tdel(12)=1.3; tdel(13)=1.6; tdel(14)=2.0;
%
for kdelta=1:ndelmax
%
tdelta=zeros(ndtmax,1);
for j=1:ndtmax
tdelta(j)=tdel(kdelta);
end
% measure cpu time for different proliferation rates
% (xc(n,j),yc(n,j)) coordinates of n:th cell at time t_j, n=1:nc, j=1:ntim,
% time points in t(1:ntim)
% solution with forward Euler (fE)
nte12tot=0;
% initial cell coordinates
xc0=xc00;
yc0=yc00;
zc0=zc00;
nc=nc00;
te12gl=zeros(tedim,1);
rseed=rng;
for ndt=1:ndtmax
dtim=tdelta(ndt);
intst=(ndt==1);
% integrate between 0 and dtim from (xc0,yc0,zc0) to (xce12,yce12,zce12) with fE
tic
[xce12,yce12,zce12,te12,nte12,fev12(ndt),dt1]=exp3Dord12(ntim,dtim,nc,xc0,yc0,zc0,dt0,1,intst,eps,s,a,ra,mu);
% cpu time for this \Delta t_{div}
cput(ndt,1)=toc;
% F evaluations for this \Delta t_{div}
fev(ndt,1)=fev12(ndt);
% a new cell is added in proliferation to final solution above, separation between cells is 0.3*s
[nc1,xc0,yc0,zc0]=addpart(nc,xce12(:,nte12),yce12(:,nte12),zce12(:,nte12),0.3*s);
if ndt>1
te12end=te12gl(nte12tot);
else
te12end=0;
te12first=te12;
nte12first=nte12;
end
% time points for the solution
for j=1:nte12
te12gl(nte12tot+j)=te12(j)+te12end;
end
nte12tot=nte12tot+nte12;
nc=nc1;
dt0=dt1;
end
% determine the time steps in the fE solution
dte120=dtextr(nte12first,te12first);
nte120=nte12first-2;
% forward Euler with constant time step (fEc) and at least the same
% accuracy as fE, constant time step is chosen to be the smallest time step for adaptive fE
dtconst=min(dte120(1:nte120));
nte1consttot=0;
xc0=xc00;
yc0=yc00;
zc0=zc00;
nc=nc00;
te1constgl=zeros(tedim,1);
rng(rseed);
for ndt=1:ndtmax
dtim=tdelta(ndt);
intst=0;
% integrate between 0 and dtim from (xc0,yc0,zc0) to (xce1const,yce1const,zce1const) with fEc
tic
[xce1const,yce1const,zce1const,te1const,nte1const,fevfEconst(ndt),dt1]=exp3Dord12(ntim,dtim,nc,xc0,yc0,zc0,dtconst,0,intst,eps,s,a,ra,mu);
% cpu time for this \Delta t_{div}
cput(ndt,2)=toc;
% F evaluations for this \Delta t_{div}
fevfEconst(ndt)=ceil(dtim/dtconst);
fev(ndt,2)=fevfEconst(ndt);
% a new cell is added in proliferation to final solution above
[nc1,xc0,yc0,zc0]=addpart(nc,xce1const(:,nte1const),yce1const(:,nte1const),zce1const(:,nte1const),0.3*s);
if ndt>1
te1constend=te1constgl(nte1consttot);
else
te1constend=0;
end
% time points for the solution
for j=1:nte1const
te1constgl(nte1consttot+j)=te1const(j)+te1constend;
end
nte1consttot=nte1consttot+nte1const;
nc=nc1;
end
% solution with multirate forward Euler (mfE)
nte12mtot=0;
xc0=xc00;
yc0=yc00;
zc0=zc00;
nc=nc00;
te12mgl=zeros(tedim,1);
rng(rseed);
for ndt=1:ndtmax
dtim=tdelta(ndt);
% integrate between 0 and dtim from (xc0,yc0,zc0) to (xce12m,yce12m,zce12m) with mfE
tic
[xce12m,yce12m,zce12m,te12m,nte12m,fev12m(ndt),dt1]=exp3Dord12adap(ntim,dtim,6,nc,xc0,yc0,zc0,dt0,eps,s,a,ra,mu);
% cpu time for this \Delta t_{div}
cput(ndt,3)=toc;
% F evaluations for this \Delta t_{div}
fev(ndt,3)=fev12m(ndt);
% a new cell is added in proliferation to final solution above
[nc1,xc0,yc0,zc0]=addpart(nc,xce12m(:,nte12m),yce12m(:,nte12m),zce12m(:,nte12m),0.3*s);
if ndt>1
te12mend=te12mgl(nte12mtot);
else
te12mend=0;
end
% time points for the solution
for j=1:nte12m
te12mgl(nte12mtot+j)=te12m(j)+te12mend;
end
nte12mtot=nte12mtot+nte12m;
nc=nc1;
end
% solution with Runge-Kutta, 2nd order
nte23tot=0;
xc0=xc00;
yc0=yc00;
zc0=zc00;
nc=nc00;
te23gl=zeros(tedim,1);
rng(rseed);
for ndt=1:ndtmax
dtim=tdelta(ndt);
intst=(ndt==1);
% integrate between 0 and dtim from (xc0,yc0,zc0) to (xce23,yce23,zce23) with RK2
tic
[xce23,yce23,zce23,te23,nte23,fev23(ndt),dt1]=exp3Dord23(ntim,dtim,nc,xc0,yc0,zc0,dt0,1,intst,eps,s,a,ra,mu);
% cpu time for this \Delta t_{div}
cput(ndt,4)=toc;
% F evaluations for this \Delta t_{div}
fev(ndt,4)=fev23(ndt);
% a new cell is added in proliferation to final solution above
[nc1,xc0,yc0,zc0]=addpart(nc,xce23(:,nte23),yce23(:,nte23),zce23(:,nte23),0.3*s);
if ndt>1
te23end=te23gl(nte23tot);
else
te23end=0;
end
% time points for the solution
for j=1:nte23
te23gl(nte23tot+j)=te23(j)+te23end;
end
nte23tot=nte23tot+nte23;
nc=nc1;
dt0=dt1;
end
% solution with backward Euler
nti12tot=0;
xc0=xc00;
yc0=yc00;
zc0=zc00;
nc=nc00;
ti12gl=zeros(tedim,1);
rng(rseed);
for ndt=1:ndtmax
dtim=tdelta(ndt);
intst=(ndt==1);
% integrate between 0 and dtim from (xc0,yc0,zc0) to (xci12,yci12,zci12) with bE
tic
[xci12,yci12,zci12,ti12,nti12,iteri12,fevi12(ndt),dt1]=imp3Dord12(ntim,dtim,nc,xc0,yc0,zc0,dt0,1,intst,eps,s,a,ra,mu);
% cpu time for this \Delta t_{div}
cput(ndt,5)=toc;
% F evaluations for this \Delta t_{div}
fev(ndt,5)=fevi12(ndt);
% a new cell is added in proliferation to final solution above
[nc1,xc0,yc0,zc0]=addpart(nc,xci12(:,nti12),yci12(:,nti12),zci12(:,nti12),0.3*s);
if ndt>1
ti12end=ti12gl(nti12tot);
else
ti12end=0;
end
% time points for the solution
for j=1:nti12
ti12gl(nti12tot+j)=ti12(j)+ti12end;
end
nti12tot=nti12tot+nti12;
nc=nc1;
dt0=dt1;
end
% solution with trapezoidal method
nti23tot=0;
xc0=xc00;
yc0=yc00;
zc0=zc00;
nc=nc00;
ti23gl=zeros(tedim,1);
rng(rseed);
for ndt=1:ndtmax
dtim=tdelta(ndt);
intst=(ndt==1);
% integrate between 0 and dtim from (xc0,yc0,zc0) to (xci23,yci23,zci23) with TM
tic
[xci23,yci23,zci23,ti23,nti23,iteri23,fevi23(ndt),dt1]=imp3Dord23(ntim,dtim,nc,xc0,yc0,zc0,dt0,1,intst,eps,s,a,ra,mu);
% cpu time for this \Delta t_{div}
cput(ndt,6)=toc;
% F evaluations for this \Delta t_{div}
fev(ndt,6)=fevi23(ndt);
% a new cell is added in proliferation to final solution above
[nc1,xc0,yc0,zc0]=addpart(nc,xci23(:,nti23),yci23(:,nti23),zci23(:,nti23),0.3*s);
if ndt>1
ti23end=ti23gl(nti23tot);
else
ti23end=0;
end
% time points for the solution
for j=1:nti23
ti23gl(nti23tot+j)=ti23(j)+ti23end;
end
nti23tot=nti23tot+nti23;
nc=nc1;
dt0=dt1;
end
% sum contribution from ndtmax intervals
[cputot1,fevtot1,tdeltot1]=cpufevsum(kdelta,ndtmax,cputot,fevtot,tdeltot,cput,fev,tdel(kdelta));
cputot=cputot1;
fevtot=fevtot1;
tdeltot=tdeltot1;
kdelta
% end of delta loop
end
%
% plot summed cpu time and force evals for each delta and ODE method
res=plotcpufev(ndelmax,cputot,fevtot,tdeltot);
function [nc1,xc1,yc1,zc1]=addpart(nc0,xc0,yc0,zc0,r0)
% initiate proliferation of cell ipart at random location
xc1=xc0;
yc1=yc0;
zc1=zc0;
% a random cell
ipart=floor(1+(nc0-0.1)*rand);
dirpart(1)=2*rand-1;
dirpart(2)=2*rand-1;
dirpart(3)=2*rand-1;
dirnorm=norm(dirpart);
dirpart(:)=0.5*r0*dirpart(:)/dirnorm;
xc1(ipart)=xc0(ipart)+dirpart(1);
yc1(ipart)=xc0(ipart)+dirpart(2);
zc1(ipart)=xc0(ipart)+dirpart(3);
nc1=nc0+1;
xc1(nc1)=xc0(ipart)-dirpart(1);
yc1(nc1)=xc0(ipart)-dirpart(2);
zc1(nc1)=xc0(ipart)-dirpart(3);
function [xd,yd,zd,k1,iter,fev]=bE3(i,xc,yc,zc,dt,N,fev0,epsN,s,a,ra,mu)
% backward Euler, one step
% input
% i: time point to advance to
% (xc(j,i-1),xc(j,i-1),xc(j,i-1)) j=1:N, initial coordinates
% dt: time step
% N: number of cells
% fev0: accumulated F evaluations
% epsN: tolerance in nonlinear iterations
% s,a,ra,mu: force parameters
% output
% (xd(j),yd(j),zd(j)) j=1:N, end coordinates
% k1: force vector at t(i)
% iter: number of iterations
% fev: updated F evaluations
x(1,:)=xc(:,i-1);
x(2,:)=yc(:,i-1);
x(3,:)=zc(:,i-1);
% solve k1=F(x+dt*k1), x1=x+dt*k1 by nonlinear conjugate gradient method
[x1,k1,iter,fev]=nonlincg3(N,x,x,dt,1,fev0,epsN,s,a,ra,mu);
xd(:)=x1(1,:);
yd(:)=x1(2,:);
zd(:)=x1(3,:);
end
function [cputot1,fevtot1,tdeltot1]=cpufevsum(k,ndtmax,cputot,fevtot,tdeltot,cput,fev,tdelta)
% sum cpu time and F evaluations for each method and divide by the number of intervals between proliferations
cputot1=cputot;
fevtot1=fevtot;
tdeltot1=tdeltot;
tdeltot1(k)=tdelta;
for j=1:6
cputot1(k,j)=sum(cput(:,j))/ndtmax;
end
for j=1:6
fevtot1(k,j)=sum(fev(:,j))/ndtmax;
end
function dt=dtextr(nt,t)
% determine the time steps from nt time points
nt0=nt-2;
dt=zeros(nt0,1);
for j=1:nt0
dt(j)=t(j+1)-t(j);
end
function [xc,yc,zc,t,nt,fev,dt1]=exp3Dord12(ntim,T,N,x0,y0,z0,dt0,errest,intst,eps,s,a,ra,mu)
% CBM in 3D using forward Euler (fE) and error estimate by RK2
% input
% T: final time
% N: number of cells
% (x0(i),y0(i),z0(i), i=1:N) initial cell coordinates
% if errest then local errors are estimated and the time step varied, otherwise the time step is constant dt0
% if intst then the integration is started by finding the first time step
% eps: local error parameter
% s, a, ra, mu: force parameters
% output
% (xc(i,j),yc(i,j),zc(i,j), i=1:N, j=1:nt) cell coordinates in nt time steps
% (t(j), j=1:nt) time points
% fev is number of function evaluations
ntim1=ntim+1;
xc=zeros(N,ntim1);
yc=zeros(N,ntim1);
zc=zeros(N,ntim1);
t=zeros(ntim1,1);
xc(:,1)=x0;
yc(:,1)=y0;
zc(:,1)=z0;
fev=0;
i=2;
ti=0;
if errest && intst
fev0=fev;
% find first step, dt0 initial guess, integrate by fE
[xc(:,i),yc(:,i),zc(:,i),k1,fev]=fE3(i,xc,yc,zc,dt0,N,fev0,s,a,ra,mu);
fev0=fev;
% integrate by RK2
[xd,yd,zd,k1,k2,fev]=RK23(i,xc,yc,zc,0,k1,dt0,N,fev0,s,a,ra,mu);
% determine the first time step
dt=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt0,eps,1);
else
dt=dt0;
end
while ti<T
% integrate until final time T
ti=t(i-1)+dt;
if ti>T
dt=T-t(i-1);
ti=T;
else
dtsav=dt;
end
t(i)=ti;
fev0=fev;
% take one step forward with fE from (xc(:,i-1),yc(:,i-1),zc(:,i-1)) to (xc(:,i),yc(:,i),zc(:,i)) with time step dt
[xc(:,i),yc(:,i),zc(:,i),k1,fev]=fE3(i,xc,yc,zc,dt,N,fev0,s,a,ra,mu);
% k1 evaluated at t(i-1)
if errest && (ti<T)
fev0=fev;
% take the step with RK2
[xd,yd,zd,k1,k2,fev]=RK23(i,xc,yc,zc,0,k1,dt,N,fev0,s,a,ra,mu);
% determine the new time step
dt=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt,eps,1);
end
i=i+1;
end
dt1=dtsav;
nt=i-1;
function [xc,yc,zc,t,nt,fev,dt1]=exp3Dord12adap(ntim,T,mstep,N,x0,y0,z0,dt0,eps,s,a,ra,mu)
% CBM in 3D using forward Euler, multirate time stepping and error estimate by Jacobian
% input
% T: final time
% mstep: number of substeps
% N: number of cells
% (x0(i),y0(i),z0(i), i=1:N) initial cell coordinates
% if errest then local errors are estimated and the time step varied, otherwise the time step is constant dt0
% eps: local error parameter
% s, a, ra, mu: force parameters
% output
% (xc(i,j),yc(i,j),zc(i,j), i=1:N, j=1:nt) cell coordinates in nt time steps
% (t(j), j=1:nt) time points
% fev is the number of function evaluations
ntim1=ntim+1;
xc=zeros(N,ntim1);
yc=zeros(N,ntim1);
zc=zeros(N,ntim1);
t=zeros(ntim1,1);
xc(:,1)=x0;
yc(:,1)=y0;
zc(:,1)=z0;
i=2;
ti=0;
fev=0;
while ti<T
fev0=fev;
% determine the new time step dt1
[dt1,K0,k0,fev]=newdtadapt3(N,xc(:,i-1),yc(:,i-1),zc(:,i-1),mstep,fev0,eps,s,a,ra,mu);
% k0 is the force at t(i-1)
dtsav=dt1;
fev0=fev;
ti=t(i-1)+dt1;
if ti>T
dt1=T-t(i-1);
ti=T;
end
t(i)=ti;
% take one step forward from (xc(:,i-1),yc(:,i-1),zc(:,i-1)) to (xc(:,i),yc(:,i),zc(:,i)) with time step dt1
[xc(:,i),yc(:,i),zc(:,i),fev]=fEadapt3(i,xc,yc,zc,k0,dt1,mstep,fev0,N,K0,s,a,ra,mu);
i=i+1;
end
dt1=dtsav;
nt=i-1;
function [xc,yc,zc,t,nt,fev,dt1]=exp3Dord23(ntim,T,N,x0,y0,z0,dt0,errest,intst,eps,s,a,ra,mu)
% CBM in 3D using RK2 and error estimate by RK3
% input
% T: final time
% N: number of cells
% (x0(i),y0(i),z0(i), i=1:N) initial cell coordinates
% if errest then local errors are estimated and the time step varied, otherwise the time step is constant dt0
% if intst then the integration is started by finding the first time step
% eps: local error parameter
% s, a, ra, mu: force parameters
% output
% (xc(i,j),yc(i,j),zc(i,j), i=1:N, j=1:nt) cell coordinates in nt time steps
% (t(j), j=1:nt) time points
% fev is number of function evaluations
ntim1=ntim+1;
xc=zeros(N,ntim1);
yc=zeros(N,ntim1);
zc=zeros(N,ntim1);
t=zeros(ntim1,1);
xc(:,1)=x0;
yc(:,1)=y0;
zc(:,1)=z0;
i=2;
ti=0;
fev0=0;
if errest && intst
% find first step, dt0 initial guess, integrate by RK2
[xc(:,i),yc(:,i),zc(:,i),k1,k2,fev1]=RK23(i,xc,yc,zc,1,x0,dt0,N,fev0,s,a,ra,mu);
fev0=fev1;
% integrate by RK3
[xd,yd,zd,fev1]=RK33(i,xc,yc,zc,k1,k2,dt0,N,fev0,s,a,ra,mu);
% determine the first time step
dt=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt0,eps,2);
else
dt=dt0;
k1=zeros(3,ntim1);
fev1=0;
end
while ti<T
% integrate until final time T
ti=t(i-1)+dt;
if ti>T
dt=T-t(i-1);
ti=T;
else
dtsav=dt;
end
t(i)=ti;
fev0=fev1;
% take one step forward with RK2 from (xc(:,i-1),yc(:,i-1),zc(:,i-1)) to (xc(:,i),yc(:,i),zc(:,i)) with time step dt
[xc(:,i),yc(:,i),zc(:,i),k1,k2,fev1]=RK23(i,xc,yc,zc,1,k1,dt,N,fev0,s,a,ra,mu);
if errest
fev0=fev1;
% take the step with RK3
[xd,yd,zd,fev1]=RK33(i,xc,yc,zc,k1,k2,dt,N,fev0,s,a,ra,mu);
% determine a new time step
dt1=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt,eps,2);
dt=dt1;
end
i=i+1;
end
dt1=dtsav;
nt=i-1;
fev=fev1;
function F=fcmp3(N,x,s,a,ra,mu)
% force calculation for CBM in 3D
% input
% N: number of cells
% x(1:3,j), j=1:N, cell coordinates
% s,a,ra,mu: force parameters
% output
% F(1:3,j), j=1:N, force vector
F=zeros(3,N);
for i=1:N
fsum=zeros(3,1);
for j=1:N
if i~=j
% diff is direction of the force between cell i and j
diff=x(:,j)-x(:,i);
ndiff=norm(diff);
% magnitude of force between two cells i and j
fabs=force(ndiff,s,a,ra,mu);
f=fabs*diff/ndiff;
fsum=fsum+f;
end
end
% sum of forces on cell i
F(:,i)=fsum;
end
function F=fcmpadapt3(N,K,x,s,a,ra,mu)
% force calculation for selected CBM in 3D
% input
% N: number of cells
% K: set of selected cells
% x(1:3,j), j=1:N, cell coordinates
% s,a,ra,mu: force parameters
% output
% F(1:3,j), j=1:N, force vector
F=zeros(3,N);
for i=1:N
if K(i)
% if K(i) then compute force, otherwise 0
fsum=zeros(3,1);
for j=1:N
if i~=j
% diff is direction of the force between cell i and j
diff=x(:,j)-x(:,i);
ndiff=norm(diff);
% magnitude of force between two cells i and j
fabs=force(ndiff,s,a,ra,mu);
f=fabs*diff/ndiff;
fsum=fsum+f;
end
end
F(:,i)=fsum;
end
end
function [xd,yd,zd,k1,fev]=fE3(i,xc,yc,zc,dt,N,fev0,s,a,ra,mu)
% forward Euler fE, one step in 3D
% input
% i: time point to advance to
% (xc(j,i-1),xc(j,i-1),xc(j,i-1)) j=1:N, initial coordinates
% dt: time step
% N: number of cells
% fev0: accumulated F evaluations
% s,a,ra,mu: force parameters
% output
% (xd(j),yd(j),zd(j)) j=1:N, end coordinates
% k1: force vector at t(i-1)
% fev: updated F evaluations
x(1,:)=xc(:,i-1);
x(2,:)=yc(:,i-1);
x(3,:)=zc(:,i-1);
% compute the force
k1=fcmp3(N,x,s,a,ra,mu);
for j=1:N
xd(j)=xc(j,i-1)+dt*k1(1,j);
yd(j)=yc(j,i-1)+dt*k1(2,j);
zd(j)=zc(j,i-1)+dt*k1(3,j);
end
fev=fev0+1;
function [xd,yd,zd,fev]=fEadapt3(i,xc,yc,zc,k0,dt1,mstep,fev0,N,K0,s,a,ra,mu)
% forward Euler, one step with multirate method in 3D
% input
% i: time point to advance to
% (xc(j,i-1),xc(j,i-1),xc(j,i-1)) j=1:N, initial coordinates
% k0: force vector evaluated at t(i-1)
% dt1: time step
% mstep: number of substeps
% fev0: accumulated F evaluations
% K0: set of fast particles
% N: number of cells
% s,a,ra,mu: force parameters
% output
% (xd(j),yd(j),zd(j)) j=1:N, end coordinates
% fev: updated F evaluations
k1=zeros(3,N);
dt0=dt1/mstep;
x(1,:)=xc(:,i-1);
x(2,:)=yc(:,i-1);
x(3,:)=zc(:,i-1);
% compute forces for slow particles in K1 at t(i-1)
K1=ones(N,1)-K0;
fev1=0;
sumK0=sum(K0);
if sumK0>0
% there are fast particles
% step one long step forward with the fast particles
for k=1:mstep
% take mstep short steps
% compute forces for fast particles in K0
if k>1
% compute forces selected by K0
k1=fcmpadapt3(N,K0,x,s,a,ra,mu);
else
% use forces in k0 in the first substep
for j=1:N
if K0(j)
k1(:,j)=k0(:,j);
end
end
end
x1=x+dt0*k1;
x=x1;
end
% update F evaluations, only part of the force vector has been changed
fev1=fev1+(mstep-1)*sumK0/N;
end
% step one long step with the slow particles
for j=1:N
if K1(j)
% integrate slow particles
xd(j)=xc(j,i-1)+dt1*k0(1,j);
yd(j)=yc(j,i-1)+dt1*k0(2,j);
zd(j)=zc(j,i-1)+dt1*k0(3,j);
else
% fast particles have already been integrated
xd(j)=x(1,j);
yd(j)=x(2,j);
zd(j)=x(3,j);
end
end
fev=fev0+fev1;
function f=force(r,s,a,ra,mu)
% the force between two cells
% input
% r: distance between cell centers
% s,a,ra,mu: force parameters in (4)
% output
% f: force magnitude
if r<ra
f=mu*(r-ra)^2*(r-s);
else
f=0;
end
function [xc,yc,zc,t,nt,iter,fev,dt1]=imp3Dord12(ntim,T,N,x0,y0,z0,dt0,errest,intst,eps,s,a,ra,mu)
% CBM in 3D using backward Euler (bE) and error estimate by RK2
% input
% T: final time
% N: number of cells
% (x0(i),y0(i),z0(i), i=1:N) initial cell coordinates
% if errest then local errors are estimated and the time step varied, otherwise the time step is constant dt0
% if intst then the integration is started by finding the first time step
% eps: local error parameter
% s, a, ra, mu: force parameters
% output
% (xc(i,j),yc(i,j),zc(i,j), i=1:N, j=1:nt) cell coordinates in nt time steps
% (t(j), j=1:nt) time points
% (iter(j), j=1:nt) is the number of iterations in each time step
% fev is number of function evaluations
% dt1: final dt
ntim1=ntim+1;
xc=zeros(N,ntim1);
yc=zeros(N,ntim1);
zc=zeros(N,ntim1);
t=zeros(ntim1,1);
iter=zeros(ntim1,1);
% tolerance in nonlinear iterations
epsN=1e-2*eps;
xc(:,1)=x0;
yc(:,1)=y0;
zc(:,1)=z0;
i=2;
ti=0;
fev0=0;
if errest
% find first step, dt0 initial guess, integrate with bE
[xc(:,i),yc(:,i),zc(:,i),k1,iter(i-1),fev1]=bE3(i,xc,yc,zc,dt0,N,fev0,epsN,s,a,ra,mu);
fev0=fev1;
% integrate with RK2
[xd,yd,zd,k1,k2,fev1]=RK23(i,xc,yc,zc,1,k1,dt0,N,fev0,s,a,ra,mu);
% determine the first time step
dt=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt0,eps,1);
fev0=fev1;
else
dt=dt0;
end
while ti<T
% integrate until final time T
ti=t(i-1)+dt;
if ti>T
dt=T-t(i-1);
ti=T;
else
dtsav=dt;
end
t(i)=ti;
% take one step forward with bE from (xc(:,i-1),yc(:,i-1),zc(:,i-1)) to (xc(:,i),yc(:,i),zc(:,i)) with time step dt
[xc(:,i),yc(:,i),zc(:,i),k11,iter(i),fev1]=bE3(i,xc,yc,zc,dt,N,fev0,epsN,s,a,ra,mu);
% k11 at t(i)
if errest && (ti<T)
fev0=fev1;
% take the step with RK2
% k1 at t(i-1)
[xd,yd,zd,k1,k2,fev1]=RK23(i,xc,yc,zc,1,k1,dt,N,fev0,s,a,ra,mu);
k1=k11;
% determine the new time step
dt1=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt,eps,1);
dt=dt1;
end
fev0=fev1;
i=i+1;
end
dt1=dtsav;
nt=i-1;
fev=fev1;
function [xc,yc,zc,t,nt,iter,fev,dt1]=imp3Dord23(ntim,T,N,x0,y0,z0,dt0,errest,intst,eps,s,a,ra,mu)
% CBM in 3D using trapezoidal method (TM) and error estimate by RK*
% input
% ntim: upper bound on the number of time steps
% T: final time
% N: number of cells
% (x0(i),y0(i),z0(i), i=1:N) initial cell coordinates
% if errest then local errors are estimated and the time step varied, otherwise the time step is constant dt0
% if intst then the integration is started by finding the first time step
% eps: local error parameter
% s, a, ra, mu: force parameters
% output
% (xc(i,j),yc(i,j),zc(i,j), i=1:N, j=1:nt) cell coordinates in nt time steps
% (t(j), j=1:nt) time points
% (iter(j), j=1:nt) is the number of iterations in each time step
% fev is number of function evaluations
% dt1: final dt
ntim1=ntim+1;
xc=zeros(N,ntim1);
yc=zeros(N,ntim1);
zc=zeros(N,ntim1);
t=zeros(ntim1,1);
iter=zeros(ntim1,1);
% tolerance in nonlinear iterations
epsN=1e-2*eps;
xc(:,1)=x0;
yc(:,1)=y0;
zc(:,1)=z0;
i=2;
ti=0;
fev0=0;
if errest
% find first step, dt0 initial guess, integrate with TM
[xc(:,i),yc(:,i),zc(:,i),k1,k2,iter(i-1),fev1]=TM3(i,xc,yc,zc,dt0,N,fev0,epsN,s,a,ra,mu);
fev0=fev1;
% integrate with RK*
[xd,yd,zd,fev1]=RKS3(i,xc,yc,zc,k1,k2,dt0,N,fev0,s,a,ra,mu);
% determine the first time step
dt=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt0,eps,2);
fev0=fev1;
else
dt=dt0;
end
dtsav=dt;
while ti<T
% integrate until final time T
ti=t(i-1)+dt;
if ti>T
dt=T-t(i-1);
ti=T;
else
dtsav=dt;
end
t(i)=ti;
% take one step forward with TM from (xc(:,i-1),yc(:,i-1),zc(:,i-1)) to (xc(:,i),yc(:,i),zc(:,i)) with time step dt
[xc(:,i),yc(:,i),zc(:,i),k1,k2,iter(i),fev1]=TM3(i,xc,yc,zc,dt,N,fev0,epsN,s,a,ra,mu);
if errest && (ti<T)
fev0=fev1;
% take the step with RK*
[xd,yd,zd,fev1]=RKS3(i,xc,yc,zc,k1,k2,dt,N,fev0,s,a,ra,mu);
% determine the new time step
dt1=newdt3(xc(:,i),yc(:,i),zc(:,i),xd',yd',zd',dt,eps,2);
dt=dt1;
end
fev0=fev1;
i=i+1;
end
dt1=dtsav;
nt=i-1;
fev=fev1;
function [s,a,ra,mu]=initiasph(N)
% initialize constants for CBM spheroid
s=1;
a=10;
ra=1.5;
mu=5.7;
function dt=newdt3(x0,y0,z0,x1,y1,z1,dt0,eps,iord)
% compute new time step by comparing (x0,y0,z0) with (x1,y1,z1) in max norm
% input
% (x0(j),y0(j),z0(j)) j=1:N, method of low order
% (x1(j),y1(j),z1(j)) j=1:N, method of high order
% dt0: previous time step
% eps: error tolerance
% iord: order of low order method
% output
% dt: new time step
xerr=max(abs(x0-x1));
yerr=max(abs(y0-y1));
zerr=max(abs(z0-z1));
err=max(xerr,max(yerr,zerr));
if iord==1
dt=dt0*sqrt(eps/err);
else
dt=dt0*(eps/err)^(1/3);
end
function [dt1,K0,k1,fev]=newdtadapt3(N,x0,y0,z0,mstep,fev0,eps,s,a,ra,mu)
% compute new time step dt1 for the slow particles in the multirate forward Euler method in 3D
% fev is number of force function evaluations
% input
% N: number of cells
% (x0(j),y0(j),z0(j)) j=1:N, coordinates at t(i-1)
% mstep: number of substeps
% fev0: accumulated F evaluations
% eps: error tolerance
% s,r,ra,mu: force parameters
% output
% dt1: new time step
% K0: the set of fast particles
% k1 is evaluated by (x0,y0,z0) at t(i-1)
% fev: F evaluations at t(i)
epsdt=1e-4;
x=zeros(3,N);
% sets of slow (K1) and fast (K0) cells
K1=zeros(N,1);
K0=ones(N,1);
for j=1:N
x(1,j)=x0(j);
x(2,j)=y0(j);
x(3,j)=z0(j);
end
% compute AF, the second derivative estimate, see (25)
k1=fcmp3(N,x,s,a,ra,mu);
x=x+epsdt*k1;
F=fcmp3(N,x,s,a,ra,mu);
% difference approximation of AF
AF=(F-k1)/epsdt;
fev=fev0+2;
% compute chi for error estimate
for j=1:N
maxAF(j)=max(abs(AF(1,j)),max(abs(AF(2,j)),abs(AF(3,j))));
end
k=1;
chia0=max(maxAF);
chia1=chia0/mstep;
% determine sets K0 and K1
for j=1:N
if maxAF(j) < chia1
% the set with long time steps
K1(j)=1;
end
end
% the set with short time steps, see (43)
K0=K0-K1;
% the long time step
dt1=sqrt(2*eps/chia1);
function [x1,f1,iter,fev]=nonlincg3(N,x0,f0,dt,c,fev0,eps,s,a,ra,mu)
% solve nonlinf3 by nonlinear conjugate gradient iterations
% to minimize F(x) with gradient f(x), theory according to Hager and Zhang
% input
% N: number of cells
% x0(1:3,j), j=1:N, cell coordinates
% f0(1:3,j), j=1:N, constant right hand side, b in (72), (73)
% c=1: backward Euler, c=1/2: trapezoidal method
% fev0: accumulated number of force function evaluations
% eps: iteration tolerance
% s,a,ra,mu: force parameters
% output
% x1(1:3,j), j=1:N, solution
% f1: the force at x1
% iter: number of iterations
% fev: updated F evaluations
epscg=1e-3;
p=zeros(3,N);
N2=2*N;
x=x0;
r0=nonlinf3(N,x,f0,dt,c,s,a,ra,mu);
p0=r0;
iter=0;
while (norm(r0)>eps)&&(iter<20)
% iterate until the l2 norm is sufficiently small or the number of iterations is 20
iter=iter+1;
for j=1:N
p(1,j)=p0(j);
p(2,j)=p0(N+j);
p(3,j)=p0(N2+j);
end
% approximate Jacobian A times p (A is Hessian of F)
x1=x+epscg*p;
r=nonlinf3(N,x1,f0,dt,c,s,a,ra,mu);
Ap=(r-r0)/epscg;
% minimize F(x) with alpha, F(x1)=F(x-alpha*p)<F(x), assuming
% quadratic approximation of F around x
alpha=r0'*r0/(p0'*Ap);
x1=x-alpha*p;
r1=nonlinf3(N,x1,f0,dt,c,s,a,ra,mu);
% beta according to Fletcher-Reeves [16]
beta=r1'*r1/(r0'*r0);
p0=r1+beta*p0;
r0=r1;
x=x1;
end
x1=x;
% the force at the final x
f1=fcmp3(N,x1,s,a,ra,mu);
fev=fev0+1+2*iter;
function [nlf]=nonlinf3(N,x0,f0,dt,c,s,a,ra,mu)
% compute the nonlinear function to be solved for x^{n+1} in bE and TM
% input
% N: number of cells
% x0(1:3,j), j=1:N, cell coordinates
% f0(1:3,j), j=1:N, constant right hand side in function
% dt: time step
% c: bE: c=1, TM: c=0.5
% s,a,ra,mu: force parameters
% output
% nlf(k), k=1:(3N) value of nonlinear function
nlf=zeros(3*N,1);
% compute the force at x0
frc=fcmp3(N,x0,s,a,ra,mu);
% value of nonlinear function
nlf0=x0-c*dt*frc-f0;
k=N;
l=2*N;
% reformat the output
for j=1:N
k=k+1;
l=l+1;
nlf(j)=nlf0(1,j);
nlf(k)=nlf0(2,j);
nlf(l)=nlf0(3,j);
end
function res=plotcpufev(ndelmax,cputot,fevtot,tdeltot)
% plot log10 of cpu time for 5 methods divided by the cpu time for fEc
cputot6=cputot(:,2);
fevtot6=fevtot(:,2);
% scaling by data for constant time step
for j=1:ndelmax
for k=1:6
cputot2(j,k)=cputot(j,k)/cputot6(j);
fevtot2(j,k)=fevtot(j,k)/fevtot6(j);
end
end
%subplot(2,1,1)
% Fig 4
plot(tdeltot(1:ndelmax),log10(cputot2(1:ndelmax,1)),'g*', ...
tdeltot(1:ndelmax),log10(cputot2(1:ndelmax,2)),'k-.',tdeltot(1:ndelmax),log10(cputot2(1:ndelmax,3)),'bo', ...
tdeltot(1:ndelmax),log10(cputot2(1:ndelmax,4)),'m^',tdeltot(1:ndelmax),log10(cputot2(1:ndelmax,5)),'c+', ...
tdeltot(1:ndelmax),log10(cputot2(1:ndelmax,6)),'rx')
% plot(tdeltot(1:ndelmax),cputot2(1:ndelmax,1),'g*', ...
% tdeltot(1:ndelmax),cputot2(1:ndelmax,2),'k-.',tdeltot(1:ndelmax),cputot2(1:ndelmax,3),'bo', ...
% tdeltot(1:ndelmax),cputot2(1:ndelmax,4),'m^',tdeltot(1:ndelmax),cputot2(1:ndelmax,5),'c+', ...
% tdeltot(1:ndelmax),cputot2(1:ndelmax,6),'rx')
%subplot(2,1,2)
%plot(tdeltot(1:ndelmax),log10(fevtot2(1:ndelmax,1)),'g*',tdeltot(1:ndelmax),log10(fevtot2(1:ndelmax,2)),'k-.', ...
% tdeltot(1:ndelmax),log10(fevtot2(1:ndelmax,3)),'bo', ...
% tdeltot(1:ndelmax),log10(fevtot2(1:ndelmax,4)),'m^',tdeltot(1:ndelmax),log10(fevtot2(1:ndelmax,5)),'c+', ...
% tdeltot(1:ndelmax),log10(fevtot2(1:ndelmax,6)),'rx')
res=0;
function [xd,yd,zd,k1,k2,fev]=RK23(i,xc,yc,zc,k1cmp,k1,dt,N,fev0,s,a,ra,mu)
% Runge's method RK2, order 2, one step in 3D
% input
% i: time point to advance to
% (xc(j,i-1),xc(j,i-1),xc(j,i-1)) j=1:N, initial coordinates
% if k1cmp then compute new k1
% k1: force vector at t(i-1)
% dt: time step
% N: number of cells
% fev0: accumulated F evaluations
% s,a,ra,mu: force parameters