CCL Home Page
Up Directory CCL dipol.awk
#!/usr/bin/nawk -f

BEGIN	{
	number = "^[-+]?([0-9]+[.]?[0-9]*|[.][0-9]+)" \
		 "([eE][-+]?[0-9]+)?$"
	first = 1;
	natoms = 0;
	ANG_TO_DEBYE = 4.802813198;
}

!isdata($1,$2,$3,$4,$5)	{
	if (!first) exit;
	first = 0;
	next;
}

{
	natoms++;
	an[natoms] = $1;
	x[natoms]  = $2;
	y[natoms]  = $3;
	z[natoms]  = $4;
	charge[natoms] = $5;
}

END	{
	if (natoms == 0) {
		printf("No atoms found.\n");
		exit;
	}

	# calculate net charge and dipole moment
	netcharge = 0.0;
	dipx = dipy = dipz = 0.0;
	for(i=1;i<=natoms;i++) {
		dipx += charge[i] * (x[i] - x[1]);
		dipy += charge[i] * (y[i] - y[1]);
		dipz += charge[i] * (z[i] - z[1]);
		netcharge += charge[i];
	}
	dipole = sqrt(dipx*dipx + dipy*dipy + dipz*dipz);

	# converts to debyes when distances are in angstroms
	dipole *= ANG_TO_DEBYE;

	printf("Net charge    = % 10.6f\n", netcharge);
	printf("Dipole moment = % 10.6f Debye\n", dipole);
}

function isdata (s1, s2, s3, s4, s5) {
	if (match(s1, number) &&
	    match(s2, number) &&
	    match(s3, number) &&
	    match(s4, number) &&
	    match(s5, number)) return 1;
	else return 0;
}
Modified: Fri Mar 3 17:00:00 1995 GMT
Page accessed 10271 times since Sat Apr 17 21:24:42 1999 GMT