# Gromacs Configuration and Expression Files
# Created:	 23/02/2011
# Last modified: 23/02/2011
# ChangeLog:
#	23/02/2011: Initial version.

filter(type="exportexpression",name="Aten Forcefield Format", extension="ff", glob="*.ff",nickname="ff")
{
	# Variable declaration
        pattern p;
	atom i;
	ffbound b;
	ffatom fi;
	double escale = 1.0, vscale = 1.0;
	int uselj = TRUE, n, nconstraints, nfailed[3] = 0, nub, hasub;
	string forms, currentform;
	
	# Get current model
	model m = aten.frame;

	# Write header
	writelinef("name \"%s\"\n", m.name());
	writelinef("units %s\n\n", aten.prefs.energyunit);

	# Write types information
	writeline("types");
	for (fi in m.fftypes) writelinef("%5i\t%s\t%s\t\"%s\"\t\"%s\"\n", fi.id, fi.name, aten.elements[fi.z].symbol, fi.neta, fi.description);
	writeline("end");

	# Write short-range information
	forms = "";
	while (1)
	{
		# Search for next functional form type used
		currentform = "";
		for (fi in m.fftypes) if (!contains(forms, "___"+fi.form+"___")) { currentform = fi.form; break; }
		if (currentform == "") break;
		
		# Write out forms of this type
		writelinef("\ninter %s\n", currentform);
		for (fi in m.fftypes)
		{
			if (fi.form != currentform) continue;
			writelinef("%5i\t%s\t%12.6f", fi.id, fi.name, fi.charge);
			for (n=1; n<=fi.nparams; ++n) writelinef(" %12.6f", fi.data[n]);
			writelinef("\n");
		}
		writeline("end");

		# Add form to list
		forms += " ___"+currentform+"___ ";
	}

	# Write bond information
	forms = "";
	while (1)
	{
		# Search for next functional form type used
		currentform = "";
		for (b in m.ffbonds) if (!contains(forms, "___"+b.form+"___")) { currentform = b.form; break; }
		if (currentform == "") break;
		
		# Write out forms of this type
		writelinef("\nbonds %s\n", currentform);
		for (b in m.ffbonds)
		{
			if (b.form != currentform) continue;
			writelinef("%s\t%s\t", b.typenames[1], b.typenames[2]);
			for (n=1; n<=b.nparams; ++n) writelinef(" %12.6f", b.data[n]);
			writelinef("\n");
		}
		writeline("end");

		# Add form to list
		forms += " ___"+currentform+"___ ";
	}

	# Write angle information
	forms = "";
	while (1)
	{
		# Search for next functional form type used
		currentform = "";
		for (b in m.ffangles) if (!contains(forms, "___"+b.form+"___")) { currentform = b.form; break; }
		if (currentform == "") break;
		
		# Write out forms of this type
		writelinef("\nangles %s\n", currentform);
		for (b in m.ffangles)
		{
			if (b.form != currentform) continue;
			writelinef("%s\t%s\t%s\t", b.typenames[1], b.typenames[2], b.typenames[3]);
			for (n=1; n<=b.nparams; ++n) writelinef(" %12.6f", b.data[n]);
			writelinef("\n");
		}
		writeline("end");

		# Add form to list
		forms += " ___"+currentform+"___ ";
	}

	# Write torsion information
	forms = "";
	while (1)
	{
		# Search for next functional form type used
		currentform = "";
		for (b in m.fftorsions) if (!contains(forms, "___"+b.form+"___")) { currentform = b.form; break; }
		if (currentform == "") break;
		
		# Write out forms of this type
		writelinef("\ntorsions %s\n", currentform);
		for (b in m.fftorsions)
		{
			if (b.form != currentform) continue;
			writelinef("%s\t%s\t%s\t%s\t", b.typenames[1], b.typenames[2], b.typenames[3], b.typenames[4]);
			for (n=1; n<=b.nparams; ++n) writelinef(" %12.6f", b.data[n]);
			writelinef("\n");
		}
		writeline("end");

		# Add form to list
		forms += " ___"+currentform+"___ ";
	}
}

