package run;
import java.util.*;

import java.io.*;

import run.readers.FembicReader;

import uka.karmi.rmi.RemoteException;

import jp.sync.*;

import jp.lang.*;

public class ModelSmp implements uka.patch.Patchable, uka.transport.Transportable {
   public void createPatch(Object _copy, uka.patch.PatchOutput po) throws java.io.IOException {
      ModelSmp copy = (ModelSmp)_copy;
      if (po.writeDiff(this.nr_of_CPUs, copy.nr_of_CPUs)) copy.nr_of_CPUs = this.nr_of_CPUs;
      if (po.writeDiff(this.autostep, copy.autostep)) copy.autostep = this.autostep;
      if (po.writeDiff(this.failure_is_set, copy.failure_is_set)) copy.failure_is_set = this.failure_is_set;
      if (po.writeDiff(this.timestep, copy.timestep)) copy.timestep = this.timestep;
      if (po.writeDiff(this.number_of_loads, copy.number_of_loads)) copy.number_of_loads = this.number_of_loads;
      if (po.writeDiff(this.number_of_constraints, copy.number_of_constraints)) copy.number_of_constraints = this.number_of_constraints;
      if (po.writeDiff(this.state, copy.state)) copy.state = this.state;
      if (po.writeDiff(this.i, copy.i)) copy.i = this.i;
      if (po.writeDiff(this.ttemp, copy.ttemp)) copy.ttemp = this.ttemp;
      if (po.writeDiff(this.time, copy.time)) copy.time = this.time;
      if (po.writeDiff(this.number_of_controls, copy.number_of_controls)) copy.number_of_controls = this.number_of_controls;
      if (po.writeDiff(this.number_of_materials, copy.number_of_materials)) copy.number_of_materials = this.number_of_materials;
      if (po.writeDiff(this.number_of_nodes, copy.number_of_nodes)) copy.number_of_nodes = this.number_of_nodes;
      if (po.writeDiff(this.number_of_trackers, copy.number_of_trackers)) copy.number_of_trackers = this.number_of_trackers;
      if (po.writeDiff(this.number_of_elements, copy.number_of_elements)) copy.number_of_elements = this.number_of_elements;
      copy.filename = this.filename = (java.lang.String)po.writeDiff(this.filename, copy.filename);
      copy.trackwriter = this.trackwriter = (run.TrackWriter)po.writeDiff(this.trackwriter, copy.trackwriter);
      copy.resultwriter = this.resultwriter = (run.Writer)po.writeDiff(this.resultwriter, copy.resultwriter);
      copy.temporary_node = this.temporary_node = (run.Node)po.writeDiff(this.temporary_node, copy.temporary_node);
      copy.temporary_constraint = this.temporary_constraint = (run.Constraint)po.writeDiff(this.temporary_constraint, copy.temporary_constraint);
      copy.temporary_tracker = this.temporary_tracker = (run.Tracker)po.writeDiff(this.temporary_tracker, copy.temporary_tracker);
      copy.temporary_element = this.temporary_element = (run.Element)po.writeDiff(this.temporary_element, copy.temporary_element);
      copy.controlset = this.controlset = (run.Controlset)po.writeDiff(this.controlset, copy.controlset);
      copy.loadlist = this.loadlist = (run.RplVector)po.writeDiff(this.loadlist, copy.loadlist);
      copy.constraintlist = this.constraintlist = (run.RplVector)po.writeDiff(this.constraintlist, copy.constraintlist);
      copy.materiallist = this.materiallist = (run.RplVector)po.writeDiff(this.materiallist, copy.materiallist);
      copy.trackerlist = this.trackerlist = (run.RplVector)po.writeDiff(this.trackerlist, copy.trackerlist);
      copy.elementlist = this.elementlist = (run.RplVector)po.writeDiff(this.elementlist, copy.elementlist);
      copy.nodelist = this.nodelist = (run.RplVector)po.writeDiff(this.nodelist, copy.nodelist);
   }
   public void applyPatch(Object _copy, uka.patch.PatchInput pi) throws java.io.IOException, ClassNotFoundException {
      ModelSmp copy = (ModelSmp)_copy;
      if (pi.hasDiff()) copy.nr_of_CPUs = this.nr_of_CPUs = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.autostep = this.autostep = pi.getDiffAsBoolean();
      if (pi.hasDiff()) copy.failure_is_set = this.failure_is_set = pi.getDiffAsBoolean();
      if (pi.hasDiff()) copy.timestep = this.timestep = pi.getDiffAsDouble();
      if (pi.hasDiff()) copy.number_of_loads = this.number_of_loads = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.number_of_constraints = this.number_of_constraints = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.state = this.state = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.i = this.i = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.ttemp = this.ttemp = pi.getDiffAsDouble();
      if (pi.hasDiff()) copy.time = this.time = pi.getDiffAsDouble();
      if (pi.hasDiff()) copy.number_of_controls = this.number_of_controls = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.number_of_materials = this.number_of_materials = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.number_of_nodes = this.number_of_nodes = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.number_of_trackers = this.number_of_trackers = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.number_of_elements = this.number_of_elements = pi.getDiffAsInt();
      if (pi.hasDiff()) copy.filename = this.filename = (java.lang.String)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.trackwriter = this.trackwriter = (run.TrackWriter)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.resultwriter = this.resultwriter = (run.Writer)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.temporary_node = this.temporary_node = (run.Node)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.temporary_constraint = this.temporary_constraint = (run.Constraint)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.temporary_tracker = this.temporary_tracker = (run.Tracker)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.temporary_element = this.temporary_element = (run.Element)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.controlset = this.controlset = (run.Controlset)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.loadlist = this.loadlist = (run.RplVector)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.constraintlist = this.constraintlist = (run.RplVector)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.materiallist = this.materiallist = (run.RplVector)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.trackerlist = this.trackerlist = (run.RplVector)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.elementlist = this.elementlist = (run.RplVector)pi.getDiffAsObject();
      if (pi.hasDiff()) copy.nodelist = this.nodelist = (run.RplVector)pi.getDiffAsObject();
   }
   public void descendReferences(uka.patch.ReferenceConsumer c) throws java.io.IOException {
      c.descend(this.filename);
      c.descend(this.trackwriter);
      c.descend(this.resultwriter);
      c.descend(this.temporary_node);
      c.descend(this.temporary_constraint);
      c.descend(this.temporary_tracker);
      c.descend(this.temporary_element);
      c.descend(this.controlset);
      c.descend(this.loadlist);
      c.descend(this.constraintlist);
      c.descend(this.materiallist);
      c.descend(this.trackerlist);
      c.descend(this.elementlist);
      c.descend(this.nodelist);
   }
   public void filterReferences(uka.patch.ReferenceFilter f) {
      this.filename = (java.lang.String)f.filter(this.filename);
      this.trackwriter = (run.TrackWriter)f.filter(this.trackwriter);
      this.resultwriter = (run.Writer)f.filter(this.resultwriter);
      this.temporary_node = (run.Node)f.filter(this.temporary_node);
      this.temporary_constraint = (run.Constraint)f.filter(this.temporary_constraint);
      this.temporary_tracker = (run.Tracker)f.filter(this.temporary_tracker);
      this.temporary_element = (run.Element)f.filter(this.temporary_element);
      this.controlset = (run.Controlset)f.filter(this.controlset);
      this.loadlist = (run.RplVector)f.filter(this.loadlist);
      this.constraintlist = (run.RplVector)f.filter(this.constraintlist);
      this.materiallist = (run.RplVector)f.filter(this.materiallist);
      this.trackerlist = (run.RplVector)f.filter(this.trackerlist);
      this.elementlist = (run.RplVector)f.filter(this.elementlist);
      this.nodelist = (run.RplVector)f.filter(this.nodelist);
   }
   public Object flatClone() {
      try {
         return super.clone();
      }  catch (CloneNotSupportedException ex) {
         throw new AssertionError("Declared Cloneable but clone() is still unsupported");
      }
   }
   public static final uka.transport.TransportDescriptor TRANSPORT_DESCRIPTOR = new uka.transport.TransportDescriptor(){
      public Object unmarshalReference(uka.transport.UnmarshalStream s, int id) throws java.io.IOException, ClassNotFoundException {
         return new ModelSmp(s, id);
      }
      public boolean unmarshal(Object obj, uka.transport.UnmarshalStream s) throws ClassNotFoundException, java.io.IOException {
         ((ModelSmp)obj).unmarshal(s);
         return false;
      }
      public void marshalReference(Object obj, uka.transport.MarshalStream s) throws java.io.IOException {
         ((ModelSmp)obj).marshalReference(s);
      }
      public void marshal(Object obj, uka.transport.MarshalStream s) throws java.io.IOException {
         ((ModelSmp)obj).marshal(s);
      }
      public Object deepClone(Object orig, int id, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
         try {
            return new ModelSmp((ModelSmp)orig, id, _helper);
         }  catch (java.io.IOException ex) {
            throw (CloneNotSupportedException)new CloneNotSupportedException().initCause(ex);
         }
      }
      public boolean deepCloneReferences(Object orig, Object copy, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
         ((ModelSmp)copy).deepCloneReferences((ModelSmp)orig, _helper);
         return false;
      }
      public Class getType() {
         return ModelSmp.class;
      }
   };
   public uka.transport.TransportDescriptor getTransportDescriptor() {
      return TRANSPORT_DESCRIPTOR;
   }
   private static final int _SIZE = uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_boolean + uka.transport.BasicIO.SIZEOF_boolean + uka.transport.BasicIO.SIZEOF_double + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_double + uka.transport.BasicIO.SIZEOF_double + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int + uka.transport.BasicIO.SIZEOF_int;
   public ModelSmp(uka.transport.UnmarshalStream _stream, int _id) throws java.io.IOException, ClassNotFoundException {
      _stream.register(this, _id);
   }
   public void unmarshal(uka.transport.UnmarshalStream _stream) throws java.io.IOException, ClassNotFoundException {
      _stream.request(ModelSmp._SIZE);
      byte[] _buffer = _stream.getBuffer();
      int _pos = _stream.getPosition();
      this.nr_of_CPUs = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.autostep = uka.transport.BasicIO.extractBoolean(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_boolean;
      this.failure_is_set = uka.transport.BasicIO.extractBoolean(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_boolean;
      this.timestep = uka.transport.BasicIO.extractDouble(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_double;
      this.number_of_loads = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.number_of_constraints = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.state = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.i = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.ttemp = uka.transport.BasicIO.extractDouble(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_double;
      this.time = uka.transport.BasicIO.extractDouble(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_double;
      this.number_of_controls = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.number_of_materials = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.number_of_nodes = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.number_of_trackers = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      this.number_of_elements = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      _stream.accept(ModelSmp._SIZE);
      this.filename = (java.lang.String)_stream.readReference();
      this.trackwriter = (run.TrackWriter)_stream.readReference();
      this.resultwriter = (run.Writer)_stream.readReference();
      this.temporary_node = (run.Node)_stream.readReference();
      this.temporary_constraint = (run.Constraint)_stream.readReference();
      this.temporary_tracker = (run.Tracker)_stream.readReference();
      this.temporary_element = (run.Element)_stream.readReference();
      this.controlset = (run.Controlset)_stream.readReference();
      this.loadlist = (run.RplVector)_stream.readReference();
      this.constraintlist = (run.RplVector)_stream.readReference();
      this.materiallist = (run.RplVector)_stream.readReference();
      this.trackerlist = (run.RplVector)_stream.readReference();
      this.elementlist = (run.RplVector)_stream.readReference();
      this.nodelist = (run.RplVector)_stream.readReference();
   }
   public void marshalReference(uka.transport.MarshalStream _stream) throws java.io.IOException {
   }
   public void marshal(uka.transport.MarshalStream _stream) throws java.io.IOException {
      _stream.reserve(ModelSmp._SIZE);
      byte[] _buffer = _stream.getBuffer();
      int _pos = _stream.getPosition();
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.nr_of_CPUs);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.autostep);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.failure_is_set);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.timestep);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.number_of_loads);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.number_of_constraints);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.state);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.i);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.ttemp);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.time);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.number_of_controls);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.number_of_materials);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.number_of_nodes);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.number_of_trackers);
      _pos = uka.transport.BasicIO.insert(_buffer, _pos, this.number_of_elements);
      _stream.deliver(_SIZE);
      _stream.writeReference(this.filename);
      _stream.writeReference(this.trackwriter);
      _stream.writeReference(this.resultwriter);
      _stream.writeReference(this.temporary_node);
      _stream.writeReference(this.temporary_constraint);
      _stream.writeReference(this.temporary_tracker);
      _stream.writeReference(this.temporary_element);
      _stream.writeReference(this.controlset);
      _stream.writeReference(this.loadlist);
      _stream.writeReference(this.constraintlist);
      _stream.writeReference(this.materiallist);
      _stream.writeReference(this.trackerlist);
      _stream.writeReference(this.elementlist);
      _stream.writeReference(this.nodelist);
   }
   public ModelSmp(ModelSmp _orig, int _id, uka.transport.DeepClone _helper) throws CloneNotSupportedException, java.io.IOException {
      _helper.add(_id, this);
      this.nr_of_CPUs = _orig.nr_of_CPUs;
      this.autostep = _orig.autostep;
      this.failure_is_set = _orig.failure_is_set;
      this.timestep = _orig.timestep;
      this.number_of_loads = _orig.number_of_loads;
      this.number_of_constraints = _orig.number_of_constraints;
      this.state = _orig.state;
      this.i = _orig.i;
      this.ttemp = _orig.ttemp;
      this.time = _orig.time;
      this.number_of_controls = _orig.number_of_controls;
      this.number_of_materials = _orig.number_of_materials;
      this.number_of_nodes = _orig.number_of_nodes;
      this.number_of_trackers = _orig.number_of_trackers;
      this.number_of_elements = _orig.number_of_elements;
   }
   public void deepCloneReferences(ModelSmp _orig, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
      this.filename = (java.lang.String)_helper.internalDeepClone(_orig.filename);
      this.trackwriter = (run.TrackWriter)_helper.internalDeepClone(_orig.trackwriter);
      this.resultwriter = (run.Writer)_helper.internalDeepClone(_orig.resultwriter);
      this.temporary_node = (run.Node)_helper.internalDeepClone(_orig.temporary_node);
      this.temporary_constraint = (run.Constraint)_helper.internalDeepClone(_orig.temporary_constraint);
      this.temporary_tracker = (run.Tracker)_helper.internalDeepClone(_orig.temporary_tracker);
      this.temporary_element = (run.Element)_helper.internalDeepClone(_orig.temporary_element);
      this.controlset = (run.Controlset)_helper.internalDeepClone(_orig.controlset);
      this.loadlist = (run.RplVector)_helper.internalDeepClone(_orig.loadlist);
      this.constraintlist = (run.RplVector)_helper.internalDeepClone(_orig.constraintlist);
      this.materiallist = (run.RplVector)_helper.internalDeepClone(_orig.materiallist);
      this.trackerlist = (run.RplVector)_helper.internalDeepClone(_orig.trackerlist);
      this.elementlist = (run.RplVector)_helper.internalDeepClone(_orig.elementlist);
      this.nodelist = (run.RplVector)_helper.internalDeepClone(_orig.nodelist);
   }
   private int number_of_elements;
   private int number_of_trackers;
   private int number_of_nodes;
   private int number_of_materials;
   private int number_of_controls;
   private RplVector nodelist;
   private RplVector elementlist;
   private RplVector trackerlist;
   private RplVector materiallist;
   private volatile double time;
   private volatile double ttemp;
   private int i;
   private int state;
   private RplVector constraintlist;
   private RplVector loadlist;
   private int number_of_constraints;
   private int number_of_loads;
   private Controlset controlset;
   private Element temporary_element;
   private Tracker temporary_tracker;
   private Constraint temporary_constraint;
   private Node temporary_node;
   private Writer resultwriter;
   private TrackWriter trackwriter;
   private java.lang.String filename;
   private double timestep;
   private boolean failure_is_set = false;
   private boolean autostep;
   private int nr_of_CPUs;
   public ModelSmp(int nr_of_CPUs) {
      super();
      this.nr_of_CPUs = nr_of_CPUs;
   }
   public void assembleMassMatrix() throws Error {
      int i;
      Node temp_node;
      Element temp_element;
      Constraint temp_constraint;
      try {
         System.out.println("Assembling Elements");
         for (i = 0; i < this.number_of_elements; i++) {
            temp_element = (Element)this.elementlist.elementAt(i);
            temp_element.assembleMassMatrix();
         }
      }  catch (ArrayIndexOutOfBoundsException e) {
         throw new Error("\n* Error during Assembly phase - Terminating" + e);
      } catch (NullPointerException e) {
         throw new Error("\n* Error during Assembly phase of Element\n* This error could be the result of a missing (or wrong) parameter in the indata file \n* " + e);
      } catch (Error e) {
         throw new Error("\n* Error during Assembly phase of Element\n* This error could be the result of a missing (or wrong) parameter in the indata file \n* " + e);
      }
      try {
         System.out.println("Assembling Nodes");
         for (i = 0; i < this.nodelist.size(); i++) {
            temp_node = (Node)this.nodelist.elementAt(i);
            temp_node.determineMassMatrix();
         }
      }  catch (ArrayIndexOutOfBoundsException e) {
         throw new Error("\n* Error during Assembly phase - Terminating" + e);
      } catch (NullPointerException e) {
         throw new Error("\n* Error during Assembly phase of Node \n* This error could be the result of a missing (or wrong) parameter in the indata file \n* " + e);
      } catch (Error e) {
         throw new Error("\n* Error during Assembly phase of Node \n* This error could be the result of a missing (or wrong) parameter in the indata file \n* " + e);
      }
      try {
         System.out.println("Assembling Constraints");
         for (i = 0; i < this.number_of_constraints; i++) {
            temp_constraint = (Constraint)this.constraintlist.elementAt(i);
            temp_constraint.determineMassMatrix(this.nodelist);
         }
      }  catch (ArrayIndexOutOfBoundsException e) {
         throw new Error("\n* Error during Assembly phase - Terminating" + e);
      } catch (NullPointerException e) {
         throw new Error("\n* Error during Assembly phase of Constraint \n* This error could be the result of a missing (or wrong) parameter in the indata file \n* " + e);
      } catch (Error e) {
         throw new Error("\n* Error during Assembly phase of Constraint \n* This error could be the result of a missing (or wrong) parameter in the indata file \n* " + e);
      }
   }
   public void initialize(String fname) throws Error, RemoteException {
      this.filename = new String(fname);
      Reader indatafile = new FembicReader(this.filename);
      try {
         this.number_of_elements = indatafile.numberOfElements();
         this.number_of_trackers = indatafile.numberOfTrackers();
         this.number_of_materials = indatafile.numberOfMaterials();
         this.number_of_nodes = indatafile.numberOfNodes();
         this.number_of_constraints = indatafile.numberOfConstraints();
         this.number_of_loads = indatafile.numberOfLoads();
         this.number_of_controls = indatafile.numberOfControls();
      }  catch (java.text.ParseException e) {
         throw new Error("Error in initialization phase \n" + e);
      }
      this.elementlist = new RplVector(this.number_of_elements);
      this.trackerlist = new RplVector(this.number_of_trackers);
      this.materiallist = new RplVector(this.number_of_materials);
      this.nodelist = new RplVector(this.number_of_nodes);
      this.constraintlist = new RplVector(this.number_of_constraints);
      this.loadlist = new RplVector(this.number_of_loads);
      this.controlset = new Controlset();
      try {
         System.out.println("Reading Constraints");
         indatafile.open();
         for (this.i = 0; this.i < this.number_of_constraints; this.i++) {
            this.constraintlist.addElement(indatafile.getNextConstraint(this.nodelist));
         }
         indatafile.close();
         System.out.println("Filled constraintlist");
         System.out.println("Reading Loads");
         indatafile.open();
         for (this.i = 0; this.i < this.number_of_loads; this.i++) {
            this.loadlist.addElement(indatafile.getNextLoad(this.nodelist));
         }
         indatafile.close();
         System.out.println("Filled loadlist");
         indatafile.open();
         System.out.println("Reading Nodes");
         for (this.i = 0; this.i < this.number_of_nodes; this.i++) {
            this.nodelist.addElement(indatafile.getNextNode(this.constraintlist, this.loadlist));
         }
         indatafile.close();
         System.out.println("Filled nodelist");
         indatafile.open();
         System.out.println("Reading Materials");
         Material m;
         for (this.i = 0; this.i < this.number_of_materials; this.i++) {
            m = indatafile.getNextMaterial();
            if (m.failure_strain_is_set || m.failure_stress_is_set) {
               this.failure_is_set = true;
            }
            this.materiallist.addElement(m);
         }
         indatafile.close();
         System.out.println("Filled materiallist");
         indatafile.open();
         System.out.println("Reading Elements");
         for (this.i = 0; this.i < this.number_of_elements; this.i++) {
            this.elementlist.addElement(indatafile.getNextElement(this.materiallist, this.nodelist, this.loadlist));
         }
         indatafile.close();
         System.out.println("Filled elementlist");
         indatafile.open();
         System.out.println("Reading Trackers");
         for (this.i = 0; this.i < this.number_of_trackers; this.i++) {
            this.trackerlist.addElement(indatafile.getNextTracker(this.nodelist, this.elementlist));
         }
         indatafile.close();
         System.out.println("Filled trackerlist");
         indatafile.open();
         System.out.println("Reading Controls");
         for (this.i = 0; this.i < this.number_of_controls; this.i++) {
            indatafile.getControlSet(this.controlset);
         }
         indatafile.close();
         System.out.println("Filled controlset");
         this.resultwriter = indatafile.getWriter(this.nodelist, this.elementlist, this.controlset, null);
         this.trackwriter = indatafile.getTrackWriter(this.trackerlist, this.controlset, null);
      }  catch (java.text.ParseException e) {
         throw new Error("Parameter error in indata file \n" + e + " in line " + e.getErrorOffset());
      } catch (Error e) {
         throw e;
      }
      try {
         this.resultwriter.initialize();
      }  catch (Error e) {
         throw new Error("Initialization error of writer");
      }
      try {
         this.trackwriter.initialize();
      }  catch (Error e) {
         throw new Error("Initialization error of trackwriter");
      }
   }
   public void post() {
   }
   private void print() {
      System.out.println();
      System.out.println();
      System.out.println("time:" + this.time);
      try {
         this.resultwriter.write(this.filename, this.time);
         this.trackwriter.write(this.time);
      }  catch (IOException ioe) {
         System.out.println(ioe);
         return;
      }
   }
   public void setInitialConditions() {
      int i;
      int j;
      Node temp_node;
      Node neighbour_node;
      Element temp_element;
      Tracker temp_tracker;
      Constraint temp_constraint;
      double total_mass;
      System.out.println("Initializing nodes");
      for (i = 0; i < this.nodelist.size(); i++) {
         temp_node = (Node)this.nodelist.elementAt(i);
         temp_node.setInitialConditions();
      }
      System.out.println("Setting up node neighbour handles");
      for (j = 0; j < this.nodelist.size(); j++) {
         temp_node = (Node)this.nodelist.elementAt(j);
         if (j < this.nodelist.size() - 1) {
            temp_node.setRight_neighbour((Node)this.nodelist.elementAt(j + 1));
         }
         if (j > 0) {
            temp_node.setLeft_neighbour((Node)this.nodelist.elementAt(j - 1));
         }
      }
      System.out.println("Sorting nodes");
      for (j = 0; j < this.nodelist.size(); j++) {
         for (i = 0; i < this.nodelist.size(); i++) {
            temp_node = (Node)this.nodelist.elementAt(i);
            temp_node.checkNeighbours();
         }
      }
      System.out.println("Determining optimal model distribution for nodes");
      temp_node = (Node)this.nodelist.firstElement();
      while (temp_node.getLeft_neighbour() != null) temp_node = temp_node.getLeft_neighbour();
      for (i = 0; i < this.nodelist.size(); i++) {
         temp_node.setCpu_number(i * this.nr_of_CPUs / this.nodelist.size());
         temp_node = temp_node.getRight_neighbour();
      }
      System.out.println("Initializing elements");
      for (i = 0; i < this.number_of_elements; i++) {
         temp_element = (Element)this.elementlist.elementAt(i);
         temp_element.setInitialConditions();
      }
      System.out.println("Initializing trackers");
      for (i = 0; i < this.number_of_trackers; i++) {
         temp_tracker = (Tracker)this.trackerlist.elementAt(i);
         temp_tracker.setInitialConditions();
      }
      System.out.println("Initializing constraints");
      for (i = 0; i < this.number_of_constraints; i++) {
         temp_constraint = (Constraint)this.constraintlist.elementAt(i);
         temp_constraint.setInitialConditions();
      }
      this.controlset.setInitialConditions();
      System.out.println("Determining smallest timestep size");
      if (this.controlset.getTimestep(0) == 0) {
         this.timestep = 1.0E10;
         this.autostep = true;
         for (i = 0; i < this.number_of_elements; i++) {
            this.timestep = ((Element)this.elementlist.elementAt(i)).checkTimestep(this.timestep);
         }
      } else {
         this.timestep = this.controlset.getTimestep(0);
         this.autostep = false;
      }
      System.out.println("Determined timestep:" + this.timestep);
      System.out.println("Calculating total model mass");
      total_mass = 0;
      for (i = 0; i < this.nodelist.size(); i++) {
         temp_node = (Node)this.nodelist.elementAt(i);
         total_mass += temp_node.getMass();
      }
      System.out.println("Total model mass = " + total_mass);
   }
   public void solve() throws InterruptedException {
      int j;
      long time_info;
      long time_info_tmp;
      long time_remained;
      int number_of_integration_points;
      int time_h;
      int time_m;
      int time_s;
      String time_str;
      Thread[] tsolve;
      Worker[] worker;
      final Barrier barrier;
      long stime;
      time_info = new Date().getTime();
      this.ttemp = 1.0E10;
      tsolve = new Thread[this.nr_of_CPUs];
      worker = new Worker[this.nr_of_CPUs];
      barrier = BarrierFactory.createBarrier(this.nr_of_CPUs + 1);
      this.time = this.controlset.getStarttime();
      for (this.i = 0; this.i < this.nr_of_CPUs; this.i++) {
         worker[this.i] = new Worker(barrier, this.time, this.timestep, this.autostep);
         tsolve[this.i] = new Thread(worker[this.i]);
      }
      System.out.println("Distributing the model on " + this.nr_of_CPUs + "CPU:s");
      for (int i = 0; i < this.elementlist.size(); i++) {
         this.temporary_element = (Element)this.elementlist.elementAt(i);
         worker[this.temporary_element.getCpu_number()].addElement(this.temporary_element);
      }
      for (int i = 0; i < this.nodelist.size(); i++) {
         this.temporary_node = (Node)this.nodelist.elementAt(i);
         worker[this.temporary_node.getCpu_number()].addNode(this.temporary_node);
      }
      for (this.i = 0; this.i < this.nr_of_CPUs; this.i++) tsolve[this.i].start();
      for (this.time = this.controlset.getStarttime(); this.time < this.controlset.getEndtime(); this.time += this.timestep) {
         for (this.i = 0; this.i < this.nr_of_CPUs; this.i++) {
            worker[this.i].setTime(this.time);
            worker[this.i].setTimestep(this.timestep);
            worker[this.i].setAutostep(this.autostep);
         }
         barrier.sync();
         stime = System.currentTimeMillis();
         barrier.sync();
         stime = System.currentTimeMillis();
         if (this.failure_is_set) {
            for (this.i = 0; this.i < this.number_of_elements; this.i++) {
               this.temporary_element = (Element)this.elementlist.elementAt(this.i);
               if (!this.temporary_element.isDeActivated()) {
                  this.temporary_element.checkIfFailed();
                  if (this.temporary_element.hasFailed()) this.temporary_element.deActivate();
               }
            }
         }
         for (this.i = 0; this.i < this.number_of_constraints; this.i++) {
            this.temporary_constraint = (Constraint)this.constraintlist.elementAt(this.i);
            this.temporary_constraint.update();
         }
         for (this.i = 0; this.i < this.nodelist.size(); this.i++) {
            this.temporary_node = (Node)this.nodelist.elementAt(this.i);
            if (!this.temporary_node.isDeActivated()) {
               this.temporary_node.calculateNewPosition(this.timestep, this.time);
               this.temporary_node.checkNeighbours();
            }
         }
         for (this.i = 0; this.i < this.number_of_trackers; this.i++) {
            this.temporary_tracker = (Tracker)this.trackerlist.elementAt(this.i);
            this.temporary_tracker.collectData();
            this.temporary_tracker.calculate();
         }
         if (this.controlset.getTimestep(this.time) == 0) {
            for (this.i = 0; this.i < this.nr_of_CPUs; this.i++) {
               this.ttemp = Math.min(this.ttemp, worker[this.i].getTtemp());
            }
            this.timestep = this.ttemp;
            this.ttemp = 1.0E10;
            this.autostep = true;
         } else {
            this.timestep = this.controlset.getTimestep(this.time);
            this.autostep = false;
         }
         if (this.controlset.timeToPrint(this.time)) {
            System.out.println();
            System.out.println();
            time_info_tmp = new Date().getTime();
            time_remained = (time_info_tmp - time_info) * (int)((this.controlset.getEndtime() - this.time) / this.controlset.getPrintstep()) / 1000;
            time_h = (int)(time_remained / 3600);
            time_m = (int)(time_remained / 60 - time_h * 60);
            time_s = (int)(time_remained - (int)(time_remained / 60) * 60);
            time_str = "";
            if (time_h < 10) {
               time_str += "0" + time_h;
            } else {
               time_str += time_h;
            }
            if (time_m < 10) {
               time_str += ":0" + time_m;
            } else {
               time_str += ":" + time_m;
            }
            if (time_s < 10) {
               time_str += ":0" + time_s;
            } else {
               time_str += ":" + time_s;
            }
            System.out.println("time:" + this.time + "\tRemaining time (hh:mm:ss) " + time_str);
            time_info = time_info_tmp;
            try {
               this.resultwriter.write(this.filename, this.time);
            }  catch (IOException ioe) {
               System.out.println(ioe);
               return;
            }
         }
         if (this.controlset.timeToPrintTracker(this.time)) {
            this.trackwriter.write(this.time);
         }
         barrier.sync();
         barrier.sync();
         if (Thread.interrupted()) {
            System.out.println("stopping main thread");
            this.time = this.controlset.getEndtime();
         }
      }
      for (this.i = 0; this.i < this.nr_of_CPUs; this.i++) worker[this.i].setKeep_running(false);
      this.post();
   }
}
