-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathConfig.pl
executable file
·892 lines (741 loc) · 26.5 KB
/
Config.pl
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
#!/usr/bin/perl
# Copyright (C) 2002 Regents of the University of Michigan,
# portions used with permission
# For more information, see http://csem.engin.umich.edu/tools/swmf
# Allow in-place editing
$^I = "";
# Add local directory to search
push @INC, ".";
use strict;
our $Component = "GM";
our $Code = "BATSRUS";
our $MakefileDefOrig = 'src/Makefile.def';
our @Arguments = @ARGV;
# Figure out remote git server
my $remote = `git config remote.origin.url`; $remote =~ s/\/BATSRUS(.git)?\n//;
my $umichgitlab = ($remote =~ /gitlab\.umich\.edu/);
#print "remote=$remote umichgitlab=$umichgitlab\n";
my $config = "share/Scripts/Config.pl";
my $gitclone;
if($umichgitlab){
$gitclone = "share/Scripts/gitclone -s";
}else{
$gitclone = "share/Scripts/githubclone";
}
# Git clone missing directories as needed. Start with share/ to get $gitclone.
if (not -f $config and not -f "../../$config"){
`git clone $remote/share; git clone $remote/util`;
}
# The component ID is hidden from Rename.pl
if ($Component eq "G"."M"){
`$gitclone srcBATL` if not -d "srcBATL";
`$gitclone srcUserExtra` if not -d "srcUserExtra" and $umichgitlab;
}
if(-f $config){
require $config;
}else{
require "../../$config";
}
# Variables inherited from share/Scripts/Config.pl
our %Remaining; # Unprocessed arguments
our $ERROR;
our $WARNING;
our $Help;
our $Verbose;
our $Show;
our $ShowGridSize;
our $NewGridSize;
our $NewGhostCell;
&print_help if $Help;
# Force settings without checking
my $Force;
# Equation and user module variables
my $Src = 'src';
my $SrcUser = 'srcUser';
my $SrcUserExtra= 'srcUserExtra';
my $UserMod = "$Src/ModUser.f90";
my $UserModSafe = "$Src/ModUser.f90.safe";
my $SrcEquation = 'srcEquation';
my $EquationMod = "$Src/ModEquation.f90";
my $EquationModSafe = "$Src/ModEquation.f90.safe";
my $Equation;
my $UserModule;
# Grid size variables
my $NameBatlFile = "srcBATL/BATL_size.f90";
my $GridSize;
my $GhostCell;
my ($nI, $nJ, $nK, $nG);
# additional variable information
my $nWave;
my $nWaveNew;
my $nMaterial;
my $nMaterialNew;
my $ChargeState;
my $nChargeStateAll;
# Settings for optimization
my $OptFile = "src/ModUpdateParamFast.f90";
my $Opt;
# For SC/BATSRUS and IH/BATSRUS src/ is created during configuration of SWMF
if(not -d $Src){exit 0};
# Read previous grid size, equation and user module
&get_settings;
foreach (@Arguments){
if(/^-f$/) {$Force=1; next};
if(/^-e$/) {$Equation=1; next};
if(/^-u$/) {$UserModule=1; next};
if(/^-e=(.*)$/) {$Equation=$1; next};
if(/^-u=(.*)$/) {$UserModule=$1; next};
if(/^-s$/) {$Show=1; next};
if(/^-nWave=(.*)$/i) {
# Check the number of wave bins (to be set)
die "$ERROR nWave=$1 must be 1 or more\n" if $1 < 1;
$nWaveNew=$1;
next};
if(/^-nMaterial=(.*)$/i) {
# Check the number of material level indices (to be set)
die "$ERROR nMaterial=$1 must be 1 or more\n" if $1 < 1;
$nMaterialNew=$1;
next};
if(/^-cs=(.*)/) {$ChargeState.="$1"; next};
if(/^-ng$/i) {print "ng=$GhostCell\n"; next};
if(/^-ng=(.*)$/i) {$NewGhostCell=$1; next};
if(/^-opt$/) {$Opt="show"; next};
if(/^-opt=(.*)$/) {$Opt=$1; next};
warn "WARNING: Unknown flag $_\n" if $Remaining{$_};
}
&set_grid_size if ($NewGridSize and $NewGridSize ne $GridSize)
or ($NewGhostCell and $NewGhostCell ne $GhostCell);
# Show grid size in a compact form if requested
print "Config.pl -g=$nI,$nJ,$nK\n",
if $ShowGridSize and not $Show;
# Set or list the equations
&set_equation if $Equation;
# Set additional variable information
open(FILE, $EquationMod);
while(<FILE>){
next if /^\s*!/; # skip commented out lines
$nWave=$1 if /\bnWave\s*=\s*(\d+)/i;
$nMaterial=$1 if /\bnMaterial\s*=\s*(\d+)/i;
$nChargeStateAll=$1 if /\bnChargeStateAll\s*=\s*(\d+)/i;
}
close FILE;
die "$ERROR nWave was not found in equation module\n" if $nWaveNew and not $nWave;
&set_nwave if $nWaveNew and $nWaveNew ne $nWave;
die "$ERROR nMaterial was not found in equation module\n" if $nMaterialNew and not $nMaterial;
&set_nmaterial if $nMaterialNew and $nMaterialNew ne $nMaterial;
die "$ERROR nChargeStateAll was not found in equation module\n" if $ChargeState and not $nChargeStateAll;
&set_charge_state if $ChargeState;
# Set or list the user modules
&set_user_module if $UserModule;
my $Settings = ¤t_settings; print $Settings if $Show;
# (Re)Create Makefile.RULES file(s) based on current settings
&create_makefile_rules($Settings);
# Optimize settings in $OptFile
&set_optimization if $Opt;
exit 0;
#############################################################################
sub check_var{
# modify the first argument "oldvalue" as needed
my $oldvalue = $_[0];
my $newvalue = $_[1];
my $first = $_[2];
print "oldvalue=$oldvalue newvalue=$newvalue first=$first\n" if $Verbose;
return if $oldvalue eq "any"; # already adjustable
$newvalue =~ s/^\s+//; # remove leading space
$newvalue =~ s/\s+.*\n//; # remove comment
$newvalue =~ s/^T$/.true./; # logical true
$newvalue =~ s/^F$/.false./; # logical false
if($first or $oldvalue eq ""){
$_[0] = $newvalue; # first use of new value, make it fixed
}elsif($newvalue ne $oldvalue){
$_[0] = "any"; # two different values, cannot be fixed
}
print "modified oldvalue=$_[0]\n" if $Verbose;
}
#############################################################################
sub set_optimization{
# Read current settings
my %Opt;
open(FILE, $OptFile) or die "$ERROR could not open $OptFile\n";
while(<FILE>){
# Extract adjustables: NAME => ... lines without Orig
$Opt{$1} = 'any' if /^\s+(\w+)\s*\=\>/ and not /Orig/;
# Extract fixed params: parameter:: NAME = VALUE
$Opt{$1} = $2 if /parameter\s*::\s*(\w+)\s*=\s*(\S+)/;
last if /^\s*contains\s*$/;
}
close(FILE);
print "-" x 70, "\nCurrent optimization:\n";
my $name;
foreach $name (sort keys %Opt){
printf "%-30s = %s\n", $name, $Opt{$name};
}
return if $Opt eq "show";
# Process -opt=... argument in $Opt
print "-" x 70, "\n-opt=$Opt\n";
print "-" x 70, "\nNew optimization:\n";
my $Change; # set to 1 is settings change
if($Opt =~ /PARAM\.in/){
# Read settings from some PARAM.in file
# Default settings
my %Set = (
nOrder => 1,
DoLf => ".true.",
IsCartesian => ".true.",
IsCartesianGrid => ".true.",
IsTimeAccurate => ".true.",
UseB0 => "UseB" ,
UseBorisCorrection => ".false.",
UseDivbSource => "UseB .and. nDim>1",
UseHyperbolicDivB => ".false.",
UseNonConservative => ".false.",
iStage => 1,
nStage => 1,
nOrder => 1,
);
# Default for UseB0 depends on the component !!!
print "processing parameter file $Opt\n";
my $first = 1; # true in the first session
my $nstage = 1; # default value for nStage
open(FILE, $Opt) or die "$ERROR could not open $Opt\n";
while(<FILE>){
if(/^\#RUN\b/ or /^\#END\b/ or eof(FILE)){
check_var($Set{"nStage"}, $nstage, $first);
check_var($Set{"iStage"}, "any", $first) if $nstage !~ /^\s*1/;
last unless /^#RUN\b/;
$first = 0; # end of first session
}
# Read settings from various commands
if(/^#TIMEACCURATE\b/){
my $timeacc = <FILE>;
check_var($Set{"IsTimeAccurate"}, $timeacc, $first);
}elsif(/^#TIMESTEPLIMIT\b/){
my $do = <FILE>; # not time accurate if limiting time step
check_var($Set{"IsTimeAccurate"},'F',$first) if $do =~ /^\s*T/;
}elsif(/^#(TIMESTEPPING|RUNGEKUTTA|RK)\b/){
$nstage = <FILE>;
}elsif(/^#SCHEME\b/){
my $norder = <FILE>;
check_var($Set{"nOrder"}, $norder, $first);
$nstage = $norder; # default nStage follows nOrder
my $scheme = <FILE>;
if($scheme =~ /^\s*Rusanov/i){
check_var($Set{"DoLf"}, "T", $first);
}else{
check_var($Set{"DoLf"}, "F", $first);
}
if($norder > 1){
my $limiter = <FILE>;
if($limiter =~ /^\s*minmod/){
check_var($Set{"LimiterBeta"}, "1.0", $first);
}else{
my $beta = <FILE>;
check_var($Set{"LimiterBeta"}, $beta, $first);
}
}
}elsif(/^#BORIS\b/){
my $boris = <FILE>;
check_var($Set{"UseBorisCorrection"}, $boris, $first);
}elsif(/^#GRIDGEOMETRY\b/){
my $geometry = <FILE>;
if($geometry =~ /^cartesian/){
check_var($Set{"Cartesian"}, "T", $first);
check_var($Set{"CartesianGrid"}, "T", $first);
}elsif($geometry =~ /^rz/){
check_var($Set{"Cartesian"}, "T", $first);
check_var($Set{"CartesianGrid"}, "F", $first);
}else{
check_var($Set{"Cartesian"}, "F", $first);
check_var($Set{"CartesianGrid"}, "F", $first);
}
}elsif(/^#DIVB\b/){
my $use8wave = <FILE>;
check_var($Set{"UseDivbSource"}, $use8wave, $first);
}elsif(/^#HYPERBOLICDIVB\b/){
my $usehyp = <FILE>;
check_var($Set{"UseHyperbolicDivB"}, $usehyp, $first);
}elsif(/^#NONCONSERVATIVE\b/){
my $noncons = <FILE>;
check_var($Set{"UseNonConservative"}, $noncons, $first);
}elsif(/^#USEB0\b/){
my $useb0 = <FILE>;
check_var($Set{"UseB0"}, $useb0, $first);
}elsif(/^#PLANET\b/){
my $planet = <FILE>;
check_var($Set{"UseB0"}, "F", $first)
if $planet =~ /^\s*(NONE|VENUS)/i;
}
}
close(FILE);
foreach $name (sort keys %Set){
if($Set{$name} ne $Opt{$name}){
$Opt{$name} = $Set{$name};
$Change = 1;
}
}
if($Change){
foreach $name (sort keys %Opt){
printf "%-30s = %s\n", $name, $Opt{$name};
}
}
}else{
# -opt=none means that nothing is fixed
if($Opt =~ s/^(any|none),?//i){
foreach $name (sort keys %Opt){
next if $Opt{$name} eq 'any';
printf "%-20s%9s -> any\n", "$name:", $Opt{$name};
$Opt{$name} = 'any';
$Change = 1;
}
}
# replace =F and =T with =.false. and =.true.
$Opt =~ s/=F/=.false./g;
$Opt =~ s/=T/=.true./g;
my @Opt = split(/,/, $Opt);
foreach (@Opt){
my $newvalue = "any";
$newvalue = $1 if s/=(.*)//;
my $oldvalue = $Opt{$_};
if(not $oldvalue){
print "$ERROR Invalid variable name=$_\n";
next;
}
if($newvalue ne $oldvalue){
$Change = 1;
# Print out modified value
printf "%-20s%9s -> %s\n", "$_:", $oldvalue, $newvalue;
$Opt{$_} = $newvalue;
}
}
}
if(not $Change){
# Nothing else to be done
print "No changes in $OptFile\n";
return;
}
# Edit the file
print "Modifying $OptFile\n";
my $parameters; # true inside setting parameters part
my $checks; # true inside checking parameters part
@ARGV = ($OptFile);
while(<>){
# Change the Name => ... and NameOrig => lines
if(/(\w+)Orig\s+=>/){
$name=$1;
if($Opt{$name} eq "any"){s/Orig//};
}elsif(/(\w+)\s+=>/){
my $name = $1;
if($Opt{$name} ne "any" and $Opt{$name} ne ""){s/\s+=>/Orig =>/};
}
$parameters = 0 if /^\s*contains/;
next if $parameters; # remove original parameters
if(/Fixed values/){
print;
$parameters = 1;
foreach my $name (sort keys %Opt){
my $value = $Opt{$name};
next if $value eq 'any';
my $type = 'real';
$type = 'logical' if $name =~ /^(Is|Use|Do|Done)[A-Z]/;
$type = 'integer' if $name =~ /^([i-n]|Int[A-Z])/;
print " $type, parameter:: $name = $value\n";
}
print "\n";
next;
}
$checks = 0 if /^\s+end subroutine check/;
next if $checks; # remove original checks
if(/Check fixed/){
print;
$checks = 1;
foreach my $name (sort keys %Opt){
my $value = $Opt{$name};
next if $value eq 'any';
if($name =~ /^(Is|Use|Do|Done)[A-Z]/){
if($value eq ".true."){
print " if(.not. ${name}Orig) ".
"call CON_stop(NameSub//': $name=F')\n"
}elsif($value eq ".false."){
print " if(${name}Orig) ".
"call CON_stop(NameSub//': $name=T')\n"
}else{
print " if(${name}Orig .neqv. $value) ".
"call CON_stop(NameSub//': $name=',${name}Orig)\n"
}
}elsif($name ne 'iStage'){ # iStage cannot be checked
$value .= ' .and. nOrder > 1' if $name eq 'LimiterBeta';
print " if(${name}Orig /= $value) ".
"call CON_stop(NameSub//': $name=',${name}Orig)\n";
}
}
print "\n";
next;
}
print;
}
}
#############################################################################
sub get_settings{
# Make sure that BATL_size.f90 is up-to-date
`make $NameBatlFile`;
open(FILE, $NameBatlFile) or die "$ERROR could not open $NameBatlFile\n";
while(<FILE>){
next if /^\s*!/; # skip commented out lines
$nI=$1 if /\bnI\s*=\s*(\d+)/i;
$nJ=$1 if /\bnJ\s*=\s*(\d+)/i;
$nK=$1 if /\bnK\s*=\s*(\d+)/i;
$GhostCell=$1 if /\bnG\s*=\s*(\d)/;
}
close FILE;
die "$ERROR could not read nI from $NameBatlFile\n" unless length($nI);
die "$ERROR could not read nJ from $NameBatlFile\n" unless length($nJ);
die "$ERROR could not read nK from $NameBatlFile\n" unless length($nK);
die "$ERROR could not read nG from $NameBatlFile\n"
unless length($GhostCell);
$GridSize = "$nI,$nJ,$nK";
}
#############################################################################
sub set_grid_size{
$GridSize = $NewGridSize if $NewGridSize;
if($GridSize =~ /^[1-9]\d*,[1-9]\d*,[1-9]\d*$/){
($nI,$nJ,$nK) = split(',', $GridSize);
}elsif($GridSize){
die "$ERROR -g=$GridSize should be 3".
" positive integers separated with commas\n";
}
$GhostCell = $NewGhostCell if $NewGhostCell;
die "$ERROR -ng=$GhostCell must be 2,3,4 or 5\n" if $GhostCell!~/^[2345]$/;
# Check the grid size (to be set)
die "$ERROR nK=$nK must be 1 if nJ is 1\n" if $nJ==1 and $nK>1;
die "$ERROR nI=$nI must be an even integer\n" if $nI%2!=0;
die "$ERROR nJ=$nJ must be 1 or an even integer\n" if $nJ>1 and $nJ%2!=0;
die "$ERROR nK=$nK must be 1 or an even integer\n" if $nK>1 and $nK%2!=0;
if(not $Force){
die "$ERROR nI=$nI nJ=$nJ nK=$nK does not allow AMR\n"
if $nI == 2 or $nJ == 2 or $nK==2;
die "$ERROR -ng=$GhostCell should not exceed nI/2=$nI/2\n"
if $GhostCell > $nI/2;
die "$ERROR -ng=$GhostCell should not exceed nJ/2=$nJ/2\n"
if $GhostCell > $nJ/2 and $nJ>1;
die "$ERROR -ng=$GhostCell should not exceed nK/2=$nK/2\n"
if $GhostCell > $nK/2 and $nK>1;
}
print "Writing new grid size $GridSize and $GhostCell ghost cells into ".
"$NameBatlFile...\n";
@ARGV = ($NameBatlFile);
while(<>){
if(/^\s*!/){print; next} # Skip commented out lines
s/\b(nI\s*=[^0-9]*)(\d+)/$1$nI/i;
s/\b(nJ\s*=[^0-9]*)(\d+)/$1$nJ/i;
s/\b(nK\s*=[^0-9]*)(\d+)/$1$nK/i;
s/\b(nG\s*=[^0-9]*)\d/$1$GhostCell/i;
print;
}
}
##############################################################################
sub set_equation{
if($Equation eq '1'){
my @EquationModules;
chdir $SrcEquation;
@EquationModules = sort(glob("ModEquation?*.f90"));
for (@EquationModules){s/^ModEquation//; s/\.f90$//;}
print "Available Equations:\n ",join("\n ",@EquationModules),"\n";
chdir "..";
return;
}
my $File = "$SrcEquation/ModEquation$Equation.f90";
die "$ERROR File $File does not exist!\n" unless -f $File;
# Check if there is any change in the equation module
if(-f $EquationMod){
open(FILE1,$File); open(FILE2,$EquationMod);
my $IsSame = 1; my $line1; my $line2;
while($line1=<FILE1> and $line2=<FILE2>){
# Ignore the nWave and nMaterial definitions
next if $line1=~/nMaterial|nWave/ and $line2=~/nMaterial|nWave/;
if($line1 ne $line2){
$IsSame = 0;
last;
}
}
close(FILE1); close(FILE2);
# Do not overwrite the equation module if there are no differences
return if $IsSame;
}
`cp $EquationMod $EquationModSafe` if -f $EquationMod; # save previous eq.
print "cp $File $EquationMod\n" if $Verbose;
`cp $File $EquationMod`;
}
##############################################################################
sub set_nwave{
$nWave = $nWaveNew;
print "Writing new nWave = $nWaveNew into $EquationMod...\n";
my $nWaveTwo = sprintf("%02d", $nWaveNew);
@ARGV = ($EquationMod);
my $prev;
while(<>){
if(/^\s*!/){print; next} # Skip commented out lines
if(m/\&\s*\n/){ # Concatenate continuation lines
$prev .= $_;
next;
}
$_ = $prev . $_;
$prev = "";
s/\b(nWave\s*=[^0-9]*)(\d+)/$1$nWaveNew/i;
s/I\([^\)]+\)/I($nWaveTwo)/m if /NamePrimitiveVar\s*\=/;
print;
}
}
#############################################################################
sub set_nmaterial{
$nMaterial = $nMaterialNew;
print "Writing new nMaterial = $nMaterialNew into $EquationMod...\n";
@ARGV = ($EquationMod);
my $prev;
while(<>){
if(/^\s*!/){print; next} # Skip commented out lines
if(m/\&\s*\n/){ # Concatenate continuation lines
$prev .= $_;
next;
}
$_ = $prev . $_;
$prev = "";
s/\b(nMaterial\s*=[^0-9]*)(\d+)/$1$nMaterialNew/i;
s/M\([^\)]+\)/M($nMaterialNew)/m if /NamePrimitiveVar\s*\=/;
print;
}
}
##############################################################################
sub set_charge_state{
my @ValidChargeState = ('h','he','li','be','b','c','n','o','f','ne','na',
'mg','al','si','p','s','cl','ar','k','ca','sc',
'ti','v','cr','mn','fe','co','ni','cu','zn');
my @ValidChargeStateAll = (2..31);
# Separate input into array
my @ChargeStateIn;
@ChargeStateIn = split('\+',$ChargeState);
# Get valid element names in correct order
my @Intersection = ();
foreach my $elem_j (@ValidChargeState) {
foreach my $elem_i (@ChargeStateIn) {
if($elem_i eq $elem_j){
push @Intersection, $elem_i;
}
}
}
# Get rid of duplicates
my %seen = ();
my @Element_I = ();
foreach my $elem (@Intersection) {
next if $seen{$elem}++;
push @Element_I,$elem;
}
my $nElement = @Element_I;
# Get Z+1 for each element
my $cs = 1;
my @nChargeState_I = ();
foreach my $elem_j (@ValidChargeState) {
$cs = $cs+1;
foreach my $elem_i (@Element_I) {
if($elem_i eq $elem_j){
push @nChargeState_I, $cs;
}
}
}
# Sum of all charge states
my $nChargeStateAll = 0;
foreach (@nChargeState_I){
$nChargeStateAll += $_;
}
# Convert array to string
my $nChargeState_I=join(",",@nChargeState_I);
$nChargeState_I = " \[$nChargeState_I\]";
# Element names have length 2, convert array to string
foreach my $elem_k (@Element_I){
if(length($elem_k) == 1) {
$elem_k = $elem_k . " ";
}
}
foreach (@Element_I) {$_ = "'$_'";}
my $NameElement_I=join(",",@Element_I);
$NameElement_I = " \[$NameElement_I\]";
# Send variables to ModEquation file
@ARGV = ($EquationMod);
my $prev;
while(<>){
if(/^\s*!/){print; next} # Skip commented out lines
if(m/\&\s*\n/){ # Concatenate continuation lines
$prev .= $_;
next;
}
$_ = $prev . $_;
$prev = "";
s/\b(nChargeStateAll\s*=[^0-9]*)(\d+)/$1$nChargeStateAll/i;
s/\b(nElement\s*=[^0-9]*)(\d+)/$1$nElement/i;
s/\b(nChargeState_I\(1:nElement\)\s*=)(.*)/$1$nChargeState_I/i;
s/\b(NameElement_I\(1:nElement\)\s*=)(.*)/$1$NameElement_I/i;
print;
}
}
#############################################################################
sub set_user_module{
if($UserModule eq '1'){
my @UserModules;
chdir $SrcUser;
@UserModules = glob("*.f90");
chdir "..";
if(-d $SrcUserExtra){
chdir $SrcUserExtra;
push @UserModules, glob("*.f90");
chdir "..";
}
@UserModules = ("Default", sort @UserModules);
for (@UserModules){s/^ModUser//; s/\.f90$//;}
print "Available User Modules:\n ",join("\n ",@UserModules),"\n";
return;
}
my $File;
if($UserModule eq "Default"){
$File = "$Src/ModUserDefault.f90";
}else{
# Search in srcUser and srcUserExtra;
$File = "$SrcUser/ModUser$UserModule.f90";
$File = "$SrcUserExtra/ModUser$UserModule.f90"
if -d $SrcUserExtra and not -f $File;
}
die "$ERROR File $File does not exist!\n" unless -f $File;
return if -f $UserMod and not `diff $File $UserMod`;
`cp $UserMod $UserModSafe` if -f $UserMod; # save previous user module
print "cp $File $UserMod\n" if $Verbose;
`cp $File $UserMod`;
}
#############################################################################
sub current_settings{
$Settings = "Number of cells in a block : nI=$nI, nJ=$nJ, nK=$nK\n";
$Settings .= "Number of ghost cell layers : nG=$GhostCell\n";
open(FILE, $UserMod) or die "$ERROR Could not open $UserMod\n";
my $Module='???';
my $Version='???';
my $NameFile='???';
while(<FILE>){
if(/NameUserModule/){
$Module = $';
$Module = <FILE> if $Module =~ /\&\s*$/; # read continuation line
$Module =~ s/\s*=\s*//; # remove equal sign
$Module =~ s/^\s*[\'\"]//; # remove leading quotation mark
$Module =~ s/[\'\"].*\n//; # remove trailing quotation marks
}
$NameFile = $1 if /NameUserFile\s*=.*ModUser(.*)\.f90/;
$Version = $1 if /VersionUserModule\s*=\s*([\d\.]+)/;
}
close(FILE);
$Settings .= "UserModule = $NameFile: $Module, ver $Version\n";
open(FILE, $EquationMod) or die "$ERROR Could not open $EquationMod\n";
my $Equation='???';
my $NameFile='???';
my $prev;
my %Value;
my $ChargeStateList;
while(<FILE>){
next if /^\s*!/; # skip commented out lines
if(s/\&\s*\n//){ # concatenate continuation lines
$prev .= $_;
next;
}
$_ = $prev . $_;
$prev = "";
# Extract variable settings: var = integer, var = othervar
$Value{"$1"} = $2 while s/(\w+)\s*=\s*(\d+)//m;
$Value{"$1"} = $Value{"$2"} while s/(\w+)\s*=\s*(\w+)//m;
# Extract descriptions
$NameFile = $1 if /NameEquationFile\s*=.*ModEquation(.*)\.f90/;
$Equation = $1 if /NameEquation\s*=\s*[\'\"]([^\'\"]*)/;
# Charge states element list
$ChargeStateList = $1 if /NameElement_I\(1:nElement\)\s*=\s*(.*)/;
}
if($Verbose){
print "$_ = $Value{$_}\n" foreach (sort keys %Value);
}
$Settings .= "Equation = $NameFile: $Equation\n";
my $nVar = $Value{"nVar"};
my $nSpecies;
$nSpecies = $Value{"SpeciesLast_"} - $Value{"SpeciesFirst_"} + 1
if $Value{"SpeciesLast_"} and $Value{"SpeciesFirst_"};
my $nIonFluid = $Value{"IonLast_"} - $Value{"IonFirst_"} + 1;
my $nFluid = $Value{"nFluid"} - $Value{"IonFirst_"} + 1;
my $nNeutralFluid;
$nNeutralFluid = $nFluid - $Value{"IonLast_"} if $Value{"IonLast_"};
$Settings .= "Charge states of elements : $ChargeStateList\n" if
$nChargeStateAll;
$Settings .= "Number of state variables : nVar=$nVar\n";
$Settings .= "Number of species : nSpecies=$nSpecies\n"
if $nSpecies;
$Settings .= "Number of wave bins : nWave=$nWave\n" if $nWave;
$Settings .= "Number of materials : nMaterial=$nMaterial\n"
if $nMaterial;
$Settings .= "Number of ion fluids : nIonFluid=$nIonFluid\n"
if $nIonFluid != 1;
$Settings .= "Number of neutral fluids : nNeutralFluid=$nNeutralFluid\n"
if $nNeutralFluid;
$Settings .= "Number of fluids : nFluid=$nFluid\n"
if $nFluid > 1;
$Settings .= "Electron pressure : UsePe=1\n" if $Value{"Pe_"}>1;
$Settings; # return value
}
#############################################################################
sub print_help{
print "
Additional options for BATSRUS/Config.pl:
-g=NI,NJ,NK Set grid block size. NI, NJ and NK are the number of cells
in the I, J and K directions, respectively.
If NK = 1, the 3rd dimension is ignored: 2D grid.
If NJ=NK=1, the 2nd and 3rd dimensions are ignored: 1D grid.
In non-ignored dimensions NI, NJ, NK have to be even integers.
To allow AMR, the number of cells has to be 4 or more in all
non-ignored directions. Use -f to set the value 2 (no AMR).
-ng Print current setting for number of ghost cell layers.
-ng=NG Set number of ghost cell layers to NG around grid blocks.
The value of NG can be 2, 3, 4 or 5, but
not more than NI/2, NJ/2 (if NJ>1), and NK/2 (if NK>1).
Default value is NG=2.
-f Force the -g and -ng settings.
-e List all available equation modules.
-e=EQUATION Select equation EQUATION.
-u List all available user modules.
-u=USERMODULE Select the user module USERMODULE.
-opt Show current parameter settings for optimized code.
Variables without a value are adjustable from PARAM.in.
-opt=STRING Set parameters for optimized code. STRING can be a parameter
file name that is analyzed for settings. Otherwise STRING is a
comma separated list consisting of NAME or NAME=any elements
for adjustable parameters, or NAME=VALUE for fixed parameters.
For logical variables, T and F can be used as VALUE. If
STRING starts with 'any' or 'none', all values are adjustable.
-nWave=NWAVE Set the number of wave bins used for radiation or wave
turbulence to NWAVE for the selected EQUATION module.
-nMaterial=NM Set the number of material levels to NM
for the selected EQUATION module.
-cs=ELEMENT_I Set the + delimited list of element names for charge
state calculation.
Only the 30 first elements of the periodic table are possible.
Works with equation module AwsomChargeState in IH and SC
components only.
Examples for BATSRUS/Config.pl:
List available options for equations and user modules and show optimizations:
Config.pl -e -u -opt
Select the MHD equations, the Default user module and optimize some parameters:
Config.pl -e=MHD -u=Default -opt=none,nOrder=2,nStage=2,IsCartesian=T
Select the CRASH equation and user modules and optimize for run/PARAM.in file.
Set the number of materials to 5 and number of radiation groups to 30:
Config.pl -e=Crash -u=Crash -opt=run/PARAM.in -nMaterial=5 -nWave=30
Set list of elements for charge state calculation
Config.pl -cs=o+c+fe+mg
Set block size to 8x8x8, number of blocks to 400 and implicit blocks to 100
and number of ghost cells to 2:
Config.pl -g=8,8,8,400,100 -ng=2
Set number of blocks as a function of number of processors in Makefile.test
Config.pl -g=8,8,8,3000/\${NP},1
Show settings for BATSRUS:
Config.pl -s
\n";
exit 0;
}