我想研究各種配置下兩種苯分子之間的範德華相互作用。要做到這一點,我已經寫了一個bash腳本來從包含那些2的位置的文件中旋轉兩個分子中的一個(現在只在x軸附近)。代碼似乎運行時沒有錯誤,但給出了完全錯誤的結果。我檢查過很多次,但沒有發現任何「數學」錯誤,所以也許我在某處出現了語法錯誤? 我是bash(和linux)的初學者,所以任何幫助將不勝感激:)! 下面是代碼:在bash腳本中編寫旋轉
#!/bin/bash
echo "Angle of rotation (in rad:"
read angle
echo "File or path to file :"
read fichier
echo "Your file:"
cat $fichier
echo "Give the line numbers which contain the atoms you want to rotate :"
read ligneDebut
read ligneFin
# creation of an associative array playing the role of the rotation matrix
declare -A Rx
Rx=([1,1]=1 [1,2]=0 [1,3]=0 [2,1]=0 [2,2]=`echo "c($angle)" | bc -l` [2,3]=`echo "-s($angle)" | bc -l` [3,1]=0 [3,2]=`echo "s($angle)" | bc -l` [3,3]=`echo "c($angle)" | bc -l`)
#storage of the atomic positions in an array
declare -A posAtomes
for((i=$ligneDebut; i<=$ligneFin; i++)) do
for((j=1; j<4; j++)) do
posAtomes[$i,$j]=`awk -v ligne=$i -v colonne=$j '{if(NR==ligne) print $(colonne+1)}' $fichier`
done
done
#computation of the new positions after rotation
declare -A newPos
for((i=$ligneDebut; i<=$ligneFin; i++)) do
for((j=1; j<4; j++)) do
newPos[$i,$j]=`echo "${posAtomes[$i,1]} * ${Rx[$j,1]} + ${posAtomes[$i,2]} * ${Rx[$j,2]} + ${posAtomes[$i,3]} * ${Rx[$j,3]}" | bc -l`
done
done
#writing the new positions in the original file
for ((i=$ligneDebut; i<=$ligneFin; i++)) do
for ((j=1;j<4;j++)) do
awk -v ligne=$i -v colonne=$j -v val=${newPos[$i,$j]} '{if(NR==ligne) {$(colonne+1)=val; print $0} else print $0}' $fichier > tmp && mv tmp $fichier
done
done
位置文件看起來像這樣(這裏只有一個分子):
C 0.00000000000000000E+00 1.39600002765655495E+00 12.25
C 1.20899999141693093E+00 6.98000013828277699E-01 12.25
C 1.20899999141693093E+00 -6.98000013828277699E-01 12.25
C 0.00000000000000000E+00 -1.39600002765655495E+00 12.25
C -1.20899999141693093E+00 -6.98000013828277699E-01 12.25
C -1.20899999141693093E+00 6.98000013828277699E-01 12.25
H 0.00000000000000000E+00 2.47900009155273393E+00 12.25
H 2.14700007438659712E+00 1.24000000953674294E+00 12.25
H 2.14700007438659712E+00 -1.24000000953674294E+00 12.25
H 0.00000000000000000E+00 -2.47900009155273393E+00 12.25
H -2.14700007438659712E+00 -1.24000000953674294E+00 12.25
H -2.14700007438659712E+00 1.24000000953674294E+00 12.25
如果你知道我做錯了什麼?在此先感謝!
既然你說你是'bash'的初學者,我會藉此機會建議你爲這類任務使用另一種語言。 'bash'有很多優點,但是實現算術運算的難易並不是其中之一。如果你正在尋找每臺Linux機器上可用的通用語言,我認爲'python'會是一個不錯的選擇。更專業化的語言將包括Mathematica和Matlab/Octave。 – Aaron