use of cbit.vcell.parser.Discontinuity in project vcell by virtualcell.
the class FiniteVolumeFileWriter method getDiscontinuityTimes.
private void getDiscontinuityTimes(Vector<Discontinuity> discontinuities, TreeSet<Double> discontinuityTimes) throws ExpressionException, MathException {
Simulation simulation = simTask.getSimulation();
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
for (Discontinuity discontinuity : discontinuities) {
Expression rfexp = discontinuity.getRootFindingExp();
rfexp.bindExpression(simSymbolTable);
rfexp = simSymbolTable.substituteFunctions(rfexp).flatten();
String[] symbols = rfexp.getSymbols();
if (symbols == null) {
continue;
}
boolean bHasT = false;
for (String symbol : symbols) {
if (symbol.equals(ReservedVariable.TIME.getName())) {
bHasT = true;
}
}
if (bHasT) {
if (symbols.length != 1) {
throw new ExpressionException(simulation.getSolverTaskDescription().getSolverDescription().getDisplayLabel() + ": time discontinuity " + discontinuity.getDiscontinuityExp().infix() + " can only be a function of time");
}
Expression deriv = rfexp.differentiate(ReservedVariable.TIME.getName());
// we don't allow 5t < 3
double d = deriv.evaluateConstant();
if (d != 1 && d != -1) {
throw new ExpressionException(simulation.getSolverTaskDescription().getSolverDescription().getDisplayLabel() + ": time discontinuity " + discontinuity.getDiscontinuityExp().infix() + " is not allowed.");
}
rfexp.substituteInPlace(new Expression(ReservedVariable.TIME.getName()), new Expression(0));
rfexp.flatten();
double st = Math.abs(rfexp.evaluateConstant());
discontinuityTimes.add(st);
}
}
/*Simulation simulation = simTask.getSimulation();
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
MonadicFunctionRootFinder rootFinder = new Bisection();
for (Discontinuity discontinuity : discontinuities) {
Expression rfexp = discontinuity.getRootFindingExp();
rfexp.bindExpression(simSymbolTable);
rfexp = simSymbolTable.substituteFunctions(rfexp).flatten();
String[] symbols = rfexp.getSymbols();
boolean bHasT = false;
for (String symbol : symbols) {
if (symbol.equals(ReservedVariable.TIME.getName())) {
bHasT = true;
}
}
if (bHasT) {
if (symbols.length != 1) {
System.err.println(simulation.getSolverTaskDescription().getSolverDescription().getDisplayLabel()
+ ": discontinuity " + discontinuity.getDiscontinuityExp().infix() + " is not just a function of time, not handled properly by solver");
continue;
}
double startTime = simulation.getSolverTaskDescription().getTimeBounds().getStartingTime();
double endTime = simulation.getSolverTaskDescription().getTimeBounds().getEndingTime();
findAllRoots(rfexp,startTime,endTime,rootFinder,discontinuityTimes,false);
}
} ---------------------------------JIM's CODE COMMENTTED FOR FUTURE DEVELOPMENT*/
}
use of cbit.vcell.parser.Discontinuity in project vcell by virtualcell.
the class OdeFileWriter method writeEvents.
private String writeEvents(HashMap<Discontinuity, String> discontinuityNameMap) throws ExpressionException {
Simulation simulation = simTask.getSimulation();
StringBuffer sb = new StringBuffer();
MathDescription mathDescription = simulation.getMathDescription();
Iterator<Event> iter = mathDescription.getEvents();
sb.append("EVENTS " + mathDescription.getNumEvents() + "\n");
while (iter.hasNext()) {
Event event = iter.next();
sb.append("EVENT " + event.getName() + "\n");
Expression triggerExpression = event.getTriggerExpression();
triggerExpression = MathUtilities.substituteFunctions(triggerExpression, varsSymbolTable).flatten();
Vector<Discontinuity> v = triggerExpression.getDiscontinuities();
for (Discontinuity od : v) {
od = getSubsitutedAndFlattened(od, varsSymbolTable);
String dname = discontinuityNameMap.get(od);
if (dname == null) {
dname = ROOT_VARIABLE_PREFIX + discontinuityNameMap.size();
discontinuityNameMap.put(od, dname);
}
triggerExpression.substituteInPlace(od.getDiscontinuityExp(), new Expression("(" + dname + "==1)"));
}
sb.append("TRIGGER " + triggerExpression.infix() + ";\n");
Delay delay = event.getDelay();
if (delay != null) {
Expression durationExpression = delay.getDurationExpression();
durationExpression = MathUtilities.substituteFunctions(durationExpression, varsSymbolTable).flatten();
sb.append("DELAY " + delay.useValuesFromTriggerTime() + " " + durationExpression.infix() + ";\n");
}
sb.append("EVENTASSIGNMENTS " + event.getNumEventAssignments() + "\n");
Iterator<EventAssignment> iter2 = event.getEventAssignments();
while (iter2.hasNext()) {
EventAssignment eventAssignment = iter2.next();
Expression assignmentExpression = eventAssignment.getAssignmentExpression();
assignmentExpression = MathUtilities.substituteFunctions(assignmentExpression, varsSymbolTable).flatten();
Variable assignmentTarget = eventAssignment.getVariable();
for (int i = 0; i < fieldStateVariables.size(); i++) {
if (assignmentTarget.getName().equals(fieldStateVariables.get(i).getVariable().getName())) {
sb.append(i + " " + assignmentExpression.infix() + ";\n");
break;
}
}
}
}
return sb.toString();
}
use of cbit.vcell.parser.Discontinuity in project vcell by virtualcell.
the class OdeFileWriter method write.
/**
* Insert the method's description here.
* Creation date: (3/8/00 10:31:52 PM)
*/
public void write(String[] parameterNames) throws Exception {
createStateVariables();
createSymbolTable();
Simulation simulation = simTask.getSimulation();
if (simulation.getSolverTaskDescription().getUseSymbolicJacobian()) {
throw new RuntimeException("symbolic jacobian option not yet supported in interpreted Stiff solver");
}
writeJMSParamters();
SolverTaskDescription solverTaskDescription = simulation.getSolverTaskDescription();
TimeBounds timeBounds = solverTaskDescription.getTimeBounds();
ErrorTolerance errorTolerance = solverTaskDescription.getErrorTolerance();
printWriter.println("SOLVER " + getSolverName());
printWriter.println("STARTING_TIME " + timeBounds.getStartingTime());
printWriter.println("ENDING_TIME " + timeBounds.getEndingTime());
printWriter.println("RELATIVE_TOLERANCE " + errorTolerance.getRelativeErrorTolerance());
printWriter.println("ABSOLUTE_TOLERANCE " + errorTolerance.getAbsoluteErrorTolerance());
printWriter.println("MAX_TIME_STEP " + simulation.getSolverTaskDescription().getTimeStep().getMaximumTimeStep());
OutputTimeSpec ots = simulation.getSolverTaskDescription().getOutputTimeSpec();
if (ots.isDefault()) {
printWriter.println("KEEP_EVERY " + ((DefaultOutputTimeSpec) ots).getKeepEvery());
} else if (ots.isUniform()) {
printWriter.println("OUTPUT_TIME_STEP " + ((UniformOutputTimeSpec) ots).getOutputTimeStep());
} else if (ots.isExplicit()) {
printWriter.println("OUTPUT_TIMES " + ((ExplicitOutputTimeSpec) ots).getNumTimePoints());
printWriter.println(((ExplicitOutputTimeSpec) ots).toSpaceSeperatedMultiLinesOfString());
}
if (parameterNames != null && parameterNames.length != 0) {
printWriter.println("NUM_PARAMETERS " + parameterNames.length);
for (int i = 0; i < parameterNames.length; i++) {
printWriter.println(parameterNames[i]);
}
}
HashMap<Discontinuity, String> discontinuityNameMap = new HashMap<Discontinuity, String>();
String eventString = null;
if (simulation.getMathDescription().hasEvents()) {
eventString = writeEvents(discontinuityNameMap);
}
String equationString = writeEquations(discontinuityNameMap);
if (discontinuityNameMap.size() > 0) {
printWriter.println("DISCONTINUITIES " + discontinuityNameMap.size());
for (Discontinuity od : discontinuityNameMap.keySet()) {
printWriter.println(discontinuityNameMap.get(od) + " " + od.getDiscontinuityExp().flatten().infix() + "; " + od.getRootFindingExp().flatten().infix() + ";");
}
}
if (eventString != null) {
printWriter.print(eventString);
}
printWriter.println("NUM_EQUATIONS " + getStateVariableCount());
printWriter.println(equationString);
}
Aggregations