test_schiff_sphere.sh (5035B)
1 #!/bin/sh 2 3 # Copyright (C) 2015, 2016, 2026 Centre National de la Recherche Scientifique 4 # Copyright (C) 2026 Clermont Auvergne INP 5 # Copyright (C) 2026 Institut Mines Télécom Albi-Carmaux 6 # Copyright (C) 2017, 2019-2021, 2026 |Méso|Star> (contact@meso-star.com) 7 # Copyright (C) 2026 Université de Lorraine 8 # Copyright (C) 2026 Université de Toulouse 9 # 10 # This program is free software: you can redistribute it and/or modify 11 # it under the terms of the GNU General Public License as published by 12 # the Free Software Foundation, either version 3 of the License, or 13 # (at your option) any later version. 14 # 15 # This program is distributed in the hope that it will be useful, 16 # but WITHOUT ANY WARRANTY; without even the implied warranty of 17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 # GNU General Public License for more details. 19 # 20 # You should have received a copy of the GNU General Public License 21 # along with this program. If not, see <http://www.gnu.org/licenses/>. */ 22 23 set -e 24 25 tmpfile=schiff_result 26 tmppipe=schiff_pipe 27 28 nrealisations=1000 29 nangles=1000 30 ninvcum=100 31 ndirs=100 32 wavelength=0.6 # in micron (in vacuum) 33 N=1.4666666666666666666666666 # Real refractive index 34 K=0.0053333333333333333333333 # Imaginary refractive index 35 Ne=1.3333333333333333333333333 # Refractive index of the medium 36 sigma=1.18 37 mean_radius="1.0 3.22 5.44 7.67 9.89 12.1 14.3 16.6 19.0 21.0" 38 39 # Extinction Absorption Scattering 40 references="\ 41 8.39 0.484 7.90 42 69.3 13.4 55.9 43 197 54.6 143 44 392 131 261 45 651 243 408 46 975 393 582 47 1370 578 787 48 1820 799 1020 49 2390 1080 1310 50 2930 1350 1580" 51 52 ######################################################################## 53 # Helper functions 54 ######################################################################## 55 die() 56 { 57 rm -rf "${tmpdir}" # cleanup temporary files 58 exit "${1:-1}" # return status code (default is 1) 59 } 60 61 # Displays the number of lines in the stream transmitted via standard 62 # input. This function wraps the "wc -l" command, whose output does not 63 # directly correspond to the number of lines. Depending on the shell 64 # being used, there may be leading spaces, and the line count may be 65 # followed by the filename, even if it is standard input. 66 wcl() 67 { 68 wc -l \ 69 | sed -e 's/^[[:space:]]\{0,\}//g' -e 's/[[:space:]]\{1,\}/ /g' \ 70 | cut -d' ' -f1 71 } 72 73 # print whether or not the $1 is equal to $2 +/- 4*$3 74 eq_eps() 75 { 76 { 77 printf 'scale=20\n' 78 printf 'dst = %s - %s\n' "$1" "$2" 79 printf 'if(dst <0) dst = -dst\n' 80 printf 'dst < 4*%s\n' "$3" 81 } | bc -l 82 } 83 84 # Display the "array" mean_radius with one entry per line 85 listmu() 86 { 87 printf '%s\n' "${mean_radius}" \ 88 | sed 's/[[:space:]]/\n/g' \ 89 | sed '/^[[:space:]]\{0,\}$/d' 90 } 91 92 ######################################################################## 93 # The test 94 ######################################################################## 95 96 # Configure signal processing 97 trap 'die $?' EXIT 98 99 tmpdir="$(mktemp -d "${TMPDIR:-/tmp}/schiff_XXXXXX")" 100 cd "${tmpdir}" 101 102 if [ ! -e "${tmppipe}" ]; then mkfifo "${tmppipe}"; fi 103 104 if [ ! -f "${tmpfile}" ]; then 105 listmu | while read -r mu; do 106 # Generate the geometry distribution into the pipe 107 { 108 printf 'sphere:\n' 109 printf ' radius: { lognormal: { mu: %f, sigma: %f } }\n' \ 110 "${mu}" "${sigma}" 111 } > "${tmppipe}" & 112 113 # invoke the schiff command 114 printf "%f %f %f %f\n" "${wavelength}" "${N}" "${K}" "${Ne}" | \ 115 schiff -q \ 116 -a "${nangles}" \ 117 -A "${ninvcum}" \ 118 -l "${mu}" \ 119 -i "${tmppipe}" \ 120 -w "${wavelength}" \ 121 -g "${nrealisations}" \ 122 -d "${ndirs}" >> "${tmpfile}" 123 done 124 fi 125 126 err=0 # error code 127 128 # Check the estimated cross sections against the references 129 nmu="$(listmu | wcl)" 130 i=0 131 while [ "${i}" -lt "${nmu}" ]; do 132 iline=$((i*(2*nangles+ninvcum+7)+1)) 133 134 xsec="$(sed -n "${iline}p" "${tmpfile}" \ 135 | cut -d' ' -f 2- \ 136 | sed 's/[eE]+\{0,1\}/*10^/g')" 137 xsec0="$(printf '%s\n' "${xsec}" | cut -d' ' -f1)" 138 xsec1="$(printf '%s\n' "${xsec}" | cut -d' ' -f2)" 139 xsec2="$(printf '%s\n' "${xsec}" | cut -d' ' -f3)" 140 xsec3="$(printf '%s\n' "${xsec}" | cut -d' ' -f4)" 141 xsec4="$(printf '%s\n' "${xsec}" | cut -d' ' -f5)" 142 xsec5="$(printf '%s\n' "${xsec}" | cut -d' ' -f6)" 143 144 ref="$(printf '%s\n' "${references}" \ 145 | sed -n "$((i+1))p" \ 146 | sed -e 's/^[[:space:]]\{1,\}//g' -e 's/[eE]+\{0,1\}/*10^/g')" 147 ref0="$(printf '%s\n' "${ref}" | cut -d' ' -f1)" 148 ref1="$(printf '%s\n' "${ref}" | cut -d' ' -f2)" 149 ref2="$(printf '%s\n' "${ref}" | cut -d' ' -f3)" 150 151 extinction="$(eq_eps "${ref0}" "${xsec0}" "${xsec1}")" 152 absorption="$(eq_eps "${ref1}" "${xsec2}" "${xsec3}")" 153 scattering="$(eq_eps "${ref2}" "${xsec4}" "${xsec5}")" 154 155 cmu="$(listmu | sed -n "$((i+1))p")" 156 if [ "${extinction}" -eq 0 ] \ 157 || [ "${absorption}" -eq 0 ] \ 158 || [ "${scattering}" -eq 0 ]; then 159 printf '%e FAILURE\n' "${cmu}" 160 err=1 # notify the error 161 else 162 printf '%e OK\n' "${cmu}" 163 fi 164 i="$((i+1))" 165 done 166 167 if [ "${i}" -ne "${nmu}" ]; then 168 err=1 # notify the error 169 fi 170 171 die "${err}"