#!/bin/bash

ni=96
nc=1
force=10000
# generates image_id (block_id) time U_0 U_1 U-2 ...
# For single-copy images, time used for blocking: t=500 to t=100000 belong to block 0 and so on (alter "int(($1-500)/100000)" below to change the blocking)
cat ../../centers.tcl ../0-colvars/cvs.txt | \
awk -v n=$ni -v k=$force '
function acos(o) {
    if (o>=1) {
	return 0
    } else if (o<=-1) {
	return 4*atan2(0.5,0.5)
    } else {
	return atan2(-o,sqrt(1-o*o))+2*atan2(0.5,0.5)
    }
}{
    if (NR<=n) {
	split($0,C,"(")
	for (j=1;j<=12;j++) {
	    split(C[j+2],D)
	    for (l=1;l<=4;l++) q[(NR-1),j,l]=D[2*l-1]
	}
    } else {
        printf "%d %d %d",$3,int(($1-500)/100000),$1
#        printf "%d %d",$3,$1
	for (i=0;i<=n-1;i++) {
	    u=0
	    for (j=1;j<=12;j++) {
		co=0
		for (l=1;l<=4;l++) co+=q[i,j,l]*$(4*j+l-1)
		o=acos(co)
		u+=0.5*k*o*o
	    }
	    printf " %f",u
	}
	printf "\n"
    }
}' > pot.txt
