-
Notifications
You must be signed in to change notification settings - Fork 4
/
CIAOLoop
executable file
·3762 lines (3090 loc) · 123 KB
/
CIAOLoop
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
#! /usr/bin/perl
#####################################################################
############################# CIAOLoop ##############################
############# Cloudy Iterative Adaptively Organized Loop ############
### Britton Smith ###
### [email protected] ###
### January, 2006 ###
#####################################################################
#####################################################################
use strict;
use File::Glob;
# autoflush STDOUT
$| = 1;
##########################################################
####################### Directory ########################
##########################################################
### ###
### I. VARIABLE DECLARATIONS ###
### ###
### A. Parameters and Global Variables ###
### B. Variables for running in parallel ###
### C. Cooling Map Mode Parameters and Variables ###
### D. Command Storage Objects ###
### ###
### II. MAIN CODE EXECUTION ###
### ###
### III. SUBROUTINES ###
### ###
### A. General Subroutines ###
### B. Bare Mode Subroutine ###
### C. Cooling Map Mode Subroutines ###
### D. Emissivity Map Mode Subroutines ###
### D. Ion Fraction Map Mode Subroutines ###
### E. Line Map Mode Subroutines ###
### F. Custom Cloudy Mode Subroutines ###
### ###
##########################################################
##########################################################
##########################################################
# Modes for running Cloudy.
my @cloudyRunModes = (\&bareMode, # bare mode (just run Cloudy with commands provided)
\&coolingMapMode, # cooling map mode
\&emissivityMapMode, # emissivity map mode
\&ionFractionMapMode, # ion fraction map mode
\&lineMapMode, # line emissivity map mode
\&newCustomMode); # Your custom run mode here!
# Initialization routines for the above running modes.
my @initializeMode = (0, # no initialization required for bare mode
\&coolingMapModeInitialize, # set temperature ranges
\&emissivityMapModeInitialize, # set temperature, energy ranges
\&ionFractionMapModeInitialize, # set temperature ranges, elements
\&lineMapModeInitialize, # set temperature ranges, ion labels
\&newCustomModeIntialize); # Your custom mode initialization routine.
#####################################################################
################# Parameters and Global Variables ###################
#####################################################################
# Cloudy executable path
my $cloudyExe = "./cloudy.exe";
# First time run or a restart
my $restart = 0;
# Resume mode: scan for incomplete maps and start there.
my $resume = 0;
# A reprocess run (using existing data)
# or a run that will call Cloudy.
my $reprocess_run = 0;
# Restart Index
my $restartIndex = 0;
# Output file prefix
my $outputFilePrefix = "CIAOLoop";
# Output directory
my $outputDir = "";
# Run file path
my $runFile = "";
# Type of Cloudy run.
my $cloudyRunMode = 0;
# Save all output from Cloudy.
my $saveCloudyOutputFiles = 1;
# Save only the files necessary for table creation.
my $saveMinimumOutputFiles = 0;
# Exit program when Cloudy crashes
my $exitOnCrash = 0;
# Test run (don't run cloudy, just make run file)
# 0 = actual run, 1 = test
my $test = 0;
# Total number of Cloudy runs
my $totalRuns = 1;
# Index of first run
my $currentRunIndex = 1;
#####################################################################
################# Variables for running in parallel #################
#####################################################################
# Parallel on/off flag
my $parallel = 0;
# Array of PIDs for active children
# If we don't wait for them to finish, they'll become zombies.
my @pids = ();
# User login (for remote login)
my $login = getlogin() || (getpwuid($<))[0];
# Remote login (rsh, ssh, etc.)
my $remoteLogin = "rsh";
$remoteLogin.= " -l $login" if ($login);
# Machine file for parallel run
my $machineFile = "";
# Array with machine names for parallel run
my @machines = ();
# List of machines available for use
my @machinesAvailable = ();
# List of machines currently unvailable (because they're already running Cloudy)
my @machinesUnavailable = ();
# When child spawns to run Cloudy, this variable tells it which machine to run on
my $myMachine = "";
#####################################################################
#### Variables for running multiple processors on local machine #####
#####################################################################
# Multiple processor on/off flag
my $multiProcessor = 0;
# Number of processors to use on machine
my $numberMultiProcessors = 0;
#####################################################################
############### Variables for performing run in parts ###############
#####################################################################
# Multiple parts on/off flag
my $multiParts = 0;
# Total number of parts
my $totalMultiParts;
# This specific part
my $thisMultiPart;
# Starting index for this part
my $multiPartStartIndex;
# Ending index for this part
my $multiPartEndIndex;
#####################################################################
############# Cooling Map Mode Parameters and Variables #############
#####################################################################
# minimum temperature of cooling map
my $coolingMapTmin;
# maximum temperature of cooling map
my $coolingMapTmax;
# linear temperature step for cooling map (unlikely to be used)
my $coolingMapdT;
# log temperature step for cooling map
my $coolingMapdLogT;
# number of temperature points in cooling map
my $coolingMapTpoints;
# temperature values for cooling map
my @coolingMapTemperatures;
# scaling of cooling values
# 1: n_H^2
# 2: n_H * n_e
my $coolingScaleFactor = 1;
# flag to attenuate radiation over a Jeans length
my $coolingMapUseJeansLength = 0;
# maximum length scale to use with radiation attenuation
my $coolingMapMaximumJeansLength = 3.086e20; # 100 pc
# decimal precision for temperature in cooling maps
my $temperaturePrecision = 6;
# decimal precision for heating in cooling maps
my $heatingPrecision = 6;
# decimal precision for cooling in cooling maps
my $coolingPrecision = 6;
# decimal precision for mean molecular weight in cooling maps
my $mmwPrecision = 6;
# atomic weights used for calculating mean molecular weights
my @mass = ();
#####################################################################
########### Emissivity Map Mode Parameters and Variables ############
#####################################################################
# File name for file containing emissivity map energies
my $emissivityMapEnergyFile;
# Minimum energy for emissivity map
my $emissivityMapEmin;
# Maximum energy for emissivity map
my $emissivityMapEmax;
# Number of energy points for emissivity map
my $emissivityMapEpoints;
# Energy units for emissivity map
my $emissivityMapEnergyUnits = "Rydbergs";
# Flag for log energy bins
my $emissivityMapLogEnergyBins;
# Energy values for emissivity map (bin centers)
my @emissivityMapEnergies;
# Energy bin widths for emissivity map
my @emissivityMapEnergyBins;
# Decimal precision for emissivity in emissivity maps
my $emissivityPrecision = 6;
# Energy units conversion hash
# conversions to Hz
my %energyConversion = ('MHz' => 1e-6,
'eV' => 4.1356668e-15,
'keV' => 4.1356668e-18,
'Rydbergs' => 3.04093147e-16);
#####################################################################
########## Ion Fraction Map Mode Parameters and Variables ###########
#####################################################################
# Array of requested elements for ion fractions.
my @ionFractionElements = ();
# Float precision for output of log temperature.
my $ionFractionLogTPrecision = 3;
# Float precision for output of ion fraction vales.
my $ionFractionPrecision = 3;
# Hash of atomic numbers for elements.
my %atomicNumber;
# Hash of full atomic names.
my %atomicName;
#####################################################################
############### Line Map Mode Parameters and Variables ##############
#####################################################################
# Array of lines for which to get emissivities.
my @lineMapLines = ();
# Array of ascii friendlier line labels.
my @lineMapLineLabels = ();
#####################################################################
#################### Command Storage Objects ########################
#####################################################################
# commands to be issued every iteration
my @constantCommands = ();
# commands to be loop over
my @loopCommands = ();
#####################################################################
#####################################################################
#####################################################################
#####################################################################
####################### Main Code Execution #########################
#####################################################################
# parse command line
my $parameterFile = "";
while (my $arg = shift @ARGV) {
# print help
if ($arg =~ /^-h$/) {
&printHelp();
}
# restart an incomplete run
if ($arg =~ /^-r$/) {
$restart = 1;
}
# restart an incomplete run
if ($arg =~ /^-rx$/) {
$resume = 1;
}
# run in parallel
elsif ($arg =~ /^-m$/) {
$parallel = 1;
$machineFile = glob(shift @ARGV);
die "Machine file $machineFile does not exist.\n" unless (-e $machineFile);
}
# run in parts
elsif ($arg =~ /^-mp$/) {
$multiParts = 1;
$thisMultiPart = shift @ARGV;
die "First argument after -mp flag must be a positive integer.\n" if ($thisMultiPart =~ /\D/);
$totalMultiParts = shift @ARGV;
die "Second argument after -mp flag must be a positive integer.\n" if ($totalMultiParts =~ /\D/);
die "Multipart arguments cannot be zero.\n" if (($thisMultiPart == 0) || ($totalMultiParts == 0));
die "First argument must be less than or equal to second.\n" if ($thisMultiPart > $totalMultiParts);
}
# run on multiprocessor machine
elsif ($arg =~ /^-np$/) {
$multiProcessor = 1;
$parallel = 1;
$numberMultiProcessors = shift @ARGV;
die "Argument after -np flag should be a positive integer.\n" if (($numberMultiProcessors =~ /\D/) ||
($numberMultiProcessors <= 0));
}
# do not run cloudy, just reprocess existing output data
elsif ($arg =~ /^-x$/) {
$reprocess_run = 1;
}
# get parameter file name
else {
$parameterFile = glob($arg);
}
}
# read parameter file
&readParameterFile($parameterFile);
# add start index to total number of runs
$totalRuns += $currentRunIndex - 1;
# make header for run file
if ($multiParts) {
$runFile = $outputDir . $outputFilePrefix . ".run.part" . $thisMultiPart . "_" . $totalMultiParts;
}
else {
$runFile = $outputDir . $outputFilePrefix . ".run";
}
# if running in multiple parts, calculate starting and ending indices
if ($multiParts) {
my $mapsPerPart = ($totalRuns - $currentRunIndex + 1) / $totalMultiParts;
$mapsPerPart = int($mapsPerPart+1) if ($mapsPerPart != int($mapsPerPart));
$multiPartStartIndex = $mapsPerPart * ($thisMultiPart - 1) + $currentRunIndex;
$multiPartEndIndex = $multiPartStartIndex + $mapsPerPart - 1;
print "Running part $thisMultiPart of $totalMultiParts, maps $multiPartStartIndex to $multiPartEndIndex.\n";
}
print "Run started at " . scalar (localtime) . "\n";
# restarting an incomplete run
# find where the run left off
if ($restart) {
&findRestartIndex($runFile);
print "Restarting run from index $restartIndex.\n";
}
# beginning a new run
# write the header to the run file
else {
&writeRunFileHeader($runFile);
}
# if this is a parallel job, get machines from machine file
if ($parallel) {
&getMachines($machineFile);
}
# initialize run mode if necessary
$initializeMode[$cloudyRunMode]->() if (ref($initializeMode[$cloudyRunMode]) eq 'CODE');
# if loop commands were given, start recursive looping over commands
if (@loopCommands) {
&recurse();
}
# if no loop commands given, run Cloudy with only constant commands
else {
&runCloudyMode();
}
# if running in parallel, wait for children to finish before exiting
while (@machinesUnavailable) {
my @machineListTemp = ();
while (my $machineRunning = shift @machinesUnavailable) {
# children communicate that they're done by deleting their .mach file
my $runningMachineFile;
if ($multiParts) {
$runningMachineFile = $outputDir . $outputFilePrefix .
".part" . $thisMultiPart . "_" . $totalMultiParts .
"_" . $machineRunning . ".mach";
}
else {
$runningMachineFile = $outputDir . $outputFilePrefix . "_" . $machineRunning . ".mach";
}
if (-e $runningMachineFile) {
push @machineListTemp,$machineRunning;
}
else {
push @machinesAvailable,$machineRunning;
}
}
@machinesUnavailable = @machineListTemp;
sleep 10 if (@machinesUnavailable);
}
# The End
print "Run completed successfully at " . scalar (localtime) . "\n";
#####################################################################
#####################################################################
#####################################################################
#####################################################################
###################### General Subroutines ##########################
#####################################################################
## CONTENTS ##
##
## printHelp - print helpful information on run time flags.
##
## readParameterFile - Read all parameters and commands from file
##
## writeRunFileHeader - Write header for run file, containing
## general information on run and all files
## made.
##
## findRestartIndex - If run is being restarted with -r flag,
## find where run left off and start there.
##
## recurse - This is the recursive engine that loops over all
## values of all commands, generating commands to be
## run by Cloudy.
##
## processCommands - Recurse creates a complicated data structure
## of commands to be given to Cloudy. This
## subroutine turns that data structure into
## a simple array with one Cloudy-ready command
## in each element.
## Also generates an array whose elements can
## be used in a header file specific to each
## iteration through the loop commands.
## Also generates an array containing names of
## any extra output files specified by the user.
## Return value of this function is a set of
## pointers to each of the three above arrays.
##
## runCloudy - This subroutine makes the actual call to Cloudy.
## It takes in names of input and output files to be
## used with Cloudy, as well as an array containing
## all commands to be given, in the form provided by
## processCommands. After Cloudy exits, this routine
## calls checkForCrash for any signs that Cloudy did
## not run successfully. Return value is an array
## containing any warning produced by Cloudy.
##
## runCloudyMode - Calls the specific run mode subroutine selected
## by the user.
## If in parallel mode, passes Cloudy calls out
## to machines specified in machine file given
## with -p flag.
## Also, updates run file with information on
## each iteration through commands.
##
## checkForCrash - Parses through cloudy output, searching for
## warnings and cautions. Returns any that are
## found.
##
## getMachines - If parallel mode is enabled with the -p flag,
## read in an mpirun-like machine file to get list
## of available machines.
##
## makeTempFile - If this is a reprocess run, make temp files
## from the stored data files for use in the
## post-processing.
##
## pushFile - Appends contents of one file to another, adding
## contents of a string to the appended file.
##
## check_point_completed - Check if a point in the map has been
## completed.
#####################################################################
############################# printHelp #############################
# Print flag information.
sub printHelp {
print "CIAOLoop:\n";
print "Usage: ./CIAOLoop [flags] <parameter file>\n";
print "\t-h: print this help text.\n";
print "\t-m <filename>: supply machine file for running on multiple machines.\n";
print "\t-mp <this part> <total parts>: break run into parts to be run separately.\n";
print "\t-np <number of processors>: run on multiple cores on a single machine.\n";
print "\t-r: restart run from last file finished.\n";
print "\t-rx: resume mode: scan for incomplete files and restart where they left off.\n";
print "\t-x: reprocess existing output data instead of running Cloudy.\n";
exit(0);
}
#####################################################################
########################## readParameterFile ########################
# Open parameter file and get commands and options
sub readParameterFile {
my ($parameterFile) = @_;
die "ERROR: Could not find parameter file: $parameterFile.\n" unless (-e $parameterFile);
# Parse parameter file.
my $lineNumber = 0;
open (PAR, "<$parameterFile") or die "ERROR: Could not open parameter file.\n";
PARLOAD: while (my $line = <PAR>) {
$lineNumber++;
chomp $line;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
$line =~ s/\#.*//;
if (!$line) {
next PARLOAD;
}
##################################
####### General parameters #######
##################################
# get path to Cloudy executable
elsif ($line =~ /^cloudyExe(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
$value = glob($value);
$value = "./" . $value unless ($value =~ /\//);
# if this is a reprocess run, we don't care about the exe
unless ($reprocess_run) {
die "No output file prefix given on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Invalid path to Cloudy executable specified on line $lineNumber of $parameterFile.\n"
unless (-f $value);
}
$cloudyExe = $value;
undef $value;
}
# get output file prefix
elsif ($line =~ /^outputFilePrefix(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No output file prefix given on line $lineNumber of $parameterFile.\n"
unless ($value);
die "File prefix should not start with \'.\' on line $lineNumber of $parameterFile.\n"
if ($value =~ /^\./);
$value =~ s/\s/\_/g;
die "Output file prefix contains bad characters on line $lineNumber of $parameterFile.\n"
if ($value =~ /[^\w\-]/);
$outputFilePrefix = $value;
undef $value;
}
# get output directory
elsif ($line =~ /^outputDir(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No output directory given on line $lineNumber of $parameterFile.\n"
unless ($value);
$value =~ s/\s/\_/g;
($outputDir) = glob($value);
undef $value;
mkdir $outputDir, 0755 or die "Couldn't create directory: $outputDir.\n" unless (-d $outputDir);
$outputDir .= "/" unless ($outputDir =~ /\/$/);
}
# get cloudy run mode
elsif ($line =~ /^cloudyRunMode(\z|[\s\=])/) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No cloudy run mode given on line $lineNumber of $parameterFile.\n"
unless (defined($value));
die "$value is not a valid run mode on line $lineNumber of $parameterFile.\n"
unless (ref($cloudyRunModes[$value]) == 'CODE');
$cloudyRunMode = $value;
undef $value;
}
# get index of first run (default: 1)
elsif ($line =~ /^runStartIndex(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "runStartIndex must be a positive integer on line $lineNumber of $parameterFile.\n"
if ($value =~ /\D/);
$currentRunIndex = $value;
undef $value;
}
# get option to save all Cloudy output
elsif ($line =~ /^saveCloudyOutputFiles(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "saveCloudyOutputFiles must be set to 0 or 1 on line $lineNumber of $parameterFile.\n"
unless (($value eq '0') || ($value eq '1'));
$saveCloudyOutputFiles = $value;
undef $value;
}
# get option to save just the minimum output
elsif ($line =~ /^saveMinimumOutputFiles(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "saveMinimumOutputFiles must be set to 0 or 1 on line $lineNumber of $parameterFile.\n"
unless (($value eq '0') || ($value eq '1'));
$saveMinimumOutputFiles = $value;
undef $value;
}
# get option to exit when Cloudy crashes
elsif ($line =~ /^exitOnCrash(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "exitOnCrash must be set to 0 or 1 on line $lineNumber of $parameterFile.\n"
unless (($value eq '0') || ($value eq '1'));
$exitOnCrash = $value;
undef $value;
}
# get option to do test run (just make run file)
elsif ($line =~ /^test(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "test must be set to 0 or 1 on line $lineNumber of $parameterFile.\n"
unless (($value eq '0') || ($value eq '1'));
$test = $value;
undef $value;
}
######################################
######## Commands For Cloudy #########
######################################
# get Cloudy command to be executed every time
elsif ($line =~ /^command(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2 if ($line =~ /^command\s*\=/);
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No Cloudy command given on line $lineNumber of $parameterFile.\n"
unless ($value);
## assign command to something here
push @constantCommands, $value."\n";
undef $value;
}
# get file(s) containing commands for Cloudy
elsif ($line =~ /^file(\z|[\s\=])/i) {
# create a subroutine to return anonymous subroutines to read lines from file
sub returnGetFile {
my $file = shift;
my $fileContents = sub {
my ($option) = @_;
# return file contents
if ($option) {
open (FILE,"<$file") or die "Couldn't open $file.\n";
my @fileLines = <FILE>;
close (FILE);
return @fileLines;
}
# or just return file name
else {
return $file;
}
};
return $fileContents;
}
# get list of files to be looped over
if ($line =~ /^file\s+loop/i) {
my $number = 0;
my $values = $'; #';
die "No files given on line $lineNumber of $parameterFile.\n"
unless ($values);
$values =~ s/,/ /g;
push @{$loopCommands[@loopCommands]{values}}, ();
while ($values =~ /(\S+)/g) {
my @files = glob($1);
$number += @files;
foreach my $file (@files) {
die "Can't find file: $file.\n" unless (-e $file);
## assign loop files to something here
push @{$loopCommands[-1]{values}}, returnGetFile($file);
}
}
$totalRuns *= $number;
}
# get only one file, possibly more with * operator, all are opened
# on every iteration
else {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No file given on line $lineNumber of $parameterFile.\n"
unless ($value);
my @files = glob($value);
foreach my $file (@files) {
die "Can't find file: $file.\n"
unless (-e $file);
## assign files to something here
push @constantCommands, returnGetFile($file);
}
}
} # end file command
# get Cloudy command to be looped over
elsif ($line =~ /^loop(\z|[\s\[\{])/) {
my $number = 0;
# loop over one variable
# loop over variable between brackets
if ($line =~ /\[(.+)\]/) {
$loopCommands[@loopCommands]{command} = $1;
my $values = $'; #';
die "No values given for command: $1 on line $lineNumber of $parameterFile.\n"
unless ($values);
# loop values given in for loop form
if ($values =~ /\((-?\d*\.?\d*)\;(-?\d*\.?\d*)\;(-?\d*\.?\d*)\)/) {
my $start = $1;
my $end = $2;
my $step = $3;
## assign loop commands to something here
die "Infinite loop created with step size = 0 on line $lineNumber of $parameterFile.\n"
if ($step == 0);
die "Infinite loop created on line $lineNumber of $parameterFile.\n"
if (($end-$start)/$step < 0);
for (my $q = $start;(($start < $end) ? $q <= $end : $q >= $end);$q += $step) {
push @{$loopCommands[-1]{values}}, $q;
$number++;
}
}
# loop values given in a list
else {
$values =~ s/,/ /g;
while ($values =~ /(\S+)/g) {
## assign loop command to something here
push @{$loopCommands[-1]{values}}, $1;
$number++;
}
}
}
# loop over a set of variables
if ($line =~ /\{/) {
my @numbers = ();
die "Improper syntax for loop set on line $lineNumber of $parameterFile.\n"
if ($line =~ /\{\S/);
$loopCommands[@loopCommands] = ();
SET: while (my $line = <PAR>) {
$lineNumber++;
chomp $line;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
$line =~ s/\#.+//;
if ($line =~ /\}/) {
die "Improper syntax for ending loop set on line $lineNumber of $parameterFile.\n"
if ($line =~ /.+\}|\}.+/);
last SET;
}
# loop over variable between brackets
elsif ($line =~ /\[(.+)\]/) {
$loopCommands[-1][@{$loopCommands[-1]}]{command} = $1;
my $values = $'; #';
die "No values given for command: $1 on line $lineNumber of $parameterFile.\n"
unless ($values);
# loop values given in for loop form
if ($values =~ /\((-?\d*\.?\d*)\;(-?\d*\.?\d*)\;(-?\d*\.?\d*)\)/) {
my $start = $1;
my $end = $2;
my $step = $3;
## assign loop commands to something here
die "Infinite loop created with step size = 0 on line $lineNumber of $parameterFile.\n"
if ($step == 0);
die "Infinite loop created on line $lineNumber of $parameterFile.\n"
if (($end-$start)/$step < 0);
push @numbers, 0;
for (my $q = $start;(($start < $end) ? $q <= $end : $q >= $end);$q += $step) {
push @{$loopCommands[-1][-1]{values}}, $q;
$numbers[-1]++;
}
}
# loop values given in a list
else {
$values =~ s/,/ /g;
push @numbers, 0;
while ($values =~ /(\S+)/g) {
## assign loop command to something here
push @{$loopCommands[-1][-1]{values}}, $1;
$numbers[-1]++;
}
}
die "Unequal number of values in loop set in line $lineNumber of $parameterFile.\n"
unless ($numbers[0] == $numbers[-1]);
}
elsif (!($line)) {
next SET;
}
else {
die "Improper commands inside loop set on line $lineNumber of $parameterFile.\n";
}
}
$number = $numbers[0];
}
$totalRuns *= $number;
} # end loop command
######################################
#### Cooling Map Mode Parameters #####
######################################
# cooling map mode options
# get minimum temperature
elsif ($line =~ /coolingMapTmin(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No minimum temperature given on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Invalid minimum temperature given on line $lineNumber of $parameterFile.\n"
if ($value < 0);
$coolingMapTmin = $value;
}
# get maximum temperature
elsif ($line =~ /coolingMapTmax(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No maximum temperature given on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Invalid maximum temperature given on line $lineNumber of $parameterFile.\n"
if ($value < 0);
$coolingMapTmax = $value;
}
# get coolingMapdT (temperature step size)
elsif ($line =~ /coolingMapdT(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No dT given on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Invalid dT given on line $lineNumber of $parameterFile.\n"
if ($value < 0);
$coolingMapdT = $value;
}
# get dLogT (temperature step size)
elsif ($line =~ /coolingMapdLogT(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No dT given on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Invalid dT given on line $lineNumber of $parameterFile.\n"
if ($value < 0);
$coolingMapdLogT = $value;
}
# get number of temperature steps
elsif ($line =~ /coolingMapTpoints(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No value given for Tsteps on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Invalid value given for Tsteps on line $lineNumber of $parameterFile.\n"
if ($value < 0);
$coolingMapTpoints = $value;
}
# get cooling scale factor flag
elsif ($line =~ /coolingScaleFactor(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No value given for coolingScaleFactor on line $lineNumber of $parameterFile.\n"
unless ($value);
$coolingScaleFactor = $value;
}
# flag to use Jeans length as length scale
elsif ($line =~ /coolingMapUseJeansLength(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "coolingMapUseJeansLength must be set to 0 or 1 on line $lineNumber of $parameterFile.\n"
unless (($value eq '0') || ($value eq '1'));
$coolingMapUseJeansLength = $value;
}
# maximum length scale to consider for Jeans length
elsif ($line =~ /coolingMapMaximumJeansLength(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "coolingMapMaximumJeansLength must be positive on line $lineNumber of $parameterFile.\n"
unless ($value gt '0');
$coolingMapMaximumJeansLength = $value;
}
######################################
### Emissivity Map Mode Parameters ###
######################################
# emissivity map mode options
# get energies from a file
elsif ($line =~ /^emissivityMapEnergyFile(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No file name given for emissivity map energy file on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Emissivity map energy file, $value, does not exist.\n"
unless (-e (glob($value))[0]);
$emissivityMapEnergyFile = (glob($value))[0];
}
# get minimum energy
elsif ($line =~ /^emissivityMapEmin(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;
(undef,$value) = split " ", $line, 2 unless (defined($value));
$value =~ s/^\s+//;
die "No minimum energy given on line $lineNumber of $parameterFile.\n"
unless ($value);
die "Invalid minimum energy given on line $lineNumber of $parameterFile.\n"
if ($value < 0);
$emissivityMapEmin = $value;
}
# get maximum energy
elsif ($line =~ /^emissivityMapEmax(\z|[\s\=])/i) {
my (undef,$value) = split /=/, $line, 2;