Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions python/test/launch-SpatialKappa/launch-SpatialKappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")))

37 changes: 22 additions & 15 deletions src/java/org/demonsoft/spatialkappa/api/SpatialKappaSim.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,21 @@ 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();
System.out.println(observation.toString());
}
}

public void runForTime(float dt) {
public void runForTime(float dt, boolean progress) {
float stepEndTime = getTime() + dt;
runUntilTime(stepEndTime);
runUntilTime(stepEndTime, progress);
}

public Map<String, Variable> getVariables() {
Expand All @@ -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<Agent> agents = new ArrayList<Agent>();
SimulationState state = (SimulationState) simulation;
for (Complex complex : kappaModel.getFixedLocatedInitialValuesMap().keySet()) {
Expand All @@ -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<Agent> agents = new ArrayList<Agent>();
for (Complex complex : kappaModel.getFixedLocatedInitialValuesMap().keySet()) {
for (Agent currentAgent : complex.agents) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -217,13 +220,20 @@ 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);
}
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;
Expand Down