| 1 | #!/bin/bash |
|---|
| 2 | ######################################################################## |
|---|
| 3 | #### Launching script for a chained simulation of PEM and PCM runs #### |
|---|
| 4 | ######################################################################## |
|---|
| 5 | # This script can take an argument: |
|---|
| 6 | # 1) None: to start a simulation from scratch; |
|---|
| 7 | # 2) 're': to relaunch a simulation from a starting point (interactive prompt). |
|---|
| 8 | ######################################################################## |
|---|
| 9 | set -e |
|---|
| 10 | trap 'echo -e "\033[31mError: an issue occurred in the script on line $LINENO! Please review the command and try again.\033[0m"' ERR |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | ######################################################################## |
|---|
| 14 | # Modify here the parameters for the simulation |
|---|
| 15 | ############################################### |
|---|
| 16 | # Set the number of years to be simulated, either Martian or Earth years (> 0): |
|---|
| 17 | n_mars_years=100. |
|---|
| 18 | #n_earth_years=300. |
|---|
| 19 | |
|---|
| 20 | # Set the number of initial PCM years (>= 2): |
|---|
| 21 | nPCM_ini=3 |
|---|
| 22 | |
|---|
| 23 | # Set the number of PCM years between each PEM run (>= 2): |
|---|
| 24 | nPCM=2 |
|---|
| 25 | |
|---|
| 26 | # Set the launching mode (0 = "processing scripts"; any other values = "submitting jobs"). The former option is usually used to process the script on a local machine while the latter is used to submit jobs on a supercomputer with SLURM or PBS/TORQUE: |
|---|
| 27 | mode=1 |
|---|
| 28 | ######################################################################## |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | dir=`pwd` |
|---|
| 32 | machine=`hostname` |
|---|
| 33 | user=`whoami` |
|---|
| 34 | if [ ! -f "lib_launchPEM.sh" ]; then |
|---|
| 35 | echo "Error: file \"lib_launchPEM.sh\" does not exist in $dir!" |
|---|
| 36 | echo "It can be found in the PEM deftank." |
|---|
| 37 | exit 1 |
|---|
| 38 | fi |
|---|
| 39 | |
|---|
| 40 | source lib_launchPEM.sh |
|---|
| 41 | export mode |
|---|
| 42 | |
|---|
| 43 | if [ $# -eq 0 ]; then |
|---|
| 44 | # Starting from scratch |
|---|
| 45 | echo "The launching script is starting!" |
|---|
| 46 | echo "The output file is \"launchPEM.log\"." |
|---|
| 47 | exec > launchPEM.log 2>&1 |
|---|
| 48 | echo "Beginning of the launching script for the PEM simulation." |
|---|
| 49 | date |
|---|
| 50 | checklaunch |
|---|
| 51 | initlaunch |
|---|
| 52 | cyclelaunch $mode $nPCM_ini |
|---|
| 53 | |
|---|
| 54 | else |
|---|
| 55 | # Starting a new cycle |
|---|
| 56 | if [ $1 = "new" ]; then |
|---|
| 57 | exec >> launchPEM.log 2>&1 |
|---|
| 58 | echo |
|---|
| 59 | echo "This is a new cycle for the PEM simulation." |
|---|
| 60 | date |
|---|
| 61 | if [ $mode -ne 0 ]; then |
|---|
| 62 | job_scheduler |
|---|
| 63 | if command -v squeue &> /dev/null; then |
|---|
| 64 | unset SLURM_MEM_PER_CPU SLURM_MEM_PER_GPU SLURM_MEM_PER_NODE |
|---|
| 65 | fi |
|---|
| 66 | fi |
|---|
| 67 | read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < launchPEM.info |
|---|
| 68 | cyclelaunch $mode $nPCM |
|---|
| 69 | |
|---|
| 70 | # Starting a relaunch |
|---|
| 71 | elif [ $1 = "re" ]; then |
|---|
| 72 | if [ ! -f "launchPEM.info" ]; then |
|---|
| 73 | echo "Error: file \"launchPEM.info\" does not exist in $dir!" |
|---|
| 74 | echo "It is necessary to relaunch a PEM simulation." |
|---|
| 75 | errlaunch |
|---|
| 76 | fi |
|---|
| 77 | echo "The relaunch is initialized with a previous successful run to be specified." |
|---|
| 78 | while true; do |
|---|
| 79 | echo "Do you want to relaunch from a 'PCM' or 'PEM' run?" |
|---|
| 80 | read relaunch |
|---|
| 81 | if [ $relaunch = "PCM" ] || [ $relaunch = "PEM" ]; then |
|---|
| 82 | break |
|---|
| 83 | else |
|---|
| 84 | echo "Invalid input. Please enter 'PCM' or 'PEM'." |
|---|
| 85 | fi |
|---|
| 86 | done |
|---|
| 87 | read i_myear n_myear_old convert_years iPCM iPEM nPCM_old nPCM_ini_old < launchPEM.info |
|---|
| 88 | while true; do |
|---|
| 89 | if [ $relaunch = "PCM" ]; then |
|---|
| 90 | echo "What is the number of the PCM run?" |
|---|
| 91 | echo "It should be between 1 and $(( $((iPCM - 1)) > 1 ? $((iPCM - 1)) : 1 ))." |
|---|
| 92 | read irelaunch |
|---|
| 93 | if [ 1 -le $irelaunch ] && [ $irelaunch -le $(( $((iPCM - 1)) > 1 ? $((iPCM - 1)) : 1 )) ]; then |
|---|
| 94 | break |
|---|
| 95 | else |
|---|
| 96 | echo "Invalid input. Please enter a valid PCM run number." |
|---|
| 97 | fi |
|---|
| 98 | else |
|---|
| 99 | echo "What is the number of the PEM run?" |
|---|
| 100 | echo "It should be between 1 and $(( $((iPEM - 1)) > 1 ? $((iPEM - 1)) : 1 ))." |
|---|
| 101 | read irelaunch |
|---|
| 102 | if [ 1 -le $irelaunch ] && [ $irelaunch -le $(( $((iPEM - 1)) > 1 ? $((iPEM - 1)) : 1 )) ]; then |
|---|
| 103 | break |
|---|
| 104 | else |
|---|
| 105 | echo "Invalid input. Please enter a valid PEM run number." |
|---|
| 106 | fi |
|---|
| 107 | fi |
|---|
| 108 | done |
|---|
| 109 | exec >> launchPEM.log 2>&1 |
|---|
| 110 | echo |
|---|
| 111 | echo "This is a relaunch for the PEM simulation from the run \"$relaunch $irelaunch\"." |
|---|
| 112 | date |
|---|
| 113 | checklaunch |
|---|
| 114 | convertyears |
|---|
| 115 | if [ $nPCM_ini -ne $nPCM_ini_old ]; then |
|---|
| 116 | echo "The number of initial PCM years has been modified from $nPCM_ini_old to $nPCM_ini." |
|---|
| 117 | fi |
|---|
| 118 | if [ $nPCM -ne $nPCM_old ]; then |
|---|
| 119 | echo "The number of PCM years between each PEM run has been modified from $nPCM_old to $nPCM." |
|---|
| 120 | fi |
|---|
| 121 | if [ "$(echo "$n_myear != $n_myear_old" | bc)" -eq 1 ]; then |
|---|
| 122 | echo "The number of initial PCM years has been modified from $n_myear_old to $n_myear." |
|---|
| 123 | fi |
|---|
| 124 | sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" launchPEM.info |
|---|
| 125 | if [ -f "kill_launchPEM.sh" ]; then |
|---|
| 126 | ./kill_launchPEM.sh |
|---|
| 127 | fi |
|---|
| 128 | if [ $relaunch = "PCM" ]; then |
|---|
| 129 | relaunchPCM $mode |
|---|
| 130 | else |
|---|
| 131 | relaunchPEM $mode |
|---|
| 132 | fi |
|---|
| 133 | |
|---|
| 134 | # Continuing the PEM run |
|---|
| 135 | elif [ $1 = "cont" ]; then |
|---|
| 136 | exec >> launchPEM.log 2>&1 |
|---|
| 137 | echo |
|---|
| 138 | echo "This is a continuation of the previous PEM run." |
|---|
| 139 | date |
|---|
| 140 | submitPEM $mode |
|---|
| 141 | |
|---|
| 142 | # Default case: error |
|---|
| 143 | else |
|---|
| 144 | echo "Error: given argument '$1' for the launching script is unknown!" |
|---|
| 145 | errlaunch |
|---|
| 146 | fi |
|---|
| 147 | fi |
|---|