-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchmmpmod.f90
4956 lines (4901 loc) · 172 KB
/
chmmpmod.f90
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
! Copyright 2021, Christian Hafner
!
! This file is part of OpenMaXwell.
!
! OpenMaXwell is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
! OpenMaXwell is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
! You should have received a copy of the GNU General Public License
! along with OpenMaXwell. If not, see <http://www.gnu.org/licenses/>.
MODULE CHMMP
! Expansions
USE CHINT
SAVE
Character(20), Parameter :: CHMmpIdent=' CHMMP Version 3.1 '
Complex(8), Allocatable :: MMPMtr(:),TriMtr(:),FldMtr(:,:),sMtr(:,:),xMtr(:),CGsi(:),CGpi(:),CGri(:),CGqi(:), &
& QRMtr(:,:),QRRMtr(:,:),QRWMtr(:)
Real(8), Allocatable :: cMtr(:),rMtr(:)
Real(8) residual,resUnscaled,resEigen,resCG,resPET,fMatrix0
Integer(4), Allocatable :: iMtr(:)
Integer(4) mCol,nRow,mRow,mTri,mMtr,iMtrSolver,iResCount,iunitRes,itmCG,itCG,iaccCG, &
& iAmpl,iAmplTyp,iupTri,ithvmax0,iScaleMatrix,mColsMtr,iGetEigFld,iEvlTest
Integer(2) iMMPfindExp,iMMPeigen,iMMPresV,iEvlDraw,iEvlCol
Logical lMMPrMtr,lMMPtMtr,lMMPrough,lMMPfine,lMMPuseRes,lMMP3F,lEvlSMP
Character(256) MmpFileName,CfkFileName
CONTAINS
Subroutine MMP_Defaults(lCheck)
! set default MMP parameters
Implicit none
Logical, intent(in) :: lCheck
Logical ldum
ldum=lCheck
if(.not.lgcFld) lGet3DMat=.true.
call setNameExt(ProFileName,'MMP',MmpFileName)
call setNameExt(ProFileName,'BND',CfkFileName)
call setNameExt(ProFileName,'BAS',PETFileName)
sPET(1)='1'C
sPET(2)='x'C
sPET(3)='pow(x,2)'C
sPET(4)='pow(x,3)'C
sPET(5)='pow(x,4)'C
sPET(6)='pow(x,5)'C
sPET(7)='pow(x,6)'C
sPET(8)='pow(x,7)'C
sPET(9)='pow(x,8)'C
sPET(10)='pow(x,9)'C
lMMPstat=.false.
if((cdAbs(fcFld).lt.pSmall).or.(.not.lfcFld)) lMMPstat=.true.
lMMPBndVal=.false.
iMtrSolver=5
iAmpl=1
iAmplTyp=0
iResCount=-1
iunitRes=0
residual=pBig
resUnscaled=pBig
resEigen=pBig
rMMPResV(1:9)=1.0d0
errorM=0.0d0
errorMA=0.0d0
errorA=0.0d0
Condition=-1.0d0
errorScale=0.0d0
BndDMax=0.2
BndPpW=5.0
BndOver=2.0
smallMatDist=1.0d-5
nSegPt=2
iMMPeigen=0_2
lMMPrMtr=.false.
lMMPtMtr=.false.
lMMPrough=.true.
lMMPfine=.true.
lMMPuseRes=.true.
lMMP3F=.false.
iMMPfindExp=0_2
iMMPresV=0_2
lPET=.false.
lEvlSMP=.false.
iEvlDraw=0_2
iEvlCol=1_2
nPET=3
kPET=0
kPETD=1
npPET=0
iMMPCon=0_2
iFunWeight=0_4
ithvmax0=7_4
itCG=0
itmCG=10
iaccCG=10
resCG=0.0d0
resPET=4.0d0
fMatrix0=0.0d0
iScaleMatrix=2
lMMPDeterminant=.false.
iMMPlast=0
nRow=1
mRow=100
iEvlTest=3
iWorkSpace=-1 ! check and set without display
call AllocateMtr(ldum)
if(.not.ldum) return
if(Allocated(MMPMtr)) then
MMPMtr=(1.0d0,0.0d0)
end if
end Subroutine MMP_Defaults
! Allocations
Subroutine AllocateMtr(ldum)
Implicit none
Integer(4) idum
Logical ldum
ldum=.false.
if(Allocated(QRMtr)) DeAllocate(QRMtr)
if(Allocated(QRRMtr)) DeAllocate(QRRMtr)
if(Allocated(QRWMtr)) DeAllocate(QRWMtr)
if(Allocated(MMPMtr)) DeAllocate(MMPMtr)
if(Allocated(FldMtr)) DeAllocate(FldMtr)
if(Allocated(TriMtr)) DeAllocate(TriMtr)
if(Allocated(cMtr)) DeAllocate(cMtr)
if(Allocated(xMtr)) DeAllocate(xMtr)
if(Allocated(rMtr)) DeAllocate(rMtr)
if(Allocated(sMtr)) DeAllocate(sMtr,stat=idum)
if(Allocated(iMtr)) DeAllocate(iMtr)
if(Allocated(CGri)) DeAllocate(CGri)
if(Allocated(CGsi)) DeAllocate(CGsi)
if(Allocated(CGpi)) DeAllocate(CGpi)
if(Allocated(CGqi)) DeAllocate(CGqi)
call getmCol()
mTri=max(mTri,1)
mRow=max(mRow,1)
mCol=max(mCol,1)
mColsMtr=max(mColsMtr,1)
nRHS=max(nRHS,1)
idum=0
if(iMtrSolver.ne.5) then
Allocate(TriMtr(1:mTri),stat=idum)
if(idum.eq.0) Allocate(cMtr(1:mCol),stat=idum)
end if
if(idum.eq.0) Allocate(FldMtr(1:10,1:mCol),stat=idum)
if(idum.eq.0) Allocate(rMtr(1:mCol),stat=idum)
if(iMtrSolver.lt.2) then
if(idum.eq.0) Allocate(sMtr(1:mCol,1:nRHS),stat=idum)
else
if(idum.eq.0) Allocate(sMtr(1:mColsMtr,1:nRHS),stat=idum)
end if
if(idum.eq.0) Allocate(xMtr(1:mCol),stat=idum)
if(idum.eq.0) Allocate(iMtr(1:mCol),stat=idum)
if(idum.ne.0) then
idum=MessageBoxQQ('Memory allocation for TriMtr failed!'C,'Allocate MMP matrices'C, &
MB$OK.or.MB$IconExclamation)
if(Allocated(TriMtr)) DeAllocate(TriMtr)
if(Allocated(FldMtr)) DeAllocate(FldMtr)
if(Allocated(cMtr)) DeAllocate(cMtr)
if(Allocated(rMtr)) DeAllocate(rMtr)
if(Allocated(sMtr)) DeAllocate(sMtr)
if(Allocated(xMtr)) DeAllocate(xMtr)
if(Allocated(iMtr)) DeAllocate(iMtr)
nRow=0
mRow=0
mCol=0
mTri=0
mMtr=0
return
end if
iupTri=0
if(iMtrSolver.ne.5) TriMtr(1:mTri)=(0.0d0,0.0d0)
FldMtr(1:10,1:mCol)=(0.0d0,0.0d0)
sMtr=(0.0d0,0.0d0)
xMtr(1:mCol)=(0.0d0,0.0d0)
if(iMtrSolver.ne.5) cMtr(1:mCol)=0.0d0
rMtr(1:mCol)=0.0d0
iMtr(1:mCol)=0_4
ldum=.true.
if((iMtrSolver.eq.2).or.(iMtrSolver.eq.3)) then ! allocate auxiliary arrays for CG
mMtr=mRow*mCol
mMtr=max(mMtr,1)
Allocate(MMPMtr(1:mMtr),CGsi(1:mCol),CGpi(1:mCol),CGri(1:mRow),CGqi(1:mRow),stat=idum)
if(idum.ne.0) then
idum=MessageBoxQQ('Memory allocation for MMPMtr failed!'C,'Allocate MMP-CG matrices'C, &
MB$OK.or.MB$IconExclamation)
if(Allocated(MMPMtr)) DeAllocate(MMPMtr)
if(Allocated(CGsi)) DeAllocate(CGsi)
if(Allocated(CGpi)) DeAllocate(CGpi)
if(Allocated(CGri)) DeAllocate(CGri)
if(Allocated(CGqi)) DeAllocate(CGqi)
mMtr=0
iMtrSolver=0
else
MMPMtr(1:mMtr)=(0.0d0,0.0d0)
CGsi(1:mCol)=(0.0d0,0.0d0)
CGpi(1:mCol)=(0.0d0,0.0d0)
CGri(1:mRow)=(0.0d0,0.0d0)
CGqi(1:mRow)=(0.0d0,0.0d0)
end if
else if(iMtrSolver.eq.1) then ! allocate auxiliary arrays for GUR
mMtr=mRow*mCol
Allocate(MMPMtr(1:mMtr),stat=idum)
if(idum.ne.0) then
idum=MessageBoxQQ('Memory allocation for MMPMtr failed!'C,'Allocate MMP-Rect matrices'C, &
MB$OK.or.MB$IconExclamation)
if(Allocated(MMPMtr)) DeAllocate(MMPMtr)
mMtr=0
iMtrSolver=0
else
MMPMtr(1:mMtr)=(0.0d0,0.0d0)
end if
else if(iMtrSolver.eq.5) then ! allocate auxiliary arrays for QR
if(iWorkSpace.gt.0) then
Allocate(QRMtr(mRow,mCol-nRHS),QRRMtr(mRow,nRHS),QRWMtr(iWorkSpace*mRow),stat=idum)
else
Allocate(QRMtr(mRow,mCol-nRHS),QRRMtr(mRow,nRHS),QRWMtr(1),stat=idum)
end if
if(idum.ne.0) then
idum=MessageBoxQQ('Memory allocation for QRMtr failed!'C,'Allocate MMP-Rect matrices'C, &
MB$OK.or.MB$IconExclamation)
if(Allocated(QRMtr)) DeAllocate(QRMtr)
if(Allocated(QRRMtr)) DeAllocate(QRRMtr)
if(Allocated(QRWMtr)) DeAllocate(QRWMtr)
iMtrSolver=0
end if
end if
end Subroutine AllocateMtr
! threads
Subroutine TMMPRes(lCheck)
Implicit none
Include 'resource.fd'
Logical(4), intent(in) :: lCheck
call WaitEndThread()
iThreadAction=1_4
call StartMMPThread(lCheck)
end Subroutine TMMPRes
Subroutine TMMPEvl(lCheck)
Implicit none
Include 'resource.fd'
Logical(4), intent(in) :: lCheck
call WaitEndThread()
iThreadAction=2_4
call StartMMPThread(lCheck)
end Subroutine TMMPEvl
Subroutine TMMPErr(lCheck)
Implicit none
Include 'resource.fd'
Logical(4), intent(in) :: lCheck
call WaitEndThread()
iThreadAction=3_4
call StartMMPThread(lCheck)
end Subroutine TMMPErr
Subroutine TMMPResErr(lCheck)
Implicit none
Include 'resource.fd'
Logical(4), intent(in) :: lCheck
call WaitEndThread()
iThreadAction=4_4
call StartMMPThread(lCheck)
end Subroutine TMMPResErr
Subroutine TMMPEvlErr(lCheck)
Implicit none
Include 'resource.fd'
Logical(4), intent(in) :: lCheck
call WaitEndThread()
iThreadAction=5_4
call StartMMPThread(lCheck)
end Subroutine TMMPEvlErr
Subroutine TMMPCnd(lCheck)
Implicit none
Include 'resource.fd'
Logical(4), intent(in) :: lCheck
call WaitEndThread()
iThreadAction=6_4
call StartMMPThread(lCheck)
end Subroutine TMMPCnd
Subroutine StartMMPThread(ldi)
! start the MMP thread
Implicit none
Include 'resource.fd'
Integer(INT_PTR_KIND()) iThread
Integer(4), Save:: iArgument
Integer(4) iStack,iCreation,idum
Logical, intent(in) :: ldi
Logical(4) ldum
if(lThreadStarted) return
call OutTxt('t3','start MMP thread'C)
call OutTxt('n3',' 'C)
call OutTxt('m3',' 'C)
lThreadStarted=.true.
iStack=0
iArgument=0
iCreation=0
iThread=0
if(ldi) iArgument=1_4
if(iThreadHandle.ne.0) then
call OutTxt('t3','close thread handle'C)
ldum=CloseHandle(iThreadHandle)
if(.not.ldum) then
idum=MessageBoxQQ('Cannot close thread handle'C,'Start MMP thread'C, &
MB$OK.or.MB$ICONEXCLAMATION)
lThreadStarted=.false.
return
end if
iThreadHandle=0
endif
call OutTxt('t3','Start MMP thread'C)
iThreadHandle=CreateThread(NULL,iStack,Loc(MMPThread),Loc(iArgument),iCreation,iThread)
if(iThreadHandle.eq.0) then
idum=MessageBoxQQ('Cannot create thread'C,'Start MMP thread'C, &
MB$OK.or.MB$ICONEXCLAMATION)
lThreadStarted=.false.
end if
end Subroutine StartMMPThread
Integer(4) Function MMPThread(iwhat)
! MMP tread: calls.....
Implicit none
Include 'resource.fd'
Integer(4) iwhat
Logical ldum
lStopThread=.false.
ldum=.false.
if(iWhat.ne.0) ldum=.true.
MMPThread=0_4
if(.not.lgcFld) lGet3DMat=.true.
if(iThreadAction.eq.1) then
resEigen=getRes(fcFld)
else if(iThreadAction.eq.2) then
call getEigen(ldum)
lEigen=.false.
ldum=lPET
lPET=.false.
resEigen=getRes(fcFld)
lPET=ldum
lEigen=.true.
else if(iThreadAction.eq.3) then
call getErrors(.true.)
else if(iThreadAction.eq.4) then
resEigen=getRes(fcFld)
call getErrors(.true.)
else if(iThreadAction.eq.5) then
call getEigen(ldum)
lEigen=.false.
ldum=lPET
lPET=.false.
resEigen=getRes(fcFld)
lPET=ldum
call getErrors(.true.)
lEigen=.true.
else if(iThreadAction.eq.6) then
if(Allocated(MMPMtr)) then
condition=getCondition(MMPMtr,mCol,nRow)
else
condition=-1.0d0
end if
else
MMPThread=1_4
end if
call endThread()
end Function MMPThread
! I/O
Subroutine SaveError(lCheck,l2D)
! save all current MMP error data in a function file
Implicit none
Logical, intent(in) :: lCheck
Logical l2
Logical, Optional, intent(in):: l2D
Integer(4) i,iOK,ios,idum
if(Present(l2D)) then
l2=l2D
else
l2=lgcFld
end if
if(.not.lCheck) then
call Open2write(-1,'Select error data file to be written!','Function data file ',FunFileName,'FUN',ios)
if(ios.gt.0) then
call closeFunction(1)
return
end if
if((.not.Present(l2D)).and.(.not.lgcFld)) then
l2=.false.
idum=MessageBoxQQ('Save errors along 2D boundaries?'C,'Save errors'C, &
MB$YESNO.or.MB$IconQuestion)
if(idum.eq.MB$IDYes) l2=.true.
end if
end if
open(1,file=FunFileName,iostat=ios)
if(ios.ne.0) then
idum=MessageBoxQQ('Error opening file!\rCannot save data!'C,'Save errors'C, &
MB$OK.or.MB$IconExclamation)
call closeFunction(1)
return
end if
call WriteStr(1,CHFunIdent,iOK)
ich(1)=0
ich(2)=3
if(l2.or.lgcFld) ich(2)=4
call chwrit2(1,ich,2,rch,0,sch,0,iOK)
call WriteStr(1,'n'C,iOK)
call WriteStr(1,'error'C,iOK)
call WriteStr(1,'field'C,iOK)
call WriteStr(1,'relative error'C,iOK)
if(l2.or.lgcFld) then
call WriteStr(1,'location'C,iOK)
do i=1,nBndPt
rch(1)=eBndPt(i)
rch(2)=fBndPt(i)
rch(3)=100.0d0*(eBndPt(i).div.fBndPt(i))
rch(4)=sBndPt(i)
call chwrit2(1,ich,0,rch,4,sch,0,iOK)
end do
else
do i=1,nBndPt3D
if(.not.lgcFld) then
if(iObjBndPt3D(i).lt.0) Cycle
end if
rch(1)=eBndPt3D(i)
rch(2)=fBndPt3D(i)
rch(3)=100.0d0*(eBndPt3D(i).div.fBndPt3D(i))
call chwrit2(1,ich,0,rch,3,sch,0,iOK)
end do
end if
sch(1:1)=' '
call chwrit2(1,ich,0,rch,0,sch,1,iOK)
EndFile(1)
call closeFunction(1)
end Subroutine SaveError
Subroutine SaveRmatrix(lCheck)
! save the current rectangular MMP matrix in a field file
Implicit none
Logical, intent(in) :: lCheck
Integer(4) i,iOK,ios,idum,ik,j
Character(3) EXT
if(.not.lCheck) then
call Open2write(-1,'Select matrix data file to be written!','Field file ',FldFileName,'FLD',ios)
if(ios.gt.0) then
call closeField(1)
return
end if
end if
open(1,file=FldFileName,iostat=ios)
if(ios.ne.0) then
idum=MessageBoxQQ('Error opening file!\rCannot save data!'C,'Save R matrix'C, &
MB$OK.or.MB$IconExclamation)
call closeField(1)
return
end if
call getNameExt(FldFileName,ext)
if((ext(1:3).ne.'fld').and.(ext(1:3).ne.'FLD')) then ! standard TXT file
ich(1)=mCol
ich(2)=nRow
sch(1:10)=' mCol,nRow'
call chwrit2(1,ich,2,rch,0,sch,10,iOK)
if(Allocated(MMPMtr)) then
! matrix data
ik=0
do j=1,nRow
do i=1,mCol
ik=ik+1
rch(1)=Dble(MMPMtr(ik))
rch(2)=DImag(MMPMtr(ik))
write(1,*) rch(1:2)
end do
end do
end if
ich(1)=nExp
sch(1:5)=' nExp'
call chwrit2(1,ich,1,rch,0,sch,5,iOK)
do i=1,nExp
ich(1)=tExp(i)%nPar
rch(1)=tExp(i)%Plane(1,0)
rch(2)=tExp(i)%Plane(2,0)
write(1,*) ich(1),rch(1:2)
end do
else
call WriteStr(1,CHFldIdent,iOK)
ich(1)=0
sch(1:23)=' no representation data'
call chwrit2(1,ich,1,rch,0,sch,23,iOK)
! cfield data
ich(2:6)=0
ich(1)=1
ich(7:10)=1
sch(1:10)=' lxcFld,...'
call chwrit2(1,ich,10,rch,0,sch,10,iOK)
ich(1)=mCol
ich(2)=nRow
ich(3)=1
sch(1:10)=' nxcFld,..'
call chwrit2(1,ich,3,rch,0,sch,10,iOK)
rch(1:3)=0.0d0
rch(2)=1.0d0
sch(1:8)=' origin '
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
rch(1:3)=0.0d0
rch(1)=1.0d0
sch(1:8)=' x tang.'
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
rch(1:3)=0.0d0
rch(2)=-1.0d0
sch(1:8)=' y tang.'
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
rch(1:3)=0.0d0
rch(3)=1.0d0
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
if(Allocated(MMPMtr)) then
! matrix data
ich(1)=1
sch(1:13)=' field values'
call chwrit2(1,ich,1,rch,0,sch,13,iOK)
ik=0
do j=1,nRow
do i=1,mCol
ich(1)=1
call chwrit2(1,ich,1,rch,0,sch,0,iOK)
ik=ik+1
rch(1)=Dble(MMPMtr(ik))
rch(2)=DImag(MMPMtr(ik))
call chwrit2(1,ich,0,rch,2,sch,0,iOK)
end do
end do
else
ich(1)=0
sch(1:16)=' no field values'
call chwrit2(1,ich,1,rch,0,sch,16,iOK)
end if
end if
sch(1:1)=' '
call chwrit2(1,ich,0,rch,0,sch,1,iOK)
EndFile(1)
call closeField(1)
end Subroutine SaveRmatrix
Subroutine SaveTmatrix(lCheck)
! save the current trapezoidal MMP matrix in a field file
Implicit none
Logical, intent(in) :: lCheck
Integer(4) i,iOK,ios,idum,ik,ik0,j,ikd
Character(3) EXT
if(.not.lCheck) then
call Open2write(-1,'Select matrix data file to be written!','Field file ',FldFileName,'FLD',ios)
if(ios.gt.0) then
call closeField(1)
return
end if
end if
open(1,file=FldFileName,iostat=ios)
if(ios.ne.0) then
idum=MessageBoxQQ('Error opening file!\rCannot save data!'C,'Save R matrix'C, &
MB$OK.or.MB$IconExclamation)
call closeField(1)
return
end if
call getNameExt(FldFileName,ext)
if((ext(1:3).ne.'fld').and.(ext(1:3).ne.'FLD')) then ! standard TXT file
ich(1)=mCol
ich(2)=mColsMtr
sch(1:10)=' mCol,nRow'
call chwrit2(1,ich,2,rch,0,sch,10,iOK)
if(Allocated(TriMtr)) then
! matrix data
ik0=0
do j=1,mColsMtr
ik0=ik0+j
do i=1,mCol
ikd=j
ik=ik0
if(i.ge.j) then
rch(1)=Dble(TriMtr(ik))
rch(2)=DImag(TriMtr(ik))
ik=ik+ikd
ikd=min(ikd+1,mColsMtr)
else
rch(1)=0.0d0
rch(2)=0.0d0
end if
call chwrit2(1,ich,0,rch,2,sch,0,iOK)
end do
end do
end if
else ! FLD file
call WriteStr(1,CHFldIdent,iOK)
ich(1)=0
sch(1:23)=' no representation data'
call chwrit2(1,ich,1,rch,0,sch,23,iOK)
! cfield data
ich(2:6)=0
ich(1)=1
ich(7:10)=1
sch(1:10)=' lxcFld,...'
call chwrit2(1,ich,10,rch,0,sch,10,iOK)
ich(1)=mCol
ich(2)=mColsMtr
ich(3)=1
sch(1:10)=' nxcFld,..'
call chwrit2(1,ich,3,rch,0,sch,10,iOK)
rch(1:3)=0.0d0
rch(2)=1.0d0
sch(1:8)=' origin '
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
rch(1:3)=0.0d0
rch(1)=1.0d0
sch(1:8)=' x tang.'
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
rch(1:3)=0.0d0
rch(2)=-1.0d0
sch(1:8)=' y tang.'
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
rch(1:3)=0.0d0
rch(3)=1.0d0
call chwrit2(1,ich,0,rch,3,sch,8,iOK)
if(Allocated(TriMtr)) then
! matrix data
ich(1)=1
sch(1:13)=' field values'
call chwrit2(1,ich,1,rch,0,sch,13,iOK)
ik0=0
do j=1,mColsMtr
ik0=ik0+j
do i=1,mCol
ikd=j
ik=ik0
ich(1)=1
call chwrit2(1,ich,1,rch,0,sch,0,iOK)
if(i.ge.j) then
rch(1)=Dble(TriMtr(ik))
rch(2)=DImag(TriMtr(ik))
ik=ik+ikd
ikd=min(ikd+1,mColsMtr)
else
rch(1)=0.0d0
rch(2)=0.0d0
end if
call chwrit2(1,ich,0,rch,2,sch,0,iOK)
end do
end do
else
ich(1)=0
sch(1:16)=' no field values'
call chwrit2(1,ich,1,rch,0,sch,16,iOK)
end if
end if
sch(1:1)=' '
call chwrit2(1,ich,0,rch,0,sch,1,iOK)
EndFile(1)
call closeField(1)
end Subroutine SaveTmatrix
Subroutine saveBasis(lCheck)
! save PET basis data in a file
Implicit none
Logical, intent(in) :: lCheck
Integer(4) iOk,ios,i,l,idum
if(.not.lCheck) then
call Open2write(-1,'Select PET basis data file to be written!','PET data file ',PETFileName,'BAS',ios)
if(ios.gt.0) return
end if
open(1,file=PETFileName,iostat=ios)
if(ios.ne.0) then
idum=MessageBoxQQ('Error opening file!\rCannot save data!'C,'Save PET data'C, &
MB$OK.or.MB$IconExclamation)
close(1)
return
end if
call WriteStr(1,CHPETIdent,iOK)
ich(1)=nPET
rch(1)=fPET
sch(1:13)=' nBas,fitness'
call chwrit2(1,ich,1,rch,1,sch,13,iOK)
do i=1,nPET
l=GetSLength(sPET(i))
write(1,'(a)') sPET(i)(1:l)
write(1,*) '1 0 0'
end do
close(1)
end Subroutine saveBasis
Subroutine openBasis(lCheck)
! read PET basis data from a file
Implicit none
Logical, intent(in) :: lCheck
Logical lFileExist
Integer(4) iOk,ios,i,ls,np,mp,ic,lci,lc,idum
Integer(2) l1s
Real(8) a,p,f
Character(1151) so
Character(20) text
if(.not.lCheck) then
call Open2read(-1,'Select PET basis data file to be read!','PET data file ',PETFileName,'BAS',ios)
if(ios.gt.0) return
end if
inquire(file=PETFileName,Exist=lFileExist)
if(.not.lFileExist) return
open(1,file=PETFileName,status='old',iostat=ios)
if(ios.ne.0) then
if(.not.lCheck) idum=MessageBoxQQ('Error opening file!\rCannot read data!'C,'Open PET data'C, &
MB$OK.or.MB$IconExclamation)
close(1)
return
end if
call ReadStr(1,text,iOK)
if(CHPETIdent(1:18).ne.text(1:18)) then
idum=MessageBoxQQ('Wrong file Type!\rContinue reading?'C,'Open PET data'C, &
MB$YesNo.or.MB$IconQuestion)
if(idum.eq.MB$IDNO) then
close(1)
return
end if
end if
read(1,*) nPET,f
nPET=min(nPET,mPET)
do i=1,nPET
read(1,'(a)') sPET(i)
read(1,*) a,p,f
ls=-1
call DelBlanks(sPET(i),ls)
call ToLower(sPET(i),ls)
! count number of parameters in the string
np=nParInString(sPET(i),ls)
! replace all parameters by the constant value p
call rswrit2(p,5_2,sch,l1s)
so(1:ls)=sPET(i)(1:ls)
do mp=1,np
ic=iParInString(sPET(i),ls,mp) ! get its position and value
lci=2
if(sPET(i)(ic+1:ic+1).ne.'0') lci=1
lc=ls+Int4(l1s)-lci
if(lc.gt.1150) then
Exit
end if
ls=lc
so(ic:ic+Int4(l1s-1_2))=sch(1:l1s)
so(ic+Int4(l1s):lc)=sPET(i)(ic+lci:ls)
end do
sPET(i)(1:ls)=so(1:ls)
end do
close(1)
fPET=0.0d0
end Subroutine openBasis
Subroutine SaveMmp(lCheck)
! save MMP Matrix data in a file
Implicit none
Logical, intent(in) :: lCheck
Integer(4) iOk,ios,i,k,ik,idum
if(.not.lCheck) then
call Open2write(-1,'Select MMP data file to be written!','MMP data file ',MmpFileName,'MMP',ios)
if(ios.gt.0) return
lMMPrMtr=.false.
if(Allocated(MMPMtr).and.(iMtrSolver.gt.0).and.(iMtrSolver.ne.4)) then
idum=MessageBoxQQ('Save rectangular MMP matrix?'C,'Save MMP data'C, &
MB$YesNo.or.MB$IconQuestion)
if(idum.eq.MB$IDYES) lMMPrMtr=.true.
end if
lMMPtMtr=.false.
if(Allocated(TriMtr).and.(iMtrSolver.ne.2).and.(iMtrSolver.ne.3)) then
idum=MessageBoxQQ('Save trapezoidal MMP matrix?'C,'Save MMP data'C, &
MB$YesNo.or.MB$IconQuestion)
if(idum.eq.MB$IDYES) lMMPtMtr=.true.
end if
end if
open(1,file=MmpFileName,iostat=ios)
if(ios.ne.0) then
idum=MessageBoxQQ('Error opening file!\rCannot save data!'C,'Save MMP data'C, &
MB$OK.or.MB$IconExclamation)
close(1)
return
end if
call WriteStr(1,CHMmpIdent,iOK)
ich(1)=nSegPt
rch(1)=BndDMax
rch(2)=BndPpW
rch(3)=BndOver
sch(1:11)=' nSegPt,...'
call chwrit2(1,ich,1,rch,3,sch,11,iOK)
ich(1)=iMtrSolver
if(.not.lMMPuseRes) ich(1)=ich(1)+100
ich(1)=ich(1)+1000*Int4(abs(iMMPfindExp))
if(iMMPfindExp.lt.0) ich(1)=ich(1)+10000
if(lMMP3F) ich(1)=ich(1)+1000000
ich(2)=iAmplTyp
ich(3)=iScaleMatrix
ich(4)=iAmpl
rch(1)=fMatrix0
sch(1:15)=' iMtrSolver,...'
call chwrit2(1,ich,4,rch,1,sch,15,iOK)
ich(1)=itmCG
ich(2)=iaccCG
ich(3)=iMMPLast
rch(1)=resCG
sch(1:25)=' itmCG,iaccCG,iLast,resCG'
call chwrit2(1,ich,3,rch,1,sch,25,iOK)
ich(1)=0
if(lMMPrough) ich(1)=1
ich(2)=0
if(lMMPfine) ich(2)=1
sch(1:13)=' lRough,lFine'
call chwrit2(1,ich,2,rch,0,sch,13,iOK)
rch(1)=resPET
if(lPET) then
ich(1)=nPET-1
sch(1:15)=' use PET,resPET'
call chwrit2(1,ich,1,rch,1,sch,15,iOK)
else
ich(1)=-1
sch(1:14)=' no PET,resPET'
call chwrit2(1,ich,1,rch,1,sch,14,iOK)
end if
ich(1)=Int4(iMMPCon)
sch(1:11)=' Connection'
call chwrit2(1,ich,1,rch,0,sch,11,iOK)
rch(1)=ErrorScale
sch(1:14)=' error scaling'
call chwrit2(1,ich,0,rch,1,sch,14,iOK)
ich(1)=iFunWeight
sch(1:25)=' Weight function argument'
call chwrit2(1,ich,1,rch,0,sch,25,iOK)
if(lMMPrMtr.and.Allocated(MMPMtr)) then
ich(1)=nRow
ich(2)=mCol
sch(1:19)=' rectangular matrix'
call chwrit2(1,ich,2,rch,0,sch,19,iOK)
ik=0
do i=1,nRow
ich(1)=i
call chwrit2(1,ich,1,rch,0,sch,0,iOK)
do k=1,mCol
ik=ik+1
rch(1)=Dble(MMPMtr(ik))
rch(2)=DImag(MMPMtr(ik))
call chwrit2(1,ich,0,rch,2,sch,0,iOK)
end do
end do
else
ich(1)=0
ich(2)=0
sch(1:22)=' no rectangular matrix'
call chwrit2(1,ich,2,rch,0,sch,22,iOK)
end if
if(lMMPtMtr.and.Allocated(TriMtr)) then
ich(1)=mCol
sch(1:18)=' trapezoidal matrix'
call chwrit2(1,ich,1,rch,0,sch,18,iOK)
do i=1,mTri
rch(1)=Dble(TriMtr(i))
rch(2)=DImag(TriMtr(i))
call chwrit2(1,ich,0,rch,2,sch,0,iOK)
end do
else
ich(1)=0
sch(1:21)=' no trapezoidal matrix'
call chwrit2(1,ich,1,rch,0,sch,21,iOK)
end if
sch(1:1)=' '
call chwrit2(1,ich,0,rch,0,sch,1,iOK)
EndFile(1)
close(1)
end Subroutine SaveMmp
Subroutine OpenMmp(lCheck)
! read MMP data from a file
Implicit none
Logical, intent(in) :: lCheck
Logical ldum,lFileExist
Integer(4) iOk,ios,i,k,ik,i1,k1,iVers,idum
Character(20) text
if(.not.lgcFld) lGet3DMat=.true.
iBound=0_2
if(.not.lCheck) then
call Open2read(-1,'Select MMP data file to be read!','MMP data file ',MmpFileName,'MMP',ios)
if(ios.gt.0) return
lMMPrMtr=.true.
lMMPtMtr=.true.
idum=MessageBoxQQ('Skip MMP matrix data?'C,'Open MMP data'C, &
MB$YesNo.or.MB$IconQuestion)
if(idum.eq.MB$IDYES) then
lMMPrMtr=.false.
lMMPtMtr=.false.
end if
end if
inquire(file=MmpFileName,Exist=lFileExist)
if(.not.lFileExist) return
open(1,file=MmpFileName,status='old',iostat=ios)
if(ios.ne.0) then
if(.not.lCheck) idum=MessageBoxQQ('Error opening file!\rCannot read data!'C,'Open MMP data'C, &
MB$OK.or.MB$IconExclamation)
close(1)
return
end if
call ReadStr(1,text,iOK)
if(CHMmpIdent(1:15).ne.text(1:15)) then
idum=MessageBoxQQ('Wrong file Type!\rContinue reading?'C,'Open MMP data'C, &
MB$YesNo.or.MB$IconQuestion)
if(idum.eq.MB$IDNO) then
close(1)
return
end if
end if
call StrToInt(text(16:16)//text(18:18),iVers,iOK)
call chread2(1,ich,1,rch,3,iOK)
if(iOK.ne.0) then
idum=MessageBoxQQ('Error reading input file!\r(Matching point data)'C,'Open MMP data'C, &
MB$OK.or.MB$ICONSTOP)
close(1)
return
end if
nSegPt=ich(1)
BndDMax=rch(1)
BndPpW=rch(2)
BndOver=rch(3)
if(iVers.gt.29) then
call chread2(1,ich,4,rch,1,iOK)
iAmplTyp=ich(2)
iAmpl=ich(4)
else if(iVers.gt.20) then
call chread2(1,ich,3,rch,1,iOK)
if(ich(2).gt.-1) then
iAmplTyp=1
iAmpl=ich(4)
else if(ich(2).eq.-1) then
iAmplTyp=0
iAmpl=0
else
iAmplTyp=2
iAmpl=0
end if
else
call chread2(1,ich,2,rch,0,iOK)
if(ich(2).gt.-1) then
iAmplTyp=1
iAmpl=ich(4)
else if(ich(2).eq.-1) then
iAmplTyp=0
iAmpl=0
else
iAmplTyp=2
iAmpl=0
end if
ich(3)=2
rch(1)=0.0d0
end if
if(iOK.ne.0) then
idum=MessageBoxQQ('Error reading input file!\r(Matrix solver type)'C,'Open MMP data'C, &
MB$OK.or.MB$ICONSTOP)
close(1)
return
end if
lMMP3F=.false.
if(ich(1).ge.1000000) then
ich(1)=ich(1)-1000000
lMMP3F=.true.
end if
if(ich(1).gt.500) then
if(ich(1).gt.9999) then
ich(1)=ich(1)-10000*Int4(ich(1)/10000)
iMMPfindExp=Int2(ich(1)/1000)
ich(1)=ich(1)-1000*Int4(iMMPfindExp)
iMMPfindExp=-iMMPfindExp
else
iMMPfindExp=Int2(ich(1)/1000)
ich(1)=ich(1)-1000*Int4(iMMPfindExp)
end if
else
iMMPfindExp=0
end if
if(ich(1).gt.50) then
lMMPuseRes=.false.
iMtrSolver=max(0,min(5,ich(1)-100))
else
lMMPuseRes=.true.
iMtrSolver=max(0,min(5,ich(1)))
end if
iScaleMatrix=ich(3)
fMatrix0=rch(1)
if((iMtrSolver.eq.0).or.(iMtrSolver.eq.4).or.(iMtrSolver.eq.5)) lMMPrMtr=.false.
ich(3)=0
if(iVers.gt.30) then
call chread2(1,ich,3,rch,1,iOK)
else if(iVers.gt.20) then
call chread2(1,ich,2,rch,1,iOK)