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"); }