Search in sources :

Example 6 with Discontinuity

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*/
}
Also used : Discontinuity(cbit.vcell.parser.Discontinuity) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 7 with Discontinuity

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();
}
Also used : Discontinuity(cbit.vcell.parser.Discontinuity) ReservedVariable(cbit.vcell.math.ReservedVariable) ParameterVariable(cbit.vcell.math.ParameterVariable) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) EventAssignment(cbit.vcell.math.Event.EventAssignment) MathDescription(cbit.vcell.math.MathDescription) Delay(cbit.vcell.math.Event.Delay) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) Event(cbit.vcell.math.Event)

Example 8 with Discontinuity

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);
}
Also used : Discontinuity(cbit.vcell.parser.Discontinuity) HashMap(java.util.HashMap) TimeBounds(cbit.vcell.solver.TimeBounds) DefaultOutputTimeSpec(cbit.vcell.solver.DefaultOutputTimeSpec) UniformOutputTimeSpec(cbit.vcell.solver.UniformOutputTimeSpec) ExplicitOutputTimeSpec(cbit.vcell.solver.ExplicitOutputTimeSpec) OutputTimeSpec(cbit.vcell.solver.OutputTimeSpec) ExplicitOutputTimeSpec(cbit.vcell.solver.ExplicitOutputTimeSpec) Simulation(cbit.vcell.solver.Simulation) ErrorTolerance(cbit.vcell.solver.ErrorTolerance) SolverTaskDescription(cbit.vcell.solver.SolverTaskDescription) DefaultOutputTimeSpec(cbit.vcell.solver.DefaultOutputTimeSpec)

Aggregations

Discontinuity (cbit.vcell.parser.Discontinuity)8 Expression (cbit.vcell.parser.Expression)6 Simulation (cbit.vcell.solver.Simulation)5 MathDescription (cbit.vcell.math.MathDescription)3 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)2 Equation (cbit.vcell.math.Equation)2 MathException (cbit.vcell.math.MathException)2 Variable (cbit.vcell.math.Variable)2 DefaultOutputTimeSpec (cbit.vcell.solver.DefaultOutputTimeSpec)2 ErrorTolerance (cbit.vcell.solver.ErrorTolerance)2 OutputTimeSpec (cbit.vcell.solver.OutputTimeSpec)2 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)2 SolverTaskDescription (cbit.vcell.solver.SolverTaskDescription)2 UniformOutputTimeSpec (cbit.vcell.solver.UniformOutputTimeSpec)2 FastSystemAnalyzer (cbit.vcell.mapping.FastSystemAnalyzer)1 Event (cbit.vcell.math.Event)1 Delay (cbit.vcell.math.Event.Delay)1 EventAssignment (cbit.vcell.math.Event.EventAssignment)1 FastInvariant (cbit.vcell.math.FastInvariant)1 FastRate (cbit.vcell.math.FastRate)1