# Coordinate (ATO) file for Empirical Potential Structure Refinement (EPSR) software ( (C) Rutherford Appleton Labs )
# Created:	 23/03/2011
# Last modified: 28/05/2011
# ChangeLog:
# 	28/05/2011 : Added writing of random numbers on export. Fixed import to recognise end of LJ specifications.
# 	23/03/2011 : Initial version.

filter(type="importmodel",name="EPSR ATO File", nickname="ato", extension="ato", glob="*.ato", id=18)
{
	// Variables
	atom i;
	int nmols,m,n,l,natoms,nres,nrot,count,done,id,nrotatoms;
	element el;
	double boxsize, temp, eps, sigma, mass, q;
	string atomname, elname, line, dummy;
	vector com, r, phi;
	ffatom ffa;

	addreadoption("skipblanks");

	// File header:
	// 1 : nmols, box length, temperature
	if (!readline(nmols, boxsize, temp)) error("Failed to read header information from file.\n");
	newmodel("ATO file");
	cell(boxsize,boxsize,boxsize,90,90,90);

	// 2 : step sizes etc. (ignore)
	skipline();

	// Molecule/atom specifications are in the form:
	// n  : natoms, comx, comy, comz, phix, phiy, phiz
	// n+1: atom name 1
	// n+2: x,y,z (offsets from com)
	// n+3: nrestraints, res1, res2... (number of distance restraints, 5 per line)
	// n+4: ...resN-1, resN
	// n+5: nrot (number of defined molecular rotations)
	// n+6: atom1, atom2 (bonds of rotation 'axis')
	// n+7: list of headgroup atoms that are rotated
	count = 0;
	for (m=0; m<nmols; m++)
	{
		if (!readline(natoms, com.x, com.y, com.z, phi.x, phi.y, phi.z)) error("Error reading information for molecule %i.\n", m+1);

		for (n=0; n<natoms; n++)
		{
			// Basic atom information
			if (!readline(atomname)) error("Error reading atom name %i for molecule %i\n", n+1, m+1);
			if (!readline(r.x, r.y, r.z)) error("Error reading atom coordinates %i for molecule %i\n", n+1, m+1);
			// Adjust for molecule centre of mass, and also apply a half-cell translation since EPSR uses -0.5 -> 0.5 coordinates
			r += com;
			r += boxsize/2.0;
			// Create a new atom with element 0 - it will be set to a proper element later on
			i = newatom(0, r.x, r.y, r.z);
			i.data = atomname;

			// Apply restraint information as bonds
			if (!nextarg(nres)) error("Error reading nrestraints for atom %i of molecule %i.\n", n+1, m+1);
			for (l=0; l<nres; ++l)
			{
				if (!nextarg(id) || !nextarg(temp)) error("Erro reading restraint information %i for atom %i of molecule %i.\n", l+1, n+1, m+1);
				// If the relative atom id is greater than the current atom number, ignore it (it will be repeated the other way around for the second atom)
				if (id > (n+1)) continue;
				newbond(count+n+1, count+id);
			}
		}

		// Discard molecular rotations
		// There are 14 atoms per line - first line contains number of atoms followed by (up to) 13 indices
		if (!readline(nrot)) error("Error reading nrotations for atom %i of molecule %i.\n", n+1, m+1);
		for (n=0; n<nrot; ++n)
		{
			// Skip ROT and axis lines
			skipline(2);
			// Read in number of atoms and find next number of lines to discard
			readline(nrotatoms);
			skipline(nrotatoms/14);
		}

		count += natoms;
	}

	// Atomtype specifications follow
	// Read in until we find don't find an element symbol (i.e. a number).  
	done = FALSE;
	forcefield ff = newff("Atomtypes from ATO file");
	count = 0;
	do
	{
		++count;
		// Two lines per atomtype
		//   name, symbol, 0/isomass
		//   epsilon  sigma  mass  charge
		if (!readline(atomname, elname, dummy)) error("Error reading atomtype information %i.\n", count);
		// If 'dummy' is empty, chances are we've gone on to the fmole numbers (which has no third data)
		if (dummy == "") break;
		if (!readline(eps, sigma, mass, q)) error("Error reading atomtype LJ information %i.\n", count);
		el = aten.findelement(elname);
		if (el == 0) break;
		printf("       : Mapping atom name %s to %s.\n", atomname, el.symbol);
		ffa = ff.addtype(count, atomname, atomname, el, "", "");
		for (i in aten.model.atoms) if (i.data == atomname) { i.element = el; i.type = ffa; }
		ff.addinter("lj", count, q, eps, sigma);
	} while (!done);
	ff.finalise();

	finalisemodel();
}

filter(type="exportmodel",name="EPSR ATO File", nickname="ato", extension="ato", glob="*.ato", id=18)
{
	# Variable declaration
	model srcmodel= aten.frame;
	pattern p;
	int nmols, n, m, o, mol, nresb, nresa, nrest, nrot, idj, count;
	double rij, mass, boxsize;
	vector com, r;
	atom i, j;
	bound b;
	bond bnd;

	# Options
	double temperature = option("Temperature", "doublespin", 0.0, 100000.0, 10.0, 300.0);
	double epsr_tol = option("Tolerance", "doublespin", 0.0, 1.0, 0.1, 0.0, "newline");
	double epsr_itrans = option("Intramolecular Translations", "doublespin", 0.0, 100.0, 1.0, 1.5, "newline", "group=Step Sizes", "span=2");
	double epsr_hrot = option("Headgroup Rotations", "doublespin", 0.0, 10.0, 0.1, 2.5, "newline", "group=Step Sizes", "span=2");
	double epsr_mrot = option("Whole Molecule Rotations", "doublespin", 0.0, 10.0, 0.1, 1.0, "newline", "group=Step Sizes", "span=2");
	double epsr_mtrans = option("Whole Molecule Translations", "doublespin", 0.0, 10.0, 0.01, 0.1, "newline", "group=Step Sizes", "span=2");
	double epsr_vtemp = option("Vibrational Temperature", "doublespin", 0.0, 1500.0, 10.0, 65.0, "newline", "span=2");
	int geomres = option("Restrain Based on Connectivity", "radio", "restypegroup", 1, "newline", "span=2", "group=Restraints");
	int distres = option("Restrain Based on Distance", "radio", "restypegroup", 0, "span=2", "group=Restraints");
	int geomresbonds = option("Across Bonds", "check", 1, "newline", "span=2", "group=Restraints");
	double distresmax = option("Maximum Distance", "doublespin", 0.0, 10.0, 0.1, 2.4, "group=Restraints");
	int geomresangles = option("Across Angles", "check", 0, "newline", "span=2", "group=Restraints");
	int geomrestorsions = option("Across Torsions", "check", 0, "newline", "span=2", "group=Restraints");
	double geomresmax = option("Maximum Distance", "doublespin", 0.0, 10.0, 0.1, 2.4, "newline", "group=Restraints");

	// First, some checks. We need valid patterns and a cubic cell.
	if (!createexpression(FALSE,TRUE)) error("Error: Can't write ATO file without valid forcefield types assigned to all atoms.\n");

	// Line 1 : nmols, box dimension, temperature
	// First determine total number of molecules
	nmols = 0;      
	for (p in srcmodel.patterns) nmols += p.nmols;
	if (srcmodel.cell.type == "cubic")
	{
		boxsize = srcmodel.cell.a;
		writelinef("  %4i %13.6e  %13.6e\n",nmols,srcmodel.cell.a,temperature);
	}
	else
	{
		// Set boxsize here to be zero (it is used elsewhere to shift by half-cell amounts, so this will prevent it)
		boxsize = 0.0;
		// Determine maximum extent of system
		printf("Model has no cell definition - determining molecular extent...\n");
		double maxrij = 0.0;
		for (atom i = srcmodel.atoms; i; ++i)
		{
			for (atom j = ++i; j; ++j)
			{
				rij = geometry(i,j);
				if (rij > maxrij) maxrij = rij;
			}
		}
		writelinef("  %4i %13.6e  %13.6e\n",nmols,maxrij*2.0,temperature);
	}

	// Line 2 : Tol, step sizes (intra trans, headgroup rot, mol rot, mol trans), vibrational temp
	writelinef("  %10.3e  %10.3e  %10.3e  %10.3e  %10.3e  %10.3e\n", epsr_tol, epsr_itrans, epsr_hrot, epsr_mrot, epsr_mtrans, epsr_vtemp);

	// Atom/molecule loop
	mol = 0;
	i = srcmodel.atoms;
	for (p in srcmodel.patterns)
	{
		for (m=1; m<=p.nmols; ++m)
		{
			++mol;
			// Write centre of mass
			com = p.com(m) - boxsize*0.5;
			writelinef("   %-2i %13.6e %13.6e %13.6e %13.6e %13.6e %13.6e %5i\n",p.nmolatoms,com.x,com.y,com.z,0.0,0.0,0.0,mol);

			// Write atom information
			for (n=1; n<=p.nmolatoms; ++n)
			{
				if (i.type == 0) writelinef(" %-3s     %5i\n", i.symbol, n);
				else writelinef(" %-3s     %5i\n", i.type.name, n);

				// Get mim vector to COM
				r = srcmodel.cell.mimd(i.r - boxsize*0.5, com);
				writelinef(" %12.5e %12.5e %12.5e\n", r.x, r.y, r.z);

				// Restraint information
				count = 0;
				if (geomres)
				{
					nresb = 0;
					nresa = 0;
					nrest = 0;
					if (geomresbonds) for (b in p.bonds) if (((b.id[1] == n) || (b.id[2] == n)) && (geometry(p.atoms[b.id[1]],p.atoms[b.id[2]]) < geomresmax)) ++nresb;
					if (geomresangles) for (b in p.angles) if (((b.id[1] == n) || (b.id[3] == n)) && (geometry(p.atoms[b.id[1]],p.atoms[b.id[3]]) < geomresmax)) ++nresa;
					if (geomrestorsions) for (b in p.torsions) if (((b.id[1] == n) || (b.id[4] == n)) && (geometry(p.atoms[b.id[1]],p.atoms[b.id[4]]) < geomresmax)) ++nrest;
					writelinef("  %2i", nresa+nresb+nrest);
					if (nresb > 0) for (b in p.bonds)
					{
						// Search for bonds in which this atom is involved
						if (b.id[1] == n) idj = b.id[2];
						else if (b.id[2] == n) idj = b.id[1];
						else continue;
						rij = geometry(p.atoms[b.id[1]],p.atoms[b.id[2]]);
						if (rij > geomresmax) continue;
						if (count && (count%5 == 0)) writelinef("\n");
						++count;
						writelinef("%4i %9.3e ", idj, rij);
					}
					if (nresa > 0) for (b in p.angles)
					{
						// Search for angles in which this atom is involved
						if (b.id[1] == n) idj = b.id[3];
						else if (b.id[3] == n) idj = b.id[1];
						else continue;
						rij = geometry(p.atoms[b.id[1]],p.atoms[b.id[3]]);
						if (rij > geomresmax) continue;
						if (count && (count%5 == 0)) writelinef("\n");
						++count;
						writelinef("%4i %9.3e ", idj, rij);
					}
					if (nrest > 0) for (b in p.torsions)
					{
						// Search for torsions in which this atom is involved
						if (b.id[1] == n) idj = b.id[4];
						else if (b.id[4] == n) idj = b.id[1];
						else continue;
						rij = geometry(p.atoms[b.id[1]],p.atoms[b.id[4]]);
						if (rij > geomresmax) continue;
						if (count && (count%5 == 0)) writelinef("\n");
						++count;
						writelinef("%4i %9.3e ", idj, rij);
					}
					writelinef("\n");
				}
				else
				{
					nresb = 0;
					for (idj = 1; idj<=p.nmolatoms; ++idj) if ((n != idj) && (geometry(p.atoms[n],p.atoms[idj]) < distresmax)) ++nresb;
					writelinef(" %4i", nresb);
					if (nresb > 0) for (idj = 1; idj<=p.nmolatoms; ++idj)
					{
						if (n == idj) continue;
						rij = geometry(p.atoms[n],p.atoms[idj]);
						if (rij < distresmax) writelinef("%4i %9.3e ", idj, rij);
					}
					writelinef("\n");
				}

				++i;
			}

			// Write headgroup rotations
			nrot = 0;
			for (b in p.bonds)
			{
				if (p.atomsinring(b.id[1],b.id[2])) continue;
				if (p.atoms[b.id[1]].nbonds == 1) continue;
				if (p.atoms[b.id[2]].nbonds == 1) continue;
				++nrot;
			}
			writelinef(" %5i\n", nrot);
			if (nrot > 0)
			{
				for (b in p.bonds)
				{
					if (p.atomsinring(b.id[1],b.id[2])) continue;
					if (p.atoms[b.id[1]].nbonds == 1) continue;
					if (p.atoms[b.id[2]].nbonds == 1) continue;
					bnd = p.atoms[b.id[1]].findbond(p.atoms[b.id[2]]);
					writeline(" ROT");
					writelinef("%5i%5i\n", b.id[1], b.id[2]);
					srcmodel.selectnone();
					srcmodel.selecttree(p.atoms[b.id[1]], bnd);
					// Check here the number of selected atoms - if greater than half the atoms in the molecule then we're better off with the inverse selection!
					if (srcmodel.nselected > 0.5*p.nmolatoms)
					{
						for (o=1; o<=p.nmolatoms; ++o)
						{
							if (p.atoms[o].selected) p.atoms[o].selected = FALSE;
							else p.atoms[o].selected = TRUE;
						}
					}
					srcmodel.deselect(p.atoms[b.id[1]], p.atoms[b.id[2]]);
					// List of rotating atoms should be given 14 per line (13 + natoms on the first line)
					count = 1;
					writelinef(" %4i", srcmodel.nselected);
					for (j in srcmodel.selection)
					{
						if (count%14 == 0) writelinef("\n");
						writelinef(" %4i", j.id-p.firstatomid+1);
						++count;
					}
					writelinef("\n");
				}
			}
		}
	}

	// Write the forcefield info
        // Energy unit must be kj/mol, so set automatic conversion of ff energy parameters to kj
        autoconversionunit("kj");
	for (ffatom ffa in srcmodel.fftypes)
	{
		if ((ffa.form != "lj") && (ffa.form != "ljgeom")) error("Error: Atom type '%s' contains short-range parameters of an incompatible type with EPSR (%s).\n", ffa.name, ffa.form);
		if (ffa.z == 1) writelinef(" %3s %3s    %1i\n", ffa.name, aten.elements[ffa.z].symbol, 1);
		else writelinef(" %3s %3s    %1i\n", ffa.name, aten.elements[ffa.z].symbol, 0);

		// Masses - for H atoms, write deuterium mass instead
		if (ffa.z == 1) mass = 2.0;
		else mass = aten.elements[ffa.z].mass;
		writelinef(" %10.4e  %10.4e  %10.4e  %10.4e  %10.4e\n", ffa.parameter("epsilon"), ffa.parameter("sigma"), mass, ffa.charge, 0.0);
	}
	
	// Extra data

	// Used by fmole to keep non-bonded atoms apart
	writelinef(" %10.4e  %10.4e\n", 1.0, 3.0);

	// Random numbers for restart purposes
	for (n=0; n<15; ++n) writelinef(" %i", randomi());
	writelinef("\n");
	
}

