-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChainCalculatorMin.pas
3084 lines (2972 loc) · 86.8 KB
/
ChainCalculatorMin.pas
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
unit ChainCalculatorMin;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
uses
{$IFnDEF FPC}
windows,
{$ELSE}
LCLIntf, LCLType, // LMessages,
{$ENDIF}
Classes, SysUtils, comctrls, ChainClassesMin, LSODA4cc, EuLibMin, NuclideClassesMin;
const
MaxSteps = 5000;
MaxNEQ = 500;
MaxArrayLength = 35 + 9 * MaxNEQ + 3 * MaxNEQ * MaxNEQ;
SqrtPiMult293_6div2 = 1.518528566650924E+01; // sqrt(Pi*296.3)/2 Kalkulator32
// TimeEpsilon=1.0E-13; // For Increase Check (here - in seconds)
type
integertype = Longint;
realtype = double;
realarraytype = array[1..MaxArrayLength] of realtype;
integerarraytype = array[1..MaxArrayLength] of integertype;
TChainCalculator = class;
{TSSK_Table}
TSSK_Table = class
private
fThZpA_s: integer;
fConcentration: TFloatList; // Add Used
fSSK: TFloatList; // Add Used
protected
function GetCount: integer;
function Get_Concentration(Index: integer): double;
procedure Set_Concentration(Index: integer; aConcentration: double);
function Get_SSK(Index: integer): double;
procedure Set_SSK(Index: integer; aSSK: double);
public
function LoadFromStream(Stream: TStream): Boolean;
function SaveToStream(Stream: TStream): Boolean;
function IsOK: Boolean;
(*
function InterpolateSSK(const aConcentration: double): double; // interpolation
*)
procedure Add(const aConcentration, aSSK: double);
procedure Clear;
procedure Assign(Source: TSSK_Table);
destructor Destroy; override;
procedure Order; // InCrease
procedure Normalize;
constructor Create(aThZpA_s: integer);
property Concentration[Index: integer]: double read Get_Concentration write Set_Concentration;
property SSK[Index: integer]: double read Get_SSK write Set_SSK;
property PointsNumber: integer read GetCount;
property ThZpA_s: integer read fThZpA_s;
end;
{TSSK_TableList}
TSSK_TableList = class(TList)
private
//
protected
procedure FreeItems;
public
function LoadFromStream(Stream: TStream): Boolean;
function LoadFromFile(const FileName: string): Boolean;
function SaveToStream(Stream: TStream): Boolean;
function SaveToFile(const FileName: string): Boolean;
(*
function InterpolateSSKforStateName(const aConcentration: double; const aName: string): double; // interpolation
function InterpolateSSKforStateNo(const aConcentration: double; const StateNo: integer): double; // interpolation
*)
function NotNullCount: integer;
procedure Assign(Source: TSSK_TableList);
procedure Add(const aTable: TSSK_Table);
destructor Destroy; override;
constructor Create;
end;
{TTimePoint}
TTimePoint = record
Time: double; // In Seconds to be compatible with lambda and fluxes
ThermalFlux: double;
ResonanceFlux: double;
FastFlux: double;
Tng: double;
end;
PTimePoint = ^TTimePoint;
{TTimePointList}
TTimePointList = class(TList)
private
fChainCalculator: TChainCalculator;
protected
function GetTimePoint(Index: integer): TTimePoint;
procedure SetTimePoint(Index: integer; aTimePoint: TTimePoint);
public
procedure Add(aTimePoint: TTimePoint);
destructor Destroy; override;
procedure Order(const InCrease: Boolean = True);
constructor Create(aChainCalculator: TChainCalculator);
property
TimePoints[Index: integer]: TTimePoint read GetTimePoint write SetTimePoint; default;
end;
{TChainCalculator}
TChainCalculator = class
private
fChain: TChain;
fNvsTimeST: array of array of double;
fLambdaIJ: array of array of double;
fThermalIJ: array of array of double;
fResonanceIJ0: array of array of double;
fG_factorIJ: array of array of double;
fResonanceIJ: array of array of double;
fFastIJ: array of array of double;
fTmpArray: array of double;
fTimePoints: TTimePointList;
frtolerance: array of double;
fatolerance: array of double;
fCalculatorStdOut: TStringList;
fTimeScaler: double;
fN0Scaler: double;
fNeedTolSwitch: Boolean;
fCalculating: Boolean;
fUseSSK: Boolean;
fStopCalculation: Boolean;
fDepressionConsider: Boolean;
fDepressionVolume: double;
fDepressionL: double;
// Activity Tables
// TDecayType=(dtNone, dtA, dtBM, dtEC, dtIT, dtN, dtP, dtSF, dtQ);
fNone_Table, fA_Table, fBM_Table, fEC_Table, fIT_Table, fN_Table, fP_Table, fSF_Table, fQ_Table: array of double;
fDecayTablesReady: Boolean;
fInternalSSK: TSSK_TableList;
fExternalSSK: TSSK_TableList;
fRA_VolumeForSSK: double;
fStatesCount: Integer;
fTimesCount: Integer; // Be carefull Use AdjustTimeCount
fLSODA: TLSODA;
fDebugStr: string;
function TngMultiplier(const Tng: double; const g_factor: double; const TngEpsilon: double = 1.0E-10): double;
protected
function GetNumberOfStates: integer;
procedure DoChainCalc4LSODA(t: double; y_v, dydt_v: TVector);
procedure ChainCalcDyDt4DLL(const NEQ: integertype; var T, Y_, YDOT_,
RPAR_: realtype; var IPAR_: integertype);
procedure ChainCalcDfDy4DLL(const NEQ: integertype; var T: realtype; var Y_: realtype;
var ML: integertype; var MU: integertype; var PD_: realtype; var NRPD: integertype;
var RPAR: realtype; var IPAR: integertype);
function GetTimesCount: integer;
function GetLambdaIJ(I, J: Integer): double;
procedure SetLambdaIJ(I, J: Integer; Value: double);
function GetThermalIJ(I, J: Integer): double;
procedure SetThermalIJ(I, J: Integer; Value: double);
function GetG_factor(I, J: Integer): double;
procedure SetG_factor(I, J: Integer; Value: double);
function GetResonanceIJ(I, J: Integer): double;
procedure SetResonanceIJ(I, J: Integer; Value: double);
function GetFastIJ(I, J: Integer): double;
procedure SetFastIJ(I, J: Integer; Value: double);
function GetNIvsTime(StateNo, TimeNo: Integer): double;
function IsGetNvsTime(StateNo: Integer; aFloatList: TFloatList): Boolean;
function IsGetNsForTimeNo(TimeNo: Integer; var aFloatList: TFloatList): Boolean;
function GetN0(StateNo: Integer): double;
procedure SetN0(StateNo: Integer; Value: double);
function GetAtolerance(Index: integer): double;
procedure SetAtolerance(Index: integer; Tol: double);
function GetRtolerance(Index: integer): double;
procedure SetRtolerance(Index: integer; Tol: double);
procedure PrepareSSK; // N - Ln
procedure PrepareIJ; // After UseSSK - fResonanceIJ are modified
procedure PrepareIJ0;
procedure PrepareTimes;
procedure ScaleTimes;
procedure UnScaleTimes;
procedure ScaleN0;
procedure UnScaleN0;
function GetStateName(Index: Integer): string;
function GetStateThZpA_s(Index: Integer): integer;
function CalcSSK(const StateNo: integer; const Nuclei: double): double; // ==ApproximateSSK
procedure AdjustTimeCount(Value: integer);
public
ProgressBar: TProgressBar; // external
function GetActivitiNIvsTime(StateNo, TimeNo: Integer; dt: TDecayType): double;
procedure GetElements(var ElementZs: TLongIntList);
function ApproximateSSK(const StateNo: integer; const N: double): double;
procedure AssignSSK_Tables(const aTables: TSSK_TableList);
function GetDepresssionK(const TimeNo: integer): double;
function GetElementMass(const ElementName: string; const TimeNo: integer): double;
function GetStateMass(const StateNo, TimeNo: Integer): double;
function GetTotalMass(const TimeNo: Integer): double;
procedure PrepareActivityTables;
procedure StopCalculation;
function SolveChainWithVODE(UseJAC: Boolean = True; UseDLL: Boolean = False): integer;
function SolveChainWithLSODA(UseJAC: Boolean = True; UseDLL: Boolean = False): integer;
function SolveChainWithRADAU(UseJAC: Boolean = True): integer;
function SolveChainWithMEBDF(UseJAC: Boolean = True): integer;
function CheckTimePointsIncrease: Boolean;
function GetFissionEnergyDepositionNIvsTime(const StateNo, TimeNo: integer;
const ConsiderDepression, SSKconsider: Boolean; const Energy4fission: double = 200): double;
procedure ClearTimePoints;
constructor Create(aChain: TChain);
destructor Destroy; override;
//
property RA_VolumeForSSK: double read fRA_VolumeForSSK write fRA_VolumeForSSK;
property UseSSK: Boolean read fUseSSK write fUseSSK;
property Calculating: Boolean read fCalculating;
property NoOfStates: integer read GetNumberOfStates;
property CalculatorStdOut: TStringList read fCalculatorStdOut;
property atolerance[Index: Integer]: double read GetAtolerance write SetAtolerance;
property rtolerance[Index: Integer]: double read GetRtolerance write SetRtolerance;
property TimePoints: TTimePointList read fTimePoints;
property TimesCount: integer read GetTimesCount; // Number Of Time Points Set via AdjustTimeCount
property NIvsTime[StateNo, TimeNo: Integer]: double read GetNIvsTime; //N(t) N,t-vector
property N0[Index: Integer]: double read GetN0 write SetN0;
property StateName[Index: Integer]: string read GetStateName;
property StateThZpA_s[Index: Integer]: integer read GetStateThZpA_s;
property DepressionConsider: Boolean read fDepressionConsider write fDepressionConsider;
property DepressionVolume: double read fDepressionVolume write fDepressionVolume;
property DepressionL: double read fDepressionL write fDepressionL;
// Next 5 properties are for DEBUG
(*
property LambdaIJ[I, J: Integer]: double read GetLambdaIJ write SetLambdaIJ;
property ThermalIJ[I, J: Integer]: double read GetThermalIJ write SetThermalIJ;
property G_factorIJ[I, J: Integer]: double read GetG_factor write SetG_factor;
property ResonanceIJ[I, J: Integer]: double read GetResonanceIJ write SetResonanceIJ;
property FastIJ[I, J: Integer]: double read GetFastIJ write SetFastIJ;
*)
property NeedTolSwitch: Boolean read fNeedTolSwitch write fNeedTolSwitch;
end;
function Depression(const N0, SigmaA, l: double): double;
implementation
uses
Forms, Dialogs, Parsing, DVODEu4cc;
const
BeginSSK_TableChar = #1;
EndSSK_TableChar = #2;
BeginSSK_TableListChar = #3;
EndSSK_TableListChar = #4;
EndOfLine = #13#10;
BufferSize = 255;
RTOL_mul_ABS_Y_plus_ATOL = 1.0E-50;
var // for DLLs usage
Y: realarraytype;
Ti: realtype;
DummyReal: realtype;
DummyInt: integertype;
TOUT: realtype;
ITOL: integertype;
RTOL: realarraytype;
ATOL: realarraytype;
ITASK: integertype;
ISTATE: integertype;
IOPT: integertype;
RWORK: array[1..MaxArrayLength] of realtype;
LRW: integertype;
IWORK: array[1..MaxArrayLength] of integertype;
LIW: integertype;
MF: integertype;
RPAR: realarraytype;
IPAR: integerarraytype;
// for meb
IDID, LOUT, LWORK, LIWORK, MAXDER, IERR: integertype;
H0, TEND: realtype;
MBND: array[1..4] of integertype;
MASBND: array[1..4] of integertype;
// for RADAU LWORK, IDID, LIWORK - above
X, H: realtype;
IJAC, MLJAC, MUJAC, IMAS, MLMAS, MUMAS, IOUT_RADAUS, NSMAX: integertype;
//
aThermalFlux, aResonanceFlux, aFastFlux, aTng: Double; // global vars now
ActiveCalculator: TChainCalculator; // sets at initit of integration step
// for pipes
Buffer: array[0..BufferSize] of Char;
StrRead: string;
BytesRead: Cardinal;
function Depression(const N0, SigmaA, l: double): double;
var
l_mul_MacroSigma: double;
begin
try
l_mul_MacroSigma:= (N0 * SigmaA) * l;
Result:= (1 - exp(-l_mul_MacroSigma)) / l_mul_MacroSigma;
except
Result:= 1;
end;
end;
procedure dydt4dll(var NEQ: integertype; var T, Y_, YDOT_,
RPAR_: realtype; var IPAR_: integertype); stdcall;
begin
if ActiveCalculator <> nil then
ActiveCalculator.ChainCalcDyDt4DLL(NEQ, T, Y_, YDOT_, RPAR_, IPAR_)
else
MessageDlg('ActiveCalculator=nil', mtInformation, [mbOK], 0);
end;
procedure dydt4LSODA_dll(var NEQ: integertype; var T, Y_, YDOT_: realtype); stdcall;
begin
if ActiveCalculator <> nil then
ActiveCalculator.ChainCalcDyDt4DLL(NEQ, T, Y_, YDOT_, DummyReal, DummyInt)
else
MessageDlg('ActiveCalculator=nil', mtInformation, [mbOK], 0);
end;
procedure JAC4dll(var NEQ: integertype; var T: realtype; var Y_: realtype;
var ML: integertype; var MU: integertype; var PD_: realtype; var NROWPD: integertype;
var RPAR: realtype; var IPAR: integertype); stdcall;
begin
if ActiveCalculator <> nil then
ActiveCalculator.ChainCalcDfDy4DLL(NEQ, T, Y_, ML, MU, PD_, NROWPD, RPAR, IPAR)
else
MessageDlg('ActiveCalculator=nil', mtInformation, [mbOK], 0);
end;
procedure dfdyMEB(var T, Y_, PD_: realtype; var NEQ: integertype; const MEBAND: Pointer;
var IPAR: integertype; var RPAR: realtype); stdcall;
var
ML, MU, NROWPD: integertype;
begin
ML:= 0;
MU:= 0;
NROWPD:= 0;
JAC4dll(NEQ, T, Y_, ML, MU, PD_, NROWPD, RPAR, IPAR);
end;
// SUBROUTINE JAC(N,X,Y,DFY,LDFY,RPAR,IPAR)
procedure dfdyRADAU(var N_: integertype; var T_, Y_, DFY_, LDFY_,
RPAR_: realtype; var IPAR_: integertype); stdcall;
var
ML_, MU_, NROWPD_: integertype;
begin
ML_:= 0;
MU_:= 0;
NROWPD_:= 0;
JAC4dll(N_, T_, Y_, ML_, MU_, DFY_, NROWPD_, RPAR_, IPAR_);
end;
{TChainCalculator}
constructor TChainCalculator.Create(aChain: TChain);
const
rToleranceEpsilon = 1.0E-5; //1.E-7;
aToleranceEpsilon = 1.0E-5; //1.E-10;
var
I, J: integer;
begin
ProgressBar:= nil;
fTimeScaler:= -1;
fN0Scaler:= -1;
fDecayTablesReady:= False;
if aChain is TChain then
begin
inherited Create;
fStatesCount:= aChain.States.Count;
fCalculatorStdOut:= TStringList.Create;
fTimePoints:= TTimePointList.Create(Self);
fChain:= aChain;
SetLength(fNvsTimeST, fStatesCount, 1);
SetLength(fLambdaIJ, fStatesCount, fStatesCount);
SetLength(fThermalIJ, fStatesCount, fStatesCount);
SetLength(fResonanceIJ, fStatesCount, fStatesCount);
SetLength(fFastIJ, fStatesCount, fStatesCount);
SetLength(fG_factorIJ, fStatesCount, fStatesCount);
SetLength(fatolerance, fStatesCount);
SetLength(frtolerance, fStatesCount);
for I:= 0 to fStatesCount - 1 do
begin
fNvsTimeST[I, 0]:= 0.0;
fatolerance[I]:= aToleranceEpsilon;
frtolerance[I]:= rToleranceEpsilon;
for J:= 0 to fStatesCount - 1 do
begin
fLambdaIJ[J, I]:= 0.0;
fThermalIJ[J, I]:= 0.0;
fResonanceIJ[J, I]:= 0.0;
fFastIJ[J, I]:= 0.0;
fg_factorIJ[J, I]:= 1.0;
end;
end;
end;
fInternalSSK:= TSSK_TableList.Create;
fExternalSSK:= TSSK_TableList.Create;
PrepareIJ;
fNeedTolSwitch:= False;
end;
destructor TChainCalculator.Destroy;
begin
fInternalSSK.Free;
fExternalSSK.Free;
fCalculatorStdOut.Free;
fTimePoints.Free;
SetLength(fLambdaIJ, 0, 0);
SetLength(fThermalIJ, 0, 0);
SetLength(fResonanceIJ, 0, 0);
SetLength(fFastIJ, 0, 0);
SetLength(fG_factorIJ, 0, 0);
SetLength(fResonanceIJ0, 0, 0);
SetLength(fNvsTimeST, 0, 0);
SetLength(fatolerance, 0);
SetLength(frtolerance, 0);
// ActivityTable
SetLength(fNone_Table, 0);
SetLength(fA_Table, 0);
SetLength(fBM_Table, 0);
SetLength(fEC_Table, 0);
SetLength(fIT_Table, 0);
SetLength(fN_Table, 0);
SetLength(fP_Table, 0);
SetLength(fSF_Table, 0);
SetLength(fQ_Table, 0);
ActiveCalculator:= nil;
inherited;
end;
function TChainCalculator.GetNumberOfStates: integer;
begin
try
Result:= fStatesCount;
except
Result:= -1;
end;
end;
function TChainCalculator.GetLambdaIJ(I, J: Integer): double;
begin
Result:= fLambdaIJ[I, J];
end;
function TChainCalculator.GetThermalIJ(I, J: Integer): double;
begin
Result:= fThermalIJ[I, J];
end;
function TChainCalculator.GetG_factor(I, J: Integer): double;
begin
Result:= fG_factorIJ[I, J];
end;
function TChainCalculator.GetResonanceIJ(I, J: Integer): double;
begin
Result:= fResonanceIJ[I, J];
end;
function TChainCalculator.GetFastIJ(I, J: Integer): double;
begin
Result:= fFastIJ[I, J];
end;
function TChainCalculator.GetNIvsTime(StateNo, TimeNo: Integer): double;
begin
Result:= fNvsTimeST[StateNo, TimeNo];
end;
function TChainCalculator.IsGetNsForTimeNo(TimeNo: Integer; var aFloatList: TFloatList): Boolean;
var
S: integer;
begin
Result:= False;
if aFloatList = nil then
Exit;
aFloatList.Clear;
try
for S:= 0 to fStatesCount - 1 do
aFloatList.Add(fNvsTimeST[S, TimeNo]);
Result:= True;
except
Result:= False;
end;
end;
function TChainCalculator.IsGetNvsTime(StateNo: Integer; aFloatList: TFloatList): Boolean;
var
TimeNoI: integer;
begin
Result:= False;
if aFloatList = nil then
Exit;
aFloatList.Clear;
try
for TimeNoI:= 0 to fTimesCount - 1 do
aFloatList.Add(fNvsTimeST[StateNo, TimeNoI]);
Result:= True;
except
Result:= False;
end;
end;
procedure TChainCalculator.SetLambdaIJ(I, J: Integer; Value: double);
begin
fLambdaIJ[I, J]:= Value;
end;
procedure TChainCalculator.SetThermalIJ(I, J: Integer; Value: double);
begin
fThermalIJ[I, J]:= Value;
end;
procedure TChainCalculator.SetG_factor(I, J: Integer; Value: double);
begin
fG_factorIJ[I, J]:= Value;
end;
procedure TChainCalculator.SetResonanceIJ(I, J: Integer; Value: double);
begin
fResonanceIJ[I, J]:= Value;
end;
procedure TChainCalculator.SetFastIJ(I, J: Integer; Value: double);
begin
fFastIJ[I, J]:= Value;
end;
procedure TChainCalculator.PrepareIJ0;
var
I, J: integer;
begin
if fStatesCount <> fChain.States.Count then
MessageDlg('ERROR !!!' + #13 + #10 +
'In PrepareIJ0' + #13 + #10 +
'fStatesCount<> fChain.States.Count', mtError, [mbOK], 0);
SetLength(fResonanceIJ0, fStatesCount, fStatesCount);
for I:= 0 to fStatesCount - 1 do
for J:= 0 to fStatesCount - 1 do
fResonanceIJ0[I, J]:= fResonanceIJ[I, J];
end;
procedure TChainCalculator.PrepareIJ;
var
I, StartNo, FinishNo: integer;
aState: TChainState;
aLink: TChainLink;
aStr: ShortString;
begin
for I:= 0 to fStatesCount - 1 do
begin
aState:= fChain.States[I];
with aState do
begin
fLambdaIJ[I, I]:= aState.DecayDecrease;
fThermalIJ[I, I]:= aState.ThermalDecrease;
fResonanceIJ[I, I]:= aState.ResonanceDecrease;
fFastIJ[I, I]:= aState.FastDecrease;
fG_factorIJ[I, I]:= aState.G_Factor;
end;
end;
// now fG_factorIJ
for I:= 0 to fChain.Links.Count - 1 do
begin
aLink:= fChain.Links[I];
with aLink do
begin
StartNo:= aLink.FindStartStateChainNo;
FinishNo:= aLink.FindFinishStateChainNo;
if ValuesStr.Count > 0 then
if PrepareToParse(ValuesStr[0], aStr) then
fLambdaIJ[StartNo, FinishNo]:= GetFormulaValue(aStr);
if ValuesStr.Count > 1 then
if PrepareToParse(ValuesStr[1], aStr) then
fThermalIJ[StartNo, FinishNo]:= GetFormulaValue(aStr);
if ValuesStr.Count > 2 then
if PrepareToParse(ValuesStr[2], aStr) then
fResonanceIJ[StartNo, FinishNo]:= GetFormulaValue(aStr);
if ValuesStr.Count > 3 then
if PrepareToParse(ValuesStr[3], aStr) then
fFastIJ[StartNo, FinishNo]:= GetFormulaValue(aStr);
if ValuesStr.Count > 4 then
if PrepareToParse(ValuesStr[4], aStr) then
fG_factorIJ[StartNo, FinishNo]:= GetFormulaValue(aStr);
end;
end;
end;
procedure TChainCalculator.ScaleN0;
var
I: integer;
N0max, N0min: double;
begin
N0max:= 0;
N0Min:= 1E100;
for I:= 0 to fStatesCount - 1 do
begin
if (fNvsTimeST[I, 0] > N0max) then
N0max:= fNvsTimeST[I, 0];
if (fNvsTimeST[I, 0] < N0min) then
N0min:= fNvsTimeST[I, 0];
end;
if (N0max + N0min > 0) then
fN0Scaler:= (N0max + N0min) * 2;
if fN0Scaler <= 0 then
Exit;
for I:= 0 to fStatesCount - 1 do
fNvsTimeST[I, 0]:= fNvsTimeST[I, 0] / fN0Scaler;
end;
procedure TChainCalculator.UnScaleN0;
var
S, T: integer;
begin
if fN0Scaler <= 0 then
Exit;
for T:= 0 to fTimesCount - 1 do
for S:= 0 to fStatesCount - 1 do
fNvsTimeST[S, T]:= fNvsTimeST[S, T] * fN0Scaler;
fN0Scaler:= -1;
end;
procedure TChainCalculator.ScaleTimes;
var
I, J: integer;
aTimePoint: TTimePoint;
begin
fTimeScaler:= (fTimePoints[0].Time + fTimePoints[fTimePoints.Count - 1].Time) / 2.0;
if fTimeScaler <= 0 then
Exit;
for I:= 0 to fTimePoints.Count - 1 do
begin
aTimePoint:= fTimePoints[I];
aTimePoint.Time:= aTimePoint.Time / fTimeScaler;
fTimePoints[I]:= aTimePoint;
end;
for I:= 0 to fStatesCount - 1 do
begin
for J:= 0 to fStatesCount - 1 do
begin
fThermalIJ[I, J]:= fThermalIJ[I, J] * fTimeScaler;
fResonanceIJ[I, J]:= fResonanceIJ[I, J] * fTimeScaler;
fFastIJ[I, J]:= fFastIJ[I, J] * fTimeScaler;
fLambdaIJ[I, J]:= fLambdaIJ[I, J] * fTimeScaler;
end;
end;
end;
procedure TChainCalculator.UnScaleTimes;
var
I, J: integer;
aTimePoint: TTimePoint;
begin
if fTimeScaler <= 0 then
Exit;
for I:= 0 to fTimePoints.Count - 1 do
begin
aTimePoint:= fTimePoints[I];
aTimePoint.Time:= aTimePoint.Time * fTimeScaler;
fTimePoints[I]:= aTimePoint;
end;
for I:= 0 to fStatesCount - 1 do
begin
for J:= 0 to fStatesCount - 1 do
begin
fThermalIJ[I, J]:= fThermalIJ[I, J] / fTimeScaler;
fResonanceIJ[I, J]:= fResonanceIJ[I, J] / fTimeScaler;
fFastIJ[I, J]:= fFastIJ[I, J] / fTimeScaler;
fLambdaIJ[I, J]:= fLambdaIJ[I, J] / fTimeScaler;
end;
end;
fTimeScaler:= -1;
end;
procedure TChainCalculator.PrepareTimes;
begin
AdjustTimeCount(fTimePoints.Count);
fTimePoints.Order;
end;
function TChainCalculator.TngMultiplier(const Tng: double; const g_factor: double; const TngEpsilon: double = 1.0E-10): double;
begin
if Tng < TngEpsilon then
Result:= 1
else
Result:= SqrtPiMult293_6div2 / sqrt(Tng);
if g_factor > 0 then
Result:= Result * g_factor;
end;
procedure TChainCalculator.DoChainCalc4LSODA(t: double; y_v, dydt_v: TVector);
var
NoOfY: integer;
I, J: integer;
SumIncreaseI: Double;
begin
NoOfY:= fStatesCount;
for I:= 1 to NoOfY do
begin
if fStopCalculation then
Exit;
SumIncreaseI:= 0;
for J:= 1 to NoOfY do
if (J <> I) then
SumIncreaseI:= SumIncreaseI + (fLambdaIJ[J - 1, I - 1] + fThermalIJ[J - 1, I - 1] * aThermalFlux * TngMultiplier(aTng, fG_factorIJ[J - 1, I - 1])
+ fResonanceIJ[J - 1, I - 1] * aResonanceFlux + fFastIJ[J - 1, I - 1] * aFastFlux) * y_v[J];
dydt_v[I]:= SumIncreaseI - (fLambdaIJ[I - 1, I - 1] + fThermalIJ[I - 1, I - 1] * aThermalFlux * TngMultiplier(aTng, fG_factorIJ[I - 1, I - 1])
+ fResonanceIJ[I - 1, I - 1] * aResonanceFlux + fFastIJ[I - 1, I - 1] * aFastFlux) * y_v[I];
end;
end;
function TChainCalculator.SolveChainWithLSODA
(UseJAC: Boolean = True; UseDLL: Boolean = False): integer;
const
MaxRecallCount = 5; //Max lsoda recall (high acuracy requested)
LSODA_TimeEpsilon = 1.0E-5;
var
NoOfY, NoOfX: integer;
t: double;
t_double, tout_double: double;
y_Vector: TVector;
I, J, iout: integer;
TmpLines: TStringList;
MacroSigma, l_mul_MacroSigma: Double;
aNEQ, aJT: integertype;
HDLL: Longword;
LSODAD_: procedure(
const F, NEQ, Y, T, TOUT, ITOL, RTOL, ATOL, ITASK, ISTATE,
IOPT, RWORK, LRW, IWORK, LIW, JAC, JT: Pointer); stdcall;
aF: Pointer;
aJAC: Pointer;
// For UsePipe
DllVer_: procedure(const Ver: Pointer); stdcall;
DllVer: integer;
DllFileNameToLoad: ansistring;
begin
if ProgressBar <> nil then
with ProgressBar do
begin
Min:= 0;
Max:= fTimePoints.Count - 1;
Position:= 0;
end;
fDebugStr:= '';
fStopCalculation:= False;
fCalculating:= True;
fCalculatorStdOut.Clear;
if UseDLL then
begin
ActiveCalculator:= Self;
try
NoOfY:= fStatesCount;
NoOfX:= fTimePoints.Count;
PrepareIJ;
PrepareTimes;
ScaleTimes;
ScaleN0;
if fUseSSK then
PrepareSSK;
for I:= 1 to NoOfY do
begin
rtol[I]:= frtolerance[I - 1];
atol[I]:= fatolerance[I - 1];
end;
aNEQ:= NoOfY;
ITOL:= 4;
ITASK:= 4;
ISTATE:= 1;
IOPT:= 0;
LRW:= MaxArrayLength;
LIW:= MaxArrayLength;
if UseJAC then
aJT:= 1 // jacobian JAC
else
aJT:= 2; // No jacobian
IWORK[5]:= 1;
aF:= @dydt4LSODA_dll;
aJAC:= @JAC4dll;
fCalculating:= True;
// HDLL:= LoadLibrary('lsoda');
{$IFDEF WINDOWS}
{$IFDEF CPU32}
DllFileNameToLoad:= 'lsoda';
{$ELSE}
DllFileNameToLoad:= 'lsoda64';
{$ENDIF}
{$ENDIF}
{$IFDEF LINUX}
{$IFDEF CPU32}
DllFileNameToLoad:= ExtractFilePath(Application.ExeName) + 'lsoda.so';
{$ELSE}
DllFileNameToLoad:= ExtractFilePath(Application.ExeName) + 'lsoda64.so';
{$ENDIF}
{$ENDIF}
HDLL:= LoadLibrary(DllFileNameToLoad);
if HDLL >= 32 then { успешно }
begin
Result:= -1;
try
LSODAD_:= GetProcAddress(HDLL, 'lsodad_');
for I:= 1 to NoOfY do
Y[I]:= N0[I - 1];
// Ti:= fTimePoints[0].Time;
for iout:= 1 to NoOfX - 1 do
begin
Ti:= fTimePoints[iout - 1].Time;
ITASK:= 4;
TOUT:= fTimePoints[iout].Time;
// Fluxes now are global vars
aThermalFlux:= fTimePoints[iout - 1].ThermalFlux;
aResonanceFlux:= fTimePoints[iout - 1].ResonanceFlux;
aFastFlux:= fTimePoints[iout - 1].FastFlux;
aTng:= fTimePoints[iout - 1].Tng;
if fDepressionConsider then
try
if aThermalFlux > 0 then
if fTimeScaler > 0 then
if fN0Scaler > 0 then
begin
MacroSigma:= 0;
for I:= 1 to NoOfY do
MacroSigma:= MacroSigma + fThermalIJ[I - 1, I - 1] / fTimeScaler * TngMultiplier(aTng, fG_factorIJ[I - 1, I - 1]) * Y[I] * fN0Scaler / fDepressionVolume;
l_mul_MacroSigma:= fDepressionL * MacroSigma;
aThermalFlux:= aThermalFlux * (1 - exp(-l_mul_MacroSigma)) / l_mul_MacroSigma;
end;
except
aThermalFlux:= fTimePoints[iout - 1].ThermalFlux;
end;
// Fluxes now are global vars
RWORK[1]:= TOUT;
Application.ProcessMessages;
if fStopCalculation then
begin
Result:= -1;
Exit;
end;
// SSK ResonanceIJ Correction
if fUseSSK then
if aResonanceFlux > 0 then
if fN0Scaler > 0 then
begin
for I:= 0 to NoOfY - 1 do
for J:= 0 to NoOfY - 1 do
if fResonanceIJ0[J, I] > 0 then
fResonanceIJ[J, I]:= fResonanceIJ0[J, I] * CalcSSK(J, Y[J + 1] * fN0Scaler)
else
fResonanceIJ[J, I]:= 0;
end;
// SSK ResonanceIJ Correction
LSODAD_(aF, @aNEQ, @(Y[1]), @Ti, @TOUT, @ITOL, @(RTOL[1]), @(ATOL[1]), @ITASK, @ISTATE,
@IOPT, @(RWORK[1]), @LRW, @(IWORK[1]), @LIW, aJAC, @aJT);
if ISTATE < 0 then //((ISTATE=-1)or(ISTATE=-2)) then begin
if ISTATE >= -2 then
begin // -2 means excess accuracy requested (tolerances too small).
for i:= 1 to MaxRecallCount do
begin
ISTATE:= 1; // 2, 3 - do not work
TOUT:= fTimePoints[iout].Time;
IWORK[6]:= 1000;
fCalculatorStdOut.Add('LSODA made additional steps');
fCalculatorStdOut.Add(' for TOUT#' + IntToStr(iout) + ' ,check tolenaces');
for J:= 1 to I do
IWORK[6]:= IWORK[6] * 2; //IWORK[6]*2^I;
LSODAD_(aF, @aNEQ, @(Y[1]), @Ti, @TOUT, @ITOL, @(RTOL[1]), @(ATOL[1]), @ITASK, @ISTATE,
@IOPT, @(RWORK[1]), @LRW, @(IWORK[1]), @LIW, aJAC, @aJT);
for j:= 1 to NoOfY do
if Y[j] < 0 then
Y[j]:= 0.0;
if (ISTATE = -2) then
begin
for j:= 1 to NoOfY do
if ((RTOL[j] * Y[j] + ATOL[j]) > RTOL_mul_ABS_Y_plus_ATOL) then
ATOL[j]:= 0
else
ATOL[j]:= Self.fatolerance[j - 1];
ISTATE:= 1; // Tolerance changed
continue;
end;
if (ISTATE > 0) then
break;
end;
if ((TOUT > fTimePoints[iout].Time * (1 + LSODA_TimeEpsilon)) or (TOUT < fTimePoints[iout].Time * (1 - LSODA_TimeEpsilon))) then
fCalculatorStdOut.Add('TimePoints[iout].Time<>TOUT ' + 'iout=' + IntToStr(iout) +
'TimePoints[iout].Time=' + FloatTostr(fTimePoints[iout].Time) +
' TOUT=' + FloatToStr(TOUT));
end
else
begin //( ISTATE=<-3)- Unrecoverable error - Get Messages from DLL
Result:= -1;
TmpLines:= TStringList.Create;
try
if FileExists('lsoda.log') then
begin
TmpLines.LoadFromFile('lsoda.log');
for I:= 0 to TmpLines.Count - 1 do
if ((Trim(TmpLines[I]) <> '') and (Pos('STARTED', UpperCase(TmpLines[I])) = 0) and (Pos('FINISHED', UpperCase(TmpLines[I])) = 0)) then
fCalculatorStdOut.Add(TmpLines[I]);
end;
finally
TmpLines.Free;
end;
Exit;
end;
for j:= 1 to NoOfY do
if Y[j] < 0 then
Y[j]:= 0.0;
for j:= 1 to NoOfY do
fNvsTimeST[j - 1, iout]:= Y[j];
// TolSwitch
if fNeedTolSwitch then
begin
for j:= 1 to NoOfY do
if Y[j] > Self.fatolerance[j - 1] then
atol[j]:= 0.0
else
atol[j]:= Self.fatolerance[j - 1];
end;
// TolSwitch
if ProgressBar <> nil then
with ProgressBar do
Position:= iout;
Application.ProcessMessages;
end;
finally
ActiveCalculator:= nil;
fCalculating:= False;
UnScaleN0;
UnScaleTimes;
FreeLibrary(HDLL);
end; // finally
end
else
begin
MessageDlg('LSODA.DLL"' + DllFileNameToLoad +'" not found', mtError, [mbOk], 0);
Result:= -1;
end;
except
fCalculatorStdOut.Add('WARNING!!! LSODA (DLL) ABORTED on Exception');
fCalculatorStdOut.Add('');
Result:= -1;
end;
end
else
begin // Not DLL fLSODA:= TLSODA.Create(NoOfY);
fCalculatorStdOut.Clear;
NoOfY:= fStatesCount;
NoOfX:= fTimePoints.Count;
y_Vector:= TVector.Create(NoOfY);
fLSODA:= TLSODA.Create(NoOfY);
fLSODA.prfl:= 0; // print flag 1-print
try
PrepareIJ;
PrepareTimes;
ScaleTimes;
ScaleN0;
if fUseSSK then
PrepareSSK;
try
with fLSODA do
begin
for I:= 1 to NoOfY do
y_Vector[I]:= N0[I - 1];
Lsoda4cc.StdOut.Clear;
for I:= 1 to NoOfY do
begin
fLSODA.rtol[I]:= frtolerance[I - 1];
fLSODA.atol[I]:= fatolerance[I - 1];
end;
t:= Self.fTimePoints[0].Time;
fLSODA.itol:= 4;
fLSODA.istate:= 1;
fLSODA.iopt:= 0;
fLSODA.jt:= 2;
fLSODA.Setfcn(DoChainCalc4LSODA);
fLSODA.istart:= 0;
fLSODA.itask:= 1; //qqqq 4-don't work;1-works
t_double:= t;
for iout:= 1 to NoOfX - 1 do
begin // iout - cycle Number
// Fluxes now are global vars
aThermalFlux:= fTimePoints[iout - 1].ThermalFlux;
aResonanceFlux:= fTimePoints[iout - 1].ResonanceFlux;
aFastFlux:= fTimePoints[iout - 1].FastFlux;
aTng:= fTimePoints[iout - 1].Tng;
if fDepressionConsider then
try
if aThermalFlux > 0 then
if fTimeScaler > 0 then
if fN0Scaler > 0 then
begin
MacroSigma:= 0;
for I:= 1 to NoOfY do
MacroSigma:= MacroSigma + fThermalIJ[I - 1, I - 1] / fTimeScaler * TngMultiplier(aTng, fG_factorIJ[I - 1, I - 1]) * y_Vector[I] * fN0Scaler / fDepressionVolume;
l_mul_MacroSigma:= fDepressionL * MacroSigma;
aThermalFlux:= aThermalFlux * (1 - exp(-l_mul_MacroSigma)) / l_mul_MacroSigma;
end;
except
aThermalFlux:= fTimePoints[iout - 1].ThermalFlux;
end;
// Fluxes now are global vars
// SSK ResonanceIJ Correction
if fUseSSK then
if aResonanceFlux > 0 then
if fN0Scaler > 0 then
begin
for I:= 0 to NoOfY - 1 do
for J:= 0 to NoOfY - 1 do