#!/bin/bash cd "${0%/*}" || exit # Run from this directory . ${WM_PROJECT_DIR:?}/bin/tools/RunFunctions # Tutorial run functions #------------------------------------------------------------------------------ # settings # operand setups setups=" veryStable stable slightlyStable neutral slightlyUnstable unstable " # reference tree height treeHeightRef="20" # reference z height zRef="40" # Benchmark dataset: # Arnqvist, J., Segalini, A., Dellwik, E., & Bergström, H. (2015). # Wind statistics from a forested landscape. # Boundary-Layer Meteorology, 156(1), 53-71. # DOI:10.1007/s10546-015-0016-x #------------------------------------------------------------------------------ # Simple linear interpolation on an ascending table linear_interp() { inputFile="$1" zRef="$2" yi=($( \ awk \ -v zRef="$zRef" \ ' BEGIN \ { x1=0; y1=0; x2=$1; y2=$2; } { if((x1 < zRef) && (x2 < zRef) && (getline != 0)) { x1=x2; y1=y2; x2=$1; y2=$2 } } END \ { yi = (y2-y1)/(x2-x1)*(zRef-x1) + y1; print yi } ' $inputFile )) echo "$yi" } plot_u_vs_z() { echo " # Plots for the ground-normal normalised streamwise flow speed profile" treeHeightRef="$1" zRef="$2" shift 2 setups=$@ benchmarkFile="$FOAM_TUTORIALS/resources/dataset/atm-Arnqvist-2015/uNorm-zNorm.dat" i=0 for setup in $setups do endTime=$(foamDictionary results/$setup/settings/controlDict -entry endTime -value) sampleFile="results/$setup/postProcessing/samples/$endTime/lineZ1_U.xy" # Store the ground-normal height z=($(awk '{ printf "%.16f\n", $1 }' $sampleFile)) # Compute velocity magnitude by excluding the ground-normal component magU=($(awk '{ printf "%.16f\n", sqrt($2*$2 + $3*$3) }' $sampleFile)) file="plots/dats/z_magU_$i.dat" [ ! -f $file ] && for ((j = 0; j < "${#magU[@]}"; j++)) \ do printf "%.16f %.16f\n" "${z[$j]}" "${magU[$j]}" \ >> $file; done # Find velocity magnitude at zRef height by linear interpolation uRef=$(linear_interp "$file" "$zRef") # Find z normalised by the reference tree height zNorm=($(awk -v tRef="$treeHeightRef" ' { zNorm = $1/tRef; printf "%.16f\n", zNorm }' $file)) # Find U normalised by uRef uNorm=($(awk -v uRef="$uRef" ' { uNorm = $2/uRef; printf "%.16f\n", uNorm }' $file)) file0="plots/dats/zNorm_uNorm_$i.dat" [ ! -f $file0 ] && for ((j = 0; j < "${#zNorm[@]}"; j++)) \ do printf "%.16f %.16f\n" "${zNorm[$j]}" "${uNorm[$j]}" \ >> $file0; done i=$(($i+1)) done image="plots/uNorm_vs_zNorm.png" gnuplot<> $file; done # Find k at zRef height by linear interpolation kRef=$(linear_interp "$file" "$zRef") # Find z normalised by the reference tree height zNorm=($(awk -v tRef="$treeHeightRef" ' { zNorm = $1/tRef; printf "%.16f\n", zNorm }' $file)) # Find k normalised by kRef kNorm=($(awk -v kRef="$kRef" ' { kNorm = $2/kRef; printf "%.16f\n", kNorm }' $file)) file0="plots/dats/zNorm_kNorm_$i.dat" [ ! -f $file0 ] && for ((j = 0; j < "${#zNorm[@]}"; j++)) \ do printf "%.16f %.16f\n" "${zNorm[$j]}" "${kNorm[$j]}" \ >> $file0; done i=$(($i+1)) done image="plots/kNorm_vs_zNorm.png" gnuplot<> $file; done # Find the Obukhov length at zRef height by linear interpolation ObukhovLRef=$(linear_interp "$file" "$zRef") echo "$setup = $ObukhovLRef" >> "plots/ObukhovLength.dat" i=$(($i+1)) done } plot_alpha_vs_z() { echo " # Plots for the ground-normal normalised veer profile" treeHeightRef="$1" zRef="$2" shift 2 setups=$@ benchmarkFile="$FOAM_TUTORIALS/resources/dataset/atm-Arnqvist-2015/veer-zNorm.dat" i=0 for setup in $setups do endTime=$(foamDictionary results/$setup/settings/controlDict -entry endTime -value) sampleFile="results/$setup/postProcessing/samples/$endTime/lineZ1_U.xy" # Store the ground-normal height z=($(awk '{ printf "%.16f\n", $1 }' $sampleFile)) # Store streamwise and spanwise velocity components u=($(awk '{ printf "%.16f\n", $2 }' $sampleFile)) v=($(awk '{ printf "%.16f\n", $3 }' $sampleFile)) fileu="plots/dats/z_u_$i.dat" [ ! -f $fileu ] && for ((j = 0; j < "${#z[@]}"; j++)) \ do printf "%.16f %.16f\n" "${z[$j]}" "${u[$j]}" \ >> $fileu; done filev="plots/dats/z_v_$i.dat" [ ! -f $filev ] && for ((j = 0; j < "${#z[@]}"; j++)) \ do printf "%.16f %.16f\n" "${z[$j]}" "${v[$j]}" \ >> $filev; done # Find u and v at zRef height by linear interpolation uRef=$(linear_interp "$fileu" "$zRef") vRef=$(linear_interp "$filev" "$zRef") # Find z normalised by the reference tree height zNorm=($(awk -v tRef="$treeHeightRef" ' { zNorm = $1/tRef; printf "%.16f\n", zNorm }' $fileu)) # Find veer veer=($(awk -v uRef="$uRef" -v vRef="$vRef" ' { x = $2/sqrt($2*$2 + $3*$3); xR = uRef/sqrt(uRef*uRef + vRef*vRef); veer = -1*(atan2(sqrt(1 - x*x), x) - atan2(sqrt(1 - xR*xR), xR))*180/atan2(0, -1); printf "%.16f\n", veer }' $sampleFile)) file0="plots/dats/zNorm_veer_$i.dat" [ ! -f $file0 ] && for ((j = 0; j < "${#zNorm[@]}"; j++)) \ do printf "%.16f %.16f\n" "${zNorm[$j]}" "${veer[$j]}" \ >> $file0; done i=$(($i+1)) done image="plots/alpha_vs_zNorm.png" gnuplot</dev/null || { echo "gnuplot not found - skipping graph creation" 1>&2 exit 1 } # Requires awk command -v awk >/dev/null || { echo "awk not found - skipping graph creation" 1>&2 exit 1 } # Check "results" directory [ -d "results" ] || { echo "No results directory found - skipping graph creation" 1>&2 exit 1 } #------------------------------------------------------------------------------ dirPlots="plots/dats" [ -d "$dirPlots" ] || rm -rf "plots" && mkdir -p "$dirPlots" plot_u_vs_z "$treeHeightRef" "$zRef" $setups plot_k_vs_z "$treeHeightRef" "$zRef" $setups print_Obukhov_length "$treeHeightRef" "$zRef" $setups plot_alpha_vs_z "$treeHeightRef" "$zRef" $setups #------------------------------------------------------------------------------