/** Sets up all parameters with target Graph target */ public MoleculeParameters(Molecule target) { // automatically set initial individuals size range int vertices = target.getVerticesSize(); verticesInterval.set(Math.max(2, vertices / 2), vertices * 2); cyclesInterval.set(0, target.getNumberOfCycles() * 2); // insure that all possible vertex and edge types are in initial population in roughly equal // quantities java.util.Hashtable elements = new java.util.Hashtable(); for (VertexIterator v = target.getVertexIterator(); v.more(); v.next()) { String s = v.vertex().toString(); if (!elements.containsKey(s)) { elements.put(s, s); try { provider.add((Vertex) v.vertex().clone()); } catch (CloneNotSupportedException e) { Error.fatal("can't clone vertex: " + e); } } } for (EdgeIterator e = target.getEdgeIterator(); e.more(); e.next()) { String s = e.edge().toString(); if (!elements.containsKey(s)) { elements.put(s, s); try { provider.add((Edge) e.edge().clone()); } catch (CloneNotSupportedException ee) { Error.fatal("can't clone edge: " + ee); } } } setParameters(target); }
public String toString(Molecule molecule) { // write header String returnString = String.format( "%%mem=%dGB\n%%nprocshared=%d\n#p geom=connect %s/genecp empiricaldispersion=gd3bj opt\n", mem, nprocshared, method); returnString += "\ntitle\n\n0 1\n"; // write geometry returnString = returnString + molecule.getGJFstring() + "\n"; // initialize some variables Atom fromAtom = null; Atom toAtom = null; Integer fromAtomNumber = 0; Integer toAtomNumber = 0; Double bondOrder = 0.0; DefaultWeightedEdge thisEdge = null; // read connectivity data into parallel arrays ArrayList<Integer> fromAtoms = new ArrayList<Integer>(); ArrayList<Integer> toAtoms = new ArrayList<Integer>(); ArrayList<Double> bondOrders = new ArrayList<Double>(); ArrayList<Boolean> visited = new ArrayList<Boolean>(); for (DefaultWeightedEdge e : molecule.connectivity.edgeSet()) { fromAtom = molecule.connectivity.getEdgeSource(e); fromAtomNumber = molecule.contents.indexOf(fromAtom) + 1; toAtom = molecule.connectivity.getEdgeTarget(e); toAtomNumber = molecule.contents.indexOf(toAtom) + 1; bondOrder = molecule.connectivity.getEdgeWeight(e); fromAtoms.add(fromAtomNumber); toAtoms.add(toAtomNumber); bondOrders.add(bondOrder); visited.add(false); } // write connectivity data for (int i = 0; i < molecule.contents.size(); i++) { returnString = returnString + (i + 1) + " "; for (int j = 0; j < fromAtoms.size(); j++) { if (fromAtoms.get(j) == i + 1 && visited.get(j) == false) { returnString = returnString + toAtoms.get(j) + " " + String.format("%.1f ", bondOrders.get(j)); visited.set(j, true); } if (toAtoms.get(j) == i + 1 && visited.get(j) == false) { returnString = returnString + fromAtoms.get(j) + " " + String.format("%.1f ", bondOrders.get(j)); visited.set(j, true); } } returnString = returnString + "\n"; } // write footer returnString += String.format("\n@%s\n\n", basis); return returnString; }
public static void main(String[] args) { JFrame frame = new JFrame(); Molecule molecule = new Molecule("H"); SiteType[] type = new SiteType[Colors.COLORARRAY.length]; for (int i = 0; i < type.length; i++) { type[i] = new SiteType(molecule, "Type " + i); type[i].setRadius(1 + 0.5 * (i % 3)); type[i].setColor(Colors.COLORARRAY[i]); molecule.addType(type[i]); } Site[] site = new Site[3 * type.length]; for (int i = 0; i < site.length; i++) { site[i] = new Site(molecule, type[i % type.length]); site[i].setX(30 * Math.cos(2 * Math.PI * i / site.length)); site[i].setY(30 * Math.sin(2 * Math.PI * i / site.length)); site[i].setZ(6); site[i].setLocation(SystemGeometry.INSIDE); molecule.addSite(site[i]); } Link[] link = new Link[site.length - 1]; for (int i = 0; i < link.length; i++) { link[i] = new Link(site[i], site[i + 1]); molecule.addLink(link[i]); } DrawPanel3D panel3d = new DrawPanel3D(molecule); DrawPanel3DPanel panel = new DrawPanel3DPanel(panel3d); Container c = frame.getContentPane(); c.add(panel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setSize(600, 400); frame.setVisible(true); }
private Position checkRailPos(Position curpos, Position newPos, Molecule molecule) { // TODO Auto-generated method stub double a1 = curpos.getX(); double b1 = curpos.getY(); double c1 = curpos.getZ(); double a2 = newPos.getX(); double b2 = newPos.getY(); double c2 = newPos.getZ(); for (MicroTubule mt : listOfMicroTubule) { double r = mt.getRadiusMicroTubule(); Position plusend = mt.getPlusEndCentre(); Position minusend = mt.getMinusEndCentre(); double x1 = plusend.getX(); double y1 = plusend.getY(); double z1 = plusend.getZ(); double x2 = minusend.getX(); double y2 = minusend.getY(); double z2 = minusend.getZ(); double l = x2 - x1; double m = y2 - y1; double n = z2 - z1; double a = Math.pow(n * (b2 - b1) - m * (c2 - c1), 2) + Math.pow(l * (c2 - c1) - n * (a2 - a1), 2) + Math.pow(m * (a2 - a1) - l * (b2 - b1), 2); double b = (n * (b2 - b1) - m * (c2 - c1)) * (n * (b1 - y1) - m * (c1 - z1)) + (l * (c2 - c1) - n * (a2 - a1)) * (l * (c1 - z1) - n * (a1 - x1)) + (m * (a2 - a1) - l * (b2 - b1)) * (m * (a1 - x1) - l * (b1 - y1)); double c = Math.pow(n * (b1 - y1) - m * (c1 - z1), 2) + Math.pow(l * (c1 - z1) - n * (a1 - x1), 2) + Math.pow(m * (a1 - x1) - l * (b1 - y1), 2) - r * r * (l * l + m * m + n * n); double det = -1; if ((det = b * b - a * c) >= 0) { double t1 = (Math.sqrt(det) - b) / a; Position pos1 = new Position(a1 + t1 * (a2 - a1), b1 + t1 * (b2 - b1), c1 + t1 * (c2 - c1)); double t2 = -(Math.sqrt(det) + b) / a; Position pos2 = new Position(a1 + t2 * (a2 - a1), b1 + t2 * (b2 - b1), c1 + t2 * (c2 - c1)); double dist1 = Double.MAX_VALUE; double dist2 = Double.MAX_VALUE; if (t1 > 0 && t1 <= 1 && Math.sqrt(Math.pow(pos1.getDistance(plusend), 2) - r * r) <= plusend.getDistance(minusend) && Math.sqrt(Math.pow(pos1.getDistance(minusend), 2) - r * r) <= plusend.getDistance(minusend)) { dist1 = pos1.getDistance(curpos); } if (t2 > 0 && t2 <= 1 && Math.sqrt(Math.pow(pos2.getDistance(plusend), 2) - r * r) <= plusend.getDistance(minusend) && Math.sqrt(Math.pow(pos2.getDistance(minusend), 2) - r * r) <= plusend.getDistance(minusend)) { dist2 = pos2.getDistance(curpos); } if (dist1 <= dist2 && dist1 != Double.MAX_VALUE) { molecule.setCurrentMicrotubule(mt); return pos1; } if (dist2 < dist1) { molecule.setCurrentMicrotubule(mt); return pos2; } } } return null; }
private void simulatePropagation(Molecule molecule, FileWriter... writer) { String newline = ""; long time = System.nanoTime(); long elapsed = 0; double runStep = maxSimulationTime; // boolean reachFlag = false; boolean onRailFlag = false; // double distance = distSendReciever!=0?distSendReciever:; if (maxSimulationStep > 0) { runStep = maxSimulationStep; } try { Position curpos = molecule.getPosition(); if (probDrail != 0) { if (!onRailFlag) { for (MicroTubule mt : listOfMicroTubule) { Position p = getInitPointOnMicrotubule(mt, curpos); if (p != null) { molecule.setCurrentMicrotubule(mt); molecule.setPosition(p); onRailFlag = true; break; } } } } while ((elapsed = (maxSimulationStep <= 0) ? (System.nanoTime() - time) : (elapsed + 1)) < (long) runStep) { curpos = molecule.getPosition(); if (generateOutputFile) { writer[1].write(newline + curpos.getX() + delim + curpos.getY() + delim + curpos.getZ()); newline = "\n"; } if (hasReachDestination(curpos)) { if (maxSimulationStep <= 0) { molecule.setReachTime(System.nanoTime() - time); } else { molecule.setReachTime(elapsed); } molecule.setReachFlag(true); break; } if (probDrail != 0) { if (onRailFlag) { if (hasDRailed()) { onRailFlag = false; System.out.println("off rail"); molecule.setCurrentMicrotubule(null); Position newPos = new Position(); newPos.setX( curpos.getX() + molecule.getStepLengthX() * steparr[(int) (Math.random() * 3)]); newPos.setY( curpos.getY() + molecule.getStepLengthY() * steparr[(int) (Math.random() * 3)]); newPos.setZ( curpos.getZ() + molecule.getStepLengthZ() * steparr[(int) (Math.random() * 3)]); checkBoundary(newPos); Position nextPos = checkRailPos(curpos, newPos, molecule); if (nextPos != null) { // distance = curpos.getDistance(reciever); molecule.setPosition(nextPos); onRailFlag = true; System.out.println("On Rail"); } else { molecule.setPosition(newPos); } } else { Position plusend = molecule.getCurrentMicrotubule().getPlusEndCentre(); Position minusend = molecule.getCurrentMicrotubule().getMinusEndCentre(); Position newPos = new Position(); Position perp = new Position(); double x2 = plusend.getX(); double y2 = plusend.getY(); double z2 = plusend.getZ(); double x1 = minusend.getX(); double y1 = minusend.getY(); double z1 = minusend.getZ(); double d = curpos.getX(); double e = curpos.getY(); double f = curpos.getZ(); double t = ((x2 - d) * (x2 - x1) + (y2 - e) * (y2 - y1) + (z2 - f) * (z2 - z1)) / ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)); perp.setX(d + (x2 - x1) * t); perp.setY(e + (y2 - y1) * t); perp.setZ(f + (z2 - z1) * t); double distance = perp.getDistance(curpos); curpos.setX( (velRail * perp.getX() + (distance - velRail) * curpos.getX()) / distance); curpos.setY( (velRail * perp.getY() + (distance - velRail) * curpos.getY()) / distance); curpos.setZ( (velRail * perp.getZ() + (distance - velRail) * curpos.getZ()) / distance); // System.out.println(curpos.getX()+","+curpos.getY()+","+curpos.getZ()); // molecule.setPosition(curpos); } } else { Position newPos = new Position(); newPos.setX( curpos.getX() + molecule.getStepLengthX() * steparr[(int) (Math.random() * 3)]); newPos.setY( curpos.getY() + molecule.getStepLengthY() * steparr[(int) (Math.random() * 3)]); newPos.setZ( curpos.getZ() + molecule.getStepLengthZ() * steparr[(int) (Math.random() * 3)]); checkBoundary(newPos); Position nextPos = checkRailPos(curpos, newPos, molecule); if (nextPos != null) { // distance = curpos.getDistance(reciever); molecule.setPosition(nextPos); onRailFlag = true; System.out.println("Gets on Rail"); } else { molecule.setPosition(newPos); } } } else { curpos.setX( curpos.getX() + molecule.getStepLengthX() * steparr[(int) (Math.random() * 3)]); curpos.setY( curpos.getY() + molecule.getStepLengthY() * steparr[(int) (Math.random() * 3)]); curpos.setZ( curpos.getZ() + molecule.getStepLengthZ() * steparr[(int) (Math.random() * 3)]); checkBoundary(curpos); } if (generateOutputFile) { writer[1].flush(); } } if (generateOutputFile) { writer[1].flush(); writer[1].close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (molecule.isReachFlag()) { System.out.println(":) Hooray this molecule reached to destination"); try { writer[0].write(molecule.getReachTime() + "\n"); writer[0].flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else System.out.println(":( This molecule couldn't reach its destination"); }