diff --git a/python/test/launch-SpatialKappa/launch-SpatialKappa.py b/python/test/launch-SpatialKappa/launch-SpatialKappa.py index a3a14e5..55f3693 100644 --- a/python/test/launch-SpatialKappa/launch-SpatialKappa.py +++ b/python/test/launch-SpatialKappa/launch-SpatialKappa.py @@ -8,8 +8,22 @@ sim.runForTime(0.01) print('Time: %4.3f. %5.0f molecules of P %5.0f molecules of Ca' % (sim.getTime(), sim.getObservation("P"), sim.getObservation("ca"))) +## Add an integer number of Ca ions (but given as a float +sim.addAgent('ca', 11.0) +print('Time: %4.3f. %5.0f molecules of P %5.0f molecules of Ca, 11 more than before' % (sim.getTime(), sim.getObservation("P"), sim.getObservation("ca"))) + +## Adding a non-integer number of ions should throw an error: +try: + sim.addAgent('ca', 11.5) +except: + print "Error was raised correctly" + sim.runUntilTime(0.1) print('Time: %4.3f. %5.0f molecules of P %5.0f molecules of Ca' % (sim.getTime(), sim.getObservation("P"), sim.getObservation("ca"))) +## Add an integer number of Ca ions specified as an integer +sim.addAgent('ca', 10) + sim.runForTime(0.01) print('Time: %4.3f. %5.0f molecules of P %5.0f molecules of Ca' % (sim.getTime(), sim.getObservation("P"), sim.getObservation("ca"))) + diff --git a/src/java/org/demonsoft/spatialkappa/api/SpatialKappaSim.java b/src/java/org/demonsoft/spatialkappa/api/SpatialKappaSim.java index 90cb42b..f558259 100644 --- a/src/java/org/demonsoft/spatialkappa/api/SpatialKappaSim.java +++ b/src/java/org/demonsoft/spatialkappa/api/SpatialKappaSim.java @@ -53,16 +53,11 @@ public void loadFile(String kappaFile) throws Exception { } private void initialiseSim() { - try { - System.out.println("initialiseSim()"); - simulation = new TransitionMatchingSimulation(kappaModel); - } catch (Exception e) { - System.out.println("Error in initialiseSim()"); - } + simulation = new TransitionMatchingSimulation(kappaModel); } - public void runUntilTime(float stepEndTime) { - simulation.runByTime2(stepEndTime*(float)timeMult); + public void runUntilTime(float stepEndTime, boolean progress) { + simulation.runByTime2(stepEndTime*(float)timeMult, progress); if (verbose) { // This allows us to get the value of a particular observable Observation observation = simulation.getCurrentObservation(); @@ -70,9 +65,9 @@ public void runUntilTime(float stepEndTime) { } } - public void runForTime(float dt) { + public void runForTime(float dt, boolean progress) { float stepEndTime = getTime() + dt; - runUntilTime(stepEndTime); + runUntilTime(stepEndTime, progress); } public Map getVariables() { @@ -98,7 +93,7 @@ public double getObservation(String key) { } // value can be negative - public void addAgent(String key, double value) { + public void addAgent(String key, int value) { List agents = new ArrayList(); SimulationState state = (SimulationState) simulation; for (Complex complex : kappaModel.getFixedLocatedInitialValuesMap().keySet()) { @@ -107,14 +102,23 @@ public void addAgent(String key, double value) { if (key.equals(currentAgent.name)) { // if (verbose) { System.out.println("ADD STUFF"); } agents.add(currentAgent); - state.addComplexInstances(agents, (int)value); + state.addComplexInstances(agents, value); agents.clear(); } } } } - public void setAgentInitialValue(String key, double value) { + public void addAgent(String key, double value) { + int ivalue = (int)value; + if (ivalue != value) { + String error = "Trying to add non-integer number (" + value + ") of \'" + key + "\' agents"; + throw(new IllegalArgumentException(error)); + } + addAgent(key, (int)value); + } + + public void setAgentInitialValue(String key, int value) { List agents = new ArrayList(); for (Complex complex : kappaModel.getFixedLocatedInitialValuesMap().keySet()) { for (Agent currentAgent : complex.agents) { @@ -123,13 +127,16 @@ public void setAgentInitialValue(String key, double value) { System.out.println("Set number of " + currentAgent.name + " to " + value); } agents.add(currentAgent); - kappaModel.overrideInitialValue(agents, Integer.toString((int)value), NOT_LOCATED); + kappaModel.overrideInitialValue(agents, Integer.toString(value), NOT_LOCATED); agents.clear(); } } } initialiseSim(); - System.out.println("Number of " + key + " is " + getObservation(key)); + if (verbose) { System.out.println("Number of " + key + " is " + getObservation(key)); } + } + public void setAgentInitialValue(String key, double value) { + setAgentInitialValue(key, (int)value); } private void getFixedLocatedInitialValuesMap() { diff --git a/src/java/org/demonsoft/spatialkappa/tools/TransitionMatchingSimulation.java b/src/java/org/demonsoft/spatialkappa/tools/TransitionMatchingSimulation.java index 84620f9..99525e9 100644 --- a/src/java/org/demonsoft/spatialkappa/tools/TransitionMatchingSimulation.java +++ b/src/java/org/demonsoft/spatialkappa/tools/TransitionMatchingSimulation.java @@ -201,10 +201,13 @@ public void runByTime(float totalTime, float timePerStep) { while (!noTransitionsPossible && !stop && time < totalTime); notifyObservationListeners(true, 1); } - public void runByTime2(float stepEndTime) { + runByTime2(stepEndTime, true); + } + + public void runByTime2(float stepEndTime, boolean progress) { if (verbose) { - System.out.println("runByTime2. time = " + time + " ; stepEndTime = " + stepEndTime); + System.out.println("\rTime = " + time + " / " + stepEndTime + " [" + time/stepEndTime*100 + "%]"); } startTime = Calendar.getInstance().getTimeInMillis(); stop = false; @@ -217,6 +220,9 @@ public void runByTime2(float stepEndTime) { } resetTransitionsFiredCount(); while (time < stepEndTime && !noTransitionsPossible && !stop) { + if (progress) { + System.out.format("\rTime = %12.5f/%5.5f [%3.3f%%]", time, stepEndTime, time/stepEndTime*100); + } float nextEventTime = time + getTimeDelta(); if (verbose) { System.out.println("runByTime2: nextEventTime = " + nextEventTime); @@ -224,6 +230,10 @@ public void runByTime2(float stepEndTime) { if (nextEventTime > stepEndTime) { time = stepEndTime; notifyObservationListeners(true, 1); + if (progress) { + System.out.format("\rTime = %12.5f/%5.5f [%3.3f%%]\n", time, stepEndTime, 100.0); + System.out.flush(); + } return; } int clashes = 0;