AtomEnvironment AtomEnvironmentFromXmlElement(XElement environmentEl, Dictionary <string, AtomState> atomStateDict) { AtomEnvironment atomEnvironment = new AtomEnvironment(); atomEnvironment.name = environmentEl.Attribute("name").Value; atomEnvironment.amber = environmentEl.Element("amber").Value; atomEnvironment.radius = float.Parse(environmentEl.Element("radius").Value); float r = float.Parse(environmentEl.Element("red").Value); float g = float.Parse(environmentEl.Element("green").Value); float b = float.Parse(environmentEl.Element("blue").Value); atomEnvironment.color = new Color(r, g, b); string stateName = environmentEl.Element("state").Value; if (!atomStateDict.ContainsKey(stateName)) { string[] keyArr = new string[atomStateDict.Keys.Count]; atomStateDict.Keys.CopyTo(keyArr, 0); Debug.LogErrorFormat( "State {0} not available for Atom Environment {1}. Available states for element {2}: {3}", stateName, atomEnvironment.name, environmentEl.Parent.Attribute("element").Value, string.Join(", ", keyArr) ); } else { atomEnvironment.atomState = atomStateDict [stateName]; } foreach (XElement conditionEl in environmentEl.Elements("condition")) { atomEnvironment.environmentConditions.Add(EnvironmentConditionFromXmlElement(conditionEl, atomStateDict)); } return(atomEnvironment); }
public void GetEnvironmentFromXML() { elementEnvironments = new Dictionary <string, ElementEnvironment> (); XDocument envX = FileIO.ReadXML("Assets/Resources/Data/Elements.xml"); XElement atomsX = envX.Element("elements"); foreach (XElement atomEl in atomsX.Elements("atom")) { ElementEnvironment elementEnvironment = new ElementEnvironment(); elementEnvironment.element = atomEl.Attribute("element").Value; elementEnvironment.radius = float.Parse(atomEl.Element("radius").Value); elementEnvironment.maxValency = double.Parse(atomEl.Element("maxValency").Value); elementEnvironment.mass = float.Parse(atomEl.Element("mass").Value); float r = float.Parse(atomEl.Element("red").Value); float g = float.Parse(atomEl.Element("green").Value); float b = float.Parse(atomEl.Element("blue").Value); elementEnvironment.color = new Color(r, g, b); foreach (XElement stateEl in atomEl.Elements("state")) { elementEnvironment.atomStateDict[stateEl.Attribute("name").Value] = AtomStateFromXmlElement(stateEl); } foreach (XElement environmentEl in atomEl.Elements("environment")) { AtomEnvironment atomEnvironment = AtomEnvironmentFromXmlElement(environmentEl, elementEnvironment.atomStateDict); elementEnvironment.environmentDict[atomEnvironment.name] = atomEnvironment; } elementEnvironments [elementEnvironment.element] = elementEnvironment; } }
//Environment public IEnumerator CalculateEnvironment(bool overwrite = true, int maxIterations = 4) { _busy++; int selectionNum; int atomNum; int environmentCalculatedCount = 0; Atom atom; selection = SelectIf(); if (!overwrite) { for (atomNum = 0; atomNum < size; atomNum++) { if (atoms[atomNum].amberName != "") { selection.Remove(atomNum); } } } yield return(new WaitForSeconds(0.1f)); for (int iteration = 0; iteration < maxIterations; iteration++) { if (environmentCalculatedCount == size) { break; } for (selectionNum = selection.Count - 1; selectionNum >= 0; selectionNum--) { atomNum = selection[selectionNum]; atom = atoms[atomNum]; string element = atom.element; if (!amberEnvironment.elementEnvironments.ContainsKey(element)) { Debug.LogErrorFormat("Element {0} not included in data file", element); selection.RemoveAt(atomNum); continue; } ElementEnvironment elementEnvironment = amberEnvironment.elementEnvironments [element]; atom.mass = elementEnvironment.mass; bool conditionMet = false; foreach (string key in elementEnvironment.environmentDict.Keys) { AtomEnvironment atomEnvironment = elementEnvironment.environmentDict [key]; foreach (EnvironmentCondition environmentCondition in atomEnvironment.environmentConditions) { //Meets condition if (environmentCondition.EvaluateConditions(atoms, atomNum) == 0) { atom.amberName = atomEnvironment.amber; atom.atomState = atomEnvironment.atomState; atom.vdwRadius = atomEnvironment.radius; atom.color = atomEnvironment.color; selection.RemoveAt(atomNum); conditionMet = true; break; } } if (conditionMet) { break; } } } yield return(new WaitForSeconds(0.1f)); } _busy--; }
//Environment public IEnumerator CalculateEnvironment(bool overwrite = true, int maxIterations = 4) { _busy++; int atomNum; int environmentCalculatedCount = 0; for (atomNum = 0; atomNum < size; atomNum++) { selection [atomNum] = 1.0; } if (!overwrite) { for (atomNum = 0; atomNum < size; atomNum++) { if (atoms[atomNum].amberName != "") { selection [atomNum] = 0.0; } } } yield return(new WaitForSeconds(0.1f)); for (int iteration = 0; iteration < maxIterations; iteration++) { if (environmentCalculatedCount == size) { break; } for (atomNum = 0; atomNum < size; atomNum++) { if (selection [atomNum] == 0.0) { continue; } string element = atoms [atomNum].element; if (!amberEnvironment.elementEnvironments.ContainsKey(element)) { Debug.LogErrorFormat("Element {0} not included in data file", element); selection [atomNum] = 0.0; continue; } ElementEnvironment elementEnvironment = amberEnvironment.elementEnvironments [element]; foreach (string key in elementEnvironment.environmentDict.Keys) { AtomEnvironment atomEnvironment = elementEnvironment.environmentDict [key]; foreach (EnvironmentCondition environmentCondition in atomEnvironment.environmentConditions) { //Meets condition if (environmentCondition.EvaluateConditions(atoms, atomNum) == 0) { atoms [atomNum].amberName = atomEnvironment.amber; atoms [atomNum].atomState = atomEnvironment.atomState; atoms [atomNum].vdwRadius = atomEnvironment.radius; atoms [atomNum].color = atomEnvironment.color; selection [atomNum] = 0.0; goto CONDITIONMET; } } } CONDITIONMET :; } yield return(new WaitForSeconds(0.1f)); } _busy--; }