-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakeRestartTree.pl
executable file
·199 lines (154 loc) · 5.7 KB
/
MakeRestartTree.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
#!/usr/bin/perl -s
my $help = $h;
my $verbose = $v;
my $savedir = $s;
my $link = $l;
my $treedir = $t;
my $force = $f;
use strict;
use warnings;
use Cwd qw(getcwd);
# Some defaults
my $cwd = getcwd;
my $INFO = "MakeRestartTree.pl";
my $ERROR = "ERROR in $INFO";
my $outdir = "$cwd" . "/" . "GM";
my $restart_outdir = "$outdir" . "/" . "restartOUT";
my $restart_indir = "$outdir" . "/" . "restartIN";
my $headfile = "restart.H";
my $headfile_tmp = "restart_tmp.H";
my $success = "BATSRUS.SUCCESS";
my $restart_tree = $savedir;
# Remove trailing '/' of name if user included it
$restart_tree =~ s/\/+$// if $savedir;
# Set time and iteration dummy to nonsense value
my $istep = -1;
my $itime = -1;
my $time_accurate_mode = -1;
&show_help if ($help);
# Sanity check
die "$ERROR: BATSRUS did not finish successfully\n" unless -e $success
or $force;
die "$ERROR: -t flag only works with -l flag\n" if ($treedir and not $link);
if ($treedir){
print "$INFO: linking existing $treedir to $restart_indir\n" if $verbose;
$restart_tree = $treedir;
&link_restartdir;
exit 0;
}
# Open the header file, get iteration step, and clean file by writing to new
open (HEADERFILE, "$restart_outdir/$headfile")
or die "$ERROR could not open file $headfile\n";
open (NHFILE, ">", "$restart_outdir/$headfile_tmp");
while ( <HEADERFILE> ){
# Remove this header for correct restart reading
print NHFILE unless m/IDEALAXES/;
if (/\#NSTEP/){
# Read in number of steps and extract
$istep = <HEADERFILE>;
print NHFILE $istep; # force line to be written to file
chop($istep);
# Remove leading spaces and trailing info
$istep =~ s/^\s+//;
$istep =~ s/\s.*//;
# Convert string to number
$istep += 0;
}
if (/\#TIMESIMULATION/){
# Read in simulation time and extract
$itime = <HEADERFILE>;
print NHFILE $itime;
chop($itime);
$itime =~ s/^\s+//;
$itime =~ s/\s.*//;
$itime += 0;
}
$time_accurate_mode = $itime;
}
close NHFILE;
close HEADERFILE;
die "$ERROR: cannot find simulation time in $headfile!\n" if $itime < 0;
die "$ERROR: cannot find iteration step in $headfile!\n" if $istep < 0;
print "$INFO: read simulation time = $itime and it = $istep from $headfile\n"
if $verbose;
rename "$restart_outdir/$headfile_tmp", "$restart_outdir/$headfile";
&make_restartdir;
&link_restartdir if $link;
exit 0;
#==============================================================================
# SUBROUTINES
#==============================================================================
sub make_restartdir{
unless ($savedir){
# Append time and/or iteration number to default directory name
if ($time_accurate_mode){
$restart_tree = sprintf("RESTART-t%9.4f-it%6d", $itime, $istep);
}else{
$restart_tree = sprintf("RESTART-it%6d", $istep);
}
# Replace spaces with zeros
$restart_tree =~ s/ /0/g;
}
print "$INFO: restart tree named as $restart_tree inside $outdir.\n"
if $verbose;
$restart_tree = "$outdir" . "/" . "$restart_tree";
# Create new restart directory
print "mkdir $restart_tree\n" if $verbose;
mkdir $restart_tree, 0755
or die "$ERROR: restart tree $restart_tree cannot be created!\n";
# Move the restart output over to the new restart tree
opendir(DIR, $restart_outdir)
or die "$ERROR: $restart_outdir does not exist!\n";
my @content = readdir(DIR);
closedir(DIR);
die "$ERROR: directory $restart_outdir is empty!\n" if $#content < 2;
rename $restart_outdir, $restart_tree or
die "$ERROR: cannot move $restart_outdir into $restart_tree!\n";
# Recreate the official output directory for new storage
print "mkdir $restart_outdir\n" if $verbose;
mkdir $restart_outdir, 0755
or die "$ERROR: cannot create directory $restart_outdir!\n";
}
sub link_restartdir{
# When directory already exists overwrite it with new link
if ( -l $restart_indir ){
print "rm -f $restart_indir\n" if $verbose;
unlink $restart_indir
or die "$ERROR: cannot remove link $restart_indir!\n";
}elsif ( -d $restart_indir ){
print "rmdir $restart_indir\n" if $verbose;
rmdir $restart_indir
or die "$ERROR: cannot remove directory $restart_indir!\n";
}
# Link restart tree with the input restart directory
print "ln -s $restart_indir $restart_tree\n" if $verbose;
symlink $restart_tree, $restart_indir or
die "$ERROR: cannot link $restart_tree to $restart_indir!\n";
}
sub show_help{
print
"Purpose:
Make a restart tree from a BATSRUS simulation in the working directory and
link it.
Usage:
MakeRestartTree.pl [-h] [-v] [-l] [-t=DIRNAME] [-s=DIRNAME]
-h Print help message and exit.
-v Print verbose info to screen.
-l Link restart tree to restartIN.
-f Force script to run even if BATSRUSS.SUCCESS is not present.
-t=DIRNAME Specify an existing restart tree DIRNAME to link with restartIN.
-s=DIRNAME Specify the directory DIRNAME to save the restart files into. It
will be prepared as GM/DIRNAME.
Default is GM/RESTART-t[TIME]-it[NSTEP] with [TIME] and [NSTEP]
the simulation time (in sec) and iteration step.
Examples:
Create restart tree from model with output located in GM/restartOUT and link
it to the input restart directory 'restartIN':
MakeRestartTree.pl -l
Create restart tree from model with output located in GM/restartOUT, put it in
a directory 'restart-isolated' inside GM, and print verbose info:
MakeRestartTree.pl -v -s=restart-isolated
"
,"\n\n";
exit;
}