Search in sources :

Example 1 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class NFsimXMLWriter method getListOfFunctions.

private static Element getListOfFunctions(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
    Element listOfParametersElement = new Element("ListOfFunctions");
    for (Variable var : simulationSymbolTable.getVariables()) {
        Double value = null;
        if (var instanceof Constant || var instanceof Function) {
            Expression valExpression = var.getExpression();
            Expression substitutedValExpr = null;
            try {
                substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
            } catch (Exception e) {
                e.printStackTrace(System.out);
                throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
            }
            try {
                value = substitutedValExpr.evaluateConstant();
            } catch (ExpressionException e) {
                System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
            }
            Element functionElement = new Element("Function");
            functionElement.setAttribute("id", var.getName());
            if (value != null) {
                // parameter, see getListOfParameters() above
                continue;
            } else {
                Element listOfReferencesElement = new Element("ListOfReferences");
                String[] references = valExpression.getSymbols();
                for (int i = 0; i < references.length; i++) {
                    String reference = references[i];
                    Element referenceElement = new Element("Reference");
                    referenceElement.setAttribute("name", reference);
                    Variable referenceVariable = simulationSymbolTable.getVariable(reference);
                    Double referenceValue = null;
                    Expression referenceExpression = referenceVariable.getExpression();
                    Expression substitutedReferenceExpression = null;
                    if (referenceExpression != null) {
                        try {
                            substitutedReferenceExpression = simulationSymbolTable.substituteFunctions(referenceExpression);
                        } catch (Exception e) {
                            e.printStackTrace(System.out);
                            throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
                        }
                        try {
                            referenceValue = substitutedReferenceExpression.evaluateConstant();
                        } catch (ExpressionException e) {
                            System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
                        }
                    }
                    if (referenceVariable instanceof ParticleObservable) {
                        referenceElement.setAttribute("type", "Observable");
                    } else if (referenceVariable instanceof Function) {
                        if (referenceValue != null) {
                            referenceElement.setAttribute("type", "ConstantExpression");
                        } else {
                            referenceElement.setAttribute("type", "Function");
                        }
                    } else {
                        // constant
                        referenceElement.setAttribute("type", "ConstantExpression");
                    }
                    listOfReferencesElement.addContent(referenceElement);
                }
                functionElement.addContent(listOfReferencesElement);
                Element expressionElement = new Element("Expression");
                String functionExpression = valExpression.infix();
                expressionElement.setText(functionExpression);
                functionElement.addContent(expressionElement);
            }
            listOfParametersElement.addContent(functionElement);
        }
    }
    return listOfParametersElement;
}
Also used : Variable(cbit.vcell.math.Variable) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) Element(org.jdom.Element) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) ExpressionException(cbit.vcell.parser.ExpressionException) Function(cbit.vcell.math.Function) Expression(cbit.vcell.parser.Expression) SolverException(cbit.vcell.solver.SolverException) ParticleObservable(cbit.vcell.math.ParticleObservable)

Example 2 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class NFsimXMLWriter method getListOfParameters.

private static Element getListOfParameters(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
    Element listOfParametersElement = new Element("ListOfParameters");
    for (Variable var : simulationSymbolTable.getVariables()) {
        Double value = null;
        if (var instanceof Constant || var instanceof Function) {
            Expression valExpression = var.getExpression();
            Expression substitutedValExpr = null;
            try {
                substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
            } catch (Exception e) {
                e.printStackTrace(System.out);
                throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
            }
            try {
                value = substitutedValExpr.evaluateConstant();
            } catch (ExpressionException e) {
                System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
            }
            Element parameterElement = new Element("Parameter");
            parameterElement.setAttribute("id", var.getName());
            if (value != null) {
                parameterElement.setAttribute("type", "Constant");
                parameterElement.setAttribute("value", value.toString());
            } else {
                // function, see getListOfFunctions() below
                continue;
            }
            listOfParametersElement.addContent(parameterElement);
        }
    }
    return listOfParametersElement;
}
Also used : Function(cbit.vcell.math.Function) Variable(cbit.vcell.math.Variable) Expression(cbit.vcell.parser.Expression) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) Element(org.jdom.Element) SolverException(cbit.vcell.solver.SolverException) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 3 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class ParameterEstimationTaskSimulatorIDA method getRowColumnRestultSetByBestEstimations.

public RowColumnResultSet getRowColumnRestultSetByBestEstimations(ParameterEstimationTask parameterEstimationTask, String[] paramNames, double[] paramValues) throws Exception {
    // create a temp simulation based on math description
    KeyValue key = new KeyValue("" + Math.abs(new Random().nextLong()));
    SimulationVersion dummyVersion = new SimulationVersion(key, "name", new User("temp", new KeyValue("1")), null, null, null, null, null, null, null);
    Simulation simulation = new Simulation(dummyVersion, parameterEstimationTask.getSimulationContext().getMathDescription());
    ReferenceData refData = parameterEstimationTask.getModelOptimizationSpec().getReferenceData();
    double[] times = refData.getDataByColumn(0);
    double endTime = times[times.length - 1];
    ExplicitOutputTimeSpec exTimeSpec = new ExplicitOutputTimeSpec(times);
    // set simulation ending time and output interval
    simulation.getSolverTaskDescription().setTimeBounds(new TimeBounds(0, endTime));
    simulation.getSolverTaskDescription().setOutputTimeSpec(exTimeSpec);
    // set parameters as math overrides
    MathOverrides mathOverrides = simulation.getMathOverrides();
    for (int i = 0; i < paramNames.length; i++) {
        mathOverrides.putConstant(new Constant(paramNames[i], new Expression(paramValues[i])));
    }
    SimulationTask simTask = new SimulationTask(new SimulationJob(simulation, 0, null), 0);
    IDASolverStandalone idaSolver = new IDASolverStandalone(simTask, ResourceUtil.getLocalSimDir("temp"), false);
    // startSolver();
    idaSolver.runSolver();
    Thread.sleep(1000);
    long startTimeMS = System.currentTimeMillis();
    while (idaSolver.getSolverStatus().isRunning() && System.currentTimeMillis() < (startTimeMS + 10000L)) {
        Thread.sleep(500);
    }
    ODESolverResultSet resultset = idaSolver.getODESolverResultSet();
    return resultset;
}
Also used : KeyValue(org.vcell.util.document.KeyValue) User(org.vcell.util.document.User) SimulationTask(cbit.vcell.messaging.server.SimulationTask) Constant(cbit.vcell.math.Constant) ReferenceData(cbit.vcell.opt.ReferenceData) TimeBounds(cbit.vcell.solver.TimeBounds) MathOverrides(cbit.vcell.solver.MathOverrides) ExplicitOutputTimeSpec(cbit.vcell.solver.ExplicitOutputTimeSpec) SimulationVersion(org.vcell.util.document.SimulationVersion) Random(java.util.Random) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) IDASolverStandalone(cbit.vcell.solver.ode.IDASolverStandalone) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) SimulationJob(cbit.vcell.solver.SimulationJob)

Example 4 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class ParameterEstimationTaskSimulatorIDA method getOdeSolverResultSet.

private ODESolverResultSet getOdeSolverResultSet(ParameterEstimationTask parameterEstimationTask, OptimizationSpec optSpec, OptimizationResultSet optResultSet) throws Exception {
    if (optResultSet == null) {
        return null;
    }
    String[] parameterNames = optResultSet.getOptSolverResultSet().getParameterNames();
    double[] bestEstimates = optResultSet.getOptSolverResultSet().getBestEstimates();
    // if we don't have parameter names or best estimates, return null. if we have them, we can run a simulation and generate a solution
    if (parameterNames == null || parameterNames.length == 0 || bestEstimates == null || bestEstimates.length == 0) {
        return null;
    }
    // check if we have solution or not, if not, generate a solution since we have the best estimates
    if (optResultSet.getSolutionNames() == null) {
        RowColumnResultSet rcResultSet = getRowColumnRestultSetByBestEstimations(parameterEstimationTask, parameterNames, bestEstimates);
        optResultSet.setSolutionFromRowColumnResultSet(rcResultSet);
    }
    String[] solutionNames = optResultSet.getSolutionNames();
    if (solutionNames != null && solutionNames.length > 0) {
        ODESolverResultSet odeSolverResultSet = new ODESolverResultSet();
        // add data column descriptions
        for (int i = 0; i < solutionNames.length; i++) {
            odeSolverResultSet.addDataColumn(new ODESolverResultSetColumnDescription(solutionNames[i]));
        }
        // 
        // add row data
        // 
        int numRows = optResultSet.getSolutionValues(0).length;
        for (int i = 0; i < numRows; i++) {
            odeSolverResultSet.addRow(optResultSet.getSolutionRow(i));
        }
        // 
        // make temporary simulation (with overrides for parameter values)
        // 
        MathDescription mathDesc = parameterEstimationTask.getSimulationContext().getMathDescription();
        Simulation simulation = new Simulation(mathDesc);
        SimulationSymbolTable simSymbolTable = new SimulationSymbolTable(simulation, 0);
        // 
        for (int i = 0; i < optSpec.getParameters().length; i++) {
            cbit.vcell.opt.Parameter parameter = optSpec.getParameters()[i];
            simulation.getMathOverrides().putConstant(new Constant(parameter.getName(), new Expression(parameter.getInitialGuess())));
        }
        // 
        for (int i = 0; i < parameterNames.length; i++) {
            simulation.getMathOverrides().putConstant(new Constant(parameterNames[i], new Expression(optResultSet.getOptSolverResultSet().getBestEstimates()[i])));
        }
        // 
        // add functions (evaluating them at optimal parameter)
        // 
        Vector<AnnotatedFunction> annotatedFunctions = simSymbolTable.createAnnotatedFunctionsList(mathDesc);
        for (AnnotatedFunction f : annotatedFunctions) {
            Expression funcExp = f.getExpression();
            for (int j = 0; j < parameterNames.length; j++) {
                funcExp.substituteInPlace(new Expression(parameterNames[j]), new Expression(optResultSet.getOptSolverResultSet().getBestEstimates()[j]));
            }
            odeSolverResultSet.addFunctionColumn(new FunctionColumnDescription(funcExp, f.getName(), null, f.getName(), false));
        }
        return odeSolverResultSet;
    } else {
        return null;
    }
}
Also used : MathDescription(cbit.vcell.math.MathDescription) Constant(cbit.vcell.math.Constant) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) Simulation(cbit.vcell.solver.Simulation) Expression(cbit.vcell.parser.Expression) ODESolverResultSet(cbit.vcell.solver.ode.ODESolverResultSet) ODESolverResultSetColumnDescription(cbit.vcell.math.ODESolverResultSetColumnDescription) FunctionColumnDescription(cbit.vcell.math.FunctionColumnDescription) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 5 with Constant

use of cbit.vcell.math.Constant in project vcell by virtualcell.

the class Xmlproducer method getXML.

/**
 * This method returns a XML representation of a MathDescription object.
 * Creation date: (3/2/2001 10:57:25 AM)
 * @return Element
 * @param mathdes cbit.vcell.math.MathDescription
 */
Element getXML(MathDescription mathdes) throws XmlParseException {
    Element math = new Element(XMLTags.MathDescriptionTag);
    // Add attributes
    math.setAttribute(XMLTags.NameAttrTag, mangle(mathdes.getName()));
    // Add annotation
    if (mathdes.getDescription() != null && mathdes.getDescription().length() > 0) {
        Element annotationElem = new Element(XMLTags.AnnotationTag);
        annotationElem.setText(mangle(mathdes.getDescription()));
        math.addContent(annotationElem);
    }
    List<ParticleMolecularType> particleMolecularTypes = mathdes.getParticleMolecularTypes();
    for (ParticleMolecularType particleMolecularType : particleMolecularTypes) {
        math.addContent(getXML(particleMolecularType));
    }
    // Add Constant subelements
    Enumeration<Variable> enum1 = mathdes.getVariables();
    /*java.util.Iterator k;
    try {
    	VariableHash varHash = new VariableHash();
    	while (enum1.hasMoreElements()) 
    	 	varHash.addVariable((Variable)enum1.nextElement());
    	Variable vars [] = varHash.getReorderedVariables();
    	k = new ArrayList(java.util.Arrays.asList(vars)).iterator();
    } catch (cbit.vcell.mapping.MappingException e) {
		e.printStackTrace();
		return null;
    }*/
    while (enum1.hasMoreElements()) {
        Variable var = enum1.nextElement();
        Element element = null;
        if (var instanceof Constant) {
            element = getXML((Constant) var);
        } else if (var instanceof FilamentRegionVariable) {
            element = getXML((FilamentRegionVariable) var);
        } else if (var instanceof FilamentVariable) {
            element = getXML((FilamentVariable) var);
        } else if (var instanceof PointVariable) {
            element = getXML((PointVariable) var);
        } else if (var instanceof Function) {
            element = getXML((Function) var);
        } else if (var instanceof RandomVariable) {
            element = getXML((RandomVariable) var);
        } else if (var instanceof InsideVariable) {
            // *** for internal use! Ignore it ***
            continue;
        } else if (var instanceof MembraneRegionVariable) {
            element = getXML((MembraneRegionVariable) var);
        } else if (var instanceof MemVariable) {
            element = getXML((MemVariable) var);
        } else if (var instanceof OutsideVariable) {
            // *** for internal use! Ignore it ****
            continue;
        } else if (var instanceof VolumeRegionVariable) {
            element = getXML((VolumeRegionVariable) var);
        } else if (var instanceof VolVariable) {
            element = getXML((VolVariable) var);
        } else if (var instanceof StochVolVariable) {
            // added for stochastic volumn variables
            element = getXML((StochVolVariable) var);
        } else if (var instanceof ParticleVariable) {
            element = getXML((ParticleVariable) var);
        } else if (var instanceof ParticleObservable) {
            element = getXML((ParticleObservable) var);
        } else {
            throw new XmlParseException("An unknown variable type " + var.getClass().getName() + " was found when parsing the mathdescription " + mathdes.getName() + "!");
        }
        transcribeComments(var, element);
        math.addContent(element);
    }
    // this was moved to the simspec!
    /*	buffer.append("\n");
    	if (geometry != null){
    		buffer.append(geometry.getXML());
    	}	
    	buffer.append("\n");*/
    // Add subdomains
    Enumeration<SubDomain> enum2 = mathdes.getSubDomains();
    while (enum2.hasMoreElements()) {
        SubDomain subDomain = enum2.nextElement();
        math.addContent(getXML(subDomain));
    }
    // Add Metadata (Version) if there is!
    if (mathdes.getVersion() != null) {
        math.addContent(getXML(mathdes.getVersion(), mathdes));
    }
    Iterator<Event> iter = mathdes.getEvents();
    while (iter.hasNext()) {
        math.addContent(getXML(iter.next()));
    }
    PostProcessingBlock postProcessingBlock = mathdes.getPostProcessingBlock();
    if (postProcessingBlock.getNumDataGenerators() > 0) {
        math.addContent(getXML(postProcessingBlock));
    }
    return math;
}
Also used : FilamentVariable(cbit.vcell.math.FilamentVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) InsideVariable(cbit.vcell.math.InsideVariable) VolVariable(cbit.vcell.math.VolVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) PointVariable(cbit.vcell.math.PointVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) MemVariable(cbit.vcell.math.MemVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) Variable(cbit.vcell.math.Variable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) MacroscopicRateConstant(cbit.vcell.math.MacroscopicRateConstant) Constant(cbit.vcell.math.Constant) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) ParticleVariable(cbit.vcell.math.ParticleVariable) Element(org.jdom.Element) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) InsideVariable(cbit.vcell.math.InsideVariable) PostProcessingBlock(cbit.vcell.math.PostProcessingBlock) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction) Function(cbit.vcell.math.Function) MemVariable(cbit.vcell.math.MemVariable) ParticleMolecularType(cbit.vcell.math.ParticleMolecularType) StochVolVariable(cbit.vcell.math.StochVolVariable) StochVolVariable(cbit.vcell.math.StochVolVariable) VolVariable(cbit.vcell.math.VolVariable) FilamentRegionVariable(cbit.vcell.math.FilamentRegionVariable) FilamentVariable(cbit.vcell.math.FilamentVariable) Event(cbit.vcell.math.Event) BioEvent(cbit.vcell.mapping.BioEvent) PointVariable(cbit.vcell.math.PointVariable) OutsideVariable(cbit.vcell.math.OutsideVariable) VolumeParticleObservable(cbit.vcell.math.VolumeParticleObservable) ParticleObservable(cbit.vcell.math.ParticleObservable)

Aggregations

Constant (cbit.vcell.math.Constant)69 Expression (cbit.vcell.parser.Expression)43 Variable (cbit.vcell.math.Variable)31 Function (cbit.vcell.math.Function)24 MathDescription (cbit.vcell.math.MathDescription)22 VolVariable (cbit.vcell.math.VolVariable)21 ExpressionException (cbit.vcell.parser.ExpressionException)21 MathException (cbit.vcell.math.MathException)15 Vector (java.util.Vector)15 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)14 MemVariable (cbit.vcell.math.MemVariable)14 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)13 SubDomain (cbit.vcell.math.SubDomain)13 MacroscopicRateConstant (cbit.vcell.math.MacroscopicRateConstant)12 VolumeRegionVariable (cbit.vcell.math.VolumeRegionVariable)12 ReservedVariable (cbit.vcell.math.ReservedVariable)11 FilamentVariable (cbit.vcell.math.FilamentVariable)10 InsideVariable (cbit.vcell.math.InsideVariable)10 OutsideVariable (cbit.vcell.math.OutsideVariable)10 FilamentRegionVariable (cbit.vcell.math.FilamentRegionVariable)9