package run;
import java.util.*;

import java.io.*;

import run.readers.FembicReader;

import run.readers.GmshReader;

import run.readers.NastranReader;

import run.writers.FembicWriter;

import uka.karmi.rmi.RemoteException;

public class Translator implements uka.patch.Patchable, uka.transport.Transportable {
   public void createPatch(Object _copy, uka.patch.PatchOutput po) throws java.io.IOException {
      Translator copy = (Translator)_copy;
      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.i, copy.i)) copy.i = this.i;
      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.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 {
      Translator copy = (Translator)_copy;
      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.i = this.i = pi.getDiffAsInt();
      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.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.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.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 Translator(s, id);
      }
      public boolean unmarshal(Object obj, uka.transport.UnmarshalStream s) throws ClassNotFoundException, java.io.IOException {
         ((Translator)obj).unmarshal(s);
         return false;
      }
      public void marshalReference(Object obj, uka.transport.MarshalStream s) throws java.io.IOException {
         ((Translator)obj).marshalReference(s);
      }
      public void marshal(Object obj, uka.transport.MarshalStream s) throws java.io.IOException {
         ((Translator)obj).marshal(s);
      }
      public Object deepClone(Object orig, int id, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
         try {
            return new Translator((Translator)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 {
         ((Translator)copy).deepCloneReferences((Translator)orig, _helper);
         return false;
      }
      public Class getType() {
         return Translator.class;
      }
   };
   public uka.transport.TransportDescriptor getTransportDescriptor() {
      return TRANSPORT_DESCRIPTOR;
   }
   private static final int _SIZE = 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_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 Translator(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(Translator._SIZE);
      byte[] _buffer = _stream.getBuffer();
      int _pos = _stream.getPosition();
      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.i = uka.transport.BasicIO.extractInt(_buffer, _pos);
      _pos += uka.transport.BasicIO.SIZEOF_int;
      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(Translator._SIZE);
      this.filename = (java.lang.String)_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(Translator._SIZE);
      byte[] _buffer = _stream.getBuffer();
      int _pos = _stream.getPosition();
      _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.i);
      _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.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 Translator(Translator _orig, int _id, uka.transport.DeepClone _helper) throws CloneNotSupportedException, java.io.IOException {
      _helper.add(_id, this);
      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.i = _orig.i;
      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(Translator _orig, uka.transport.DeepClone _helper) throws CloneNotSupportedException {
      this.filename = (java.lang.String)_helper.internalDeepClone(_orig.filename);
      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 double time;
   private int i;
   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 java.lang.String filename;
   private double timestep;
   private boolean failure_is_set = false;
   public Translator() {
      super();
   }
   public void assembleMassMatrix() throws Error {
      int i;
      Node temp_node;
      Element temp_element;
      Constraint temp_constraint;
      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 infile, String outfile) throws Error, RemoteException {
      Reader indatafile;
      double frac;
      double frac2;
      if (infile.trim().toUpperCase().endsWith(".NAS") || infile.trim().toUpperCase().endsWith(".NASTRAN")) indatafile = new NastranReader(infile.trim()); else if (infile.trim().toUpperCase().endsWith(".MSH")) indatafile = new GmshReader(infile.trim()); else if (infile.trim().toUpperCase().endsWith(".IN")) indatafile = new FembicReader(infile.trim()); else throw new Error("Error: Unrecognized infile. .nas or .nastran is currently supported \n");
      try {
         indatafile.preProcess();
      }  catch (java.text.ParseException e) {
         throw new Error("Error during file preprocessing \n" + e);
      }
      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 {
         indatafile.open();
         System.out.println("Reading Nodes");
         frac2 = 0;
         for (this.i = 0; this.i < this.number_of_nodes; this.i++) {
            this.nodelist.addElement(indatafile.getNextNode(this.constraintlist, this.loadlist));
            frac = this.i * 10 / this.number_of_nodes;
            frac %= 10;
            if (frac != frac2) {
               System.out.println("" + 10 * frac + "% complete");
               frac2 = frac;
            }
         }
         indatafile.close();
         System.out.println("Filled nodelist");
         System.out.println("Reading Constraints");
         indatafile.open();
         frac2 = 0;
         for (this.i = 0; this.i < this.number_of_constraints; this.i++) {
            this.constraintlist.addElement(indatafile.getNextConstraint(this.nodelist));
            frac = this.i * 10 / this.number_of_constraints;
            frac %= 10;
            if (frac != frac2) {
               System.out.println("" + 10 * frac + "% complete");
               frac2 = frac;
            }
         }
         indatafile.close();
         System.out.println("Filled constraintlist");
         System.out.println("Reading Loads");
         indatafile.open();
         frac2 = 0;
         for (this.i = 0; this.i < this.number_of_loads; this.i++) {
            this.loadlist.addElement(indatafile.getNextLoad(this.nodelist));
            frac = this.i * 10 / this.number_of_loads;
            frac %= 10;
            if (frac != frac2) {
               System.out.println("" + 10 * frac + "% complete");
               frac2 = frac;
            }
         }
         indatafile.close();
         System.out.println("Filled loadlist");
         indatafile.open();
         System.out.println("Reading Materials");
         frac2 = 0;
         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);
            frac = this.i * 10 / this.number_of_materials;
            frac %= 10;
            if (frac != frac2) {
               System.out.println("" + 10 * frac + "% complete");
               frac2 = frac;
            }
         }
         indatafile.close();
         System.out.println("Filled materiallist");
         indatafile.open();
         System.out.println("Reading Elements");
         frac2 = 0;
         for (this.i = 0; this.i < this.number_of_elements; this.i++) {
            this.elementlist.addElement(indatafile.getNextElement(this.materiallist, this.nodelist, this.loadlist));
            frac = this.i * 10 / this.number_of_elements;
            frac %= 10;
            if (frac != frac2) {
               System.out.println("" + 10 * frac + "% complete");
               frac2 = frac;
            }
         }
         indatafile.close();
         System.out.println("Filled elementlist");
         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 = new FembicWriter(this.nodelist, this.elementlist, this.controlset, this.constraintlist, this.materiallist, this.loadlist);
      }  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");
      }
      this.filename = outfile.trim();
   }
   public void setInitialConditions() {
      int i;
      int j;
      Node temp_node;
      Element temp_element;
      Constraint temp_constraint;
      System.out.println("Initializing nodes");
      for (i = 0; i < this.number_of_nodes; i++) {
         temp_node = (Node)this.nodelist.elementAt(i);
         temp_node.setInitialConditions();
      }
      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 constraints");
      for (i = 0; i < this.number_of_constraints; i++) {
         temp_constraint = (Constraint)this.constraintlist.elementAt(i);
         temp_constraint.setInitialConditions();
      }
      this.controlset.setInitialConditions();
   }
   public void solve() {
   }
   public void post() {
      try {
         this.resultwriter.write(this.filename, this.time);
      }  catch (IOException ioe) {
         System.out.println(ioe);
         return;
      }
   }
}
