Search in sources :

Example 11 with Simulation

use of cbit.vcell.solver.Simulation 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 12 with Simulation

use of cbit.vcell.solver.Simulation in project vcell by virtualcell.

the class XmlReader method getMathModel.

/*
public RateRuleVariable[] getRateRuleVariables(Element rateRuleVarsElement, Model model) throws XmlParseException  {
	Iterator<Element> rateRuleVarsIterator = rateRuleVarsElement.getChildren(XMLTags.RateRuleVariableTag, vcNamespace).iterator();
	Vector<RateRuleVariable> rateRuleVarsVector = new Vector<RateRuleVariable>();
	while (rateRuleVarsIterator.hasNext()) {
		Element rrvElement = (Element) rateRuleVarsIterator.next();

		RateRuleVariable newRateRuleVar = null;
		try {
			String rrvName = unMangle(rrvElement.getAttributeValue(XMLTags.NameAttrTag));
		    String rrvStructureName = unMangle(rrvElement.getAttributeValue(XMLTags.StructureAttrTag));
		    // structure can be null
		    Structure rrvStructure = null;
		    if (rrvStructureName != null) {
		    	rrvStructure = (Structure) model.getStructure(rrvStructureName);
		    }
//		    if (structureref == null) {
//		    	throw new XmlParseException("The structure " + rrvStructureName + "could not be resolved!");
//		    }
		    String rrvRoleStr = rrvElement.getAttributeValue(XMLTags.ParamRoleAttrTag);
		    int rrvRole = RateRuleVariable.getParamRoleFromDesc(rrvRoleStr);
		    Element rrvParamElement = rrvElement.getChild(XMLTags.ParameterTag, vcNamespace);
		    ModelParameter rrvParameter = getModelParameter(rrvParamElement, model);
		    newRateRuleVar = new RateRuleVariable(rrvName, rrvStructure, rrvParameter, rrvRole);
			newRateRuleVar.bind();
		} catch (ExpressionBindingException e) {
			e.printStackTrace(System.out);
			throw new XmlParseException(e.getMessage());
		}
		if (newRateRuleVar != null) {
			rateRuleVarsVector.add(newRateRuleVar);
		}
	}
	
	return ((RateRuleVariable[])BeanUtils.getArray(rateRuleVarsVector, RateRuleVariable.class));
}
*/
/**
 * This method returns a MathModel object from a XML Element.
 * Creation date: (3/13/2001 12:35:00 PM)
 * @return cbit.vcell.mathmodel.MathModel
 * @param param org.jdom.Element
 */
public MathModel getMathModel(Element param) throws XmlParseException {
    // Create it
    // set Metadata (version), if any
    Version versionObject = getVersion(param.getChild(XMLTags.VersionTag, vcNamespace));
    MathModel mathmodel = new MathModel(versionObject);
    // Set attributes
    String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
    try {
        mathmodel.setName(name);
        // String annotation = param.getAttributeValue(XMLTags.AnnotationAttrTag);
        // if (annotation!=null) {
        // mathmodel.setDescription(unMangle(annotation));
        // }
        // Add annotation
        String annotationText = param.getChildText(XMLTags.AnnotationTag, vcNamespace);
        if (annotationText != null && annotationText.length() > 0) {
            mathmodel.setDescription(unMangle(annotationText));
        }
    } catch (java.beans.PropertyVetoException e) {
        e.printStackTrace();
        throw new XmlParseException("An error occurred while trying to set the name " + param.getAttributeValue(XMLTags.NameAttrTag) + "to a MathModel!", e);
    }
    // set Geometry (if any)
    Element tempElem = param.getChild(XMLTags.GeometryTag, vcNamespace);
    Geometry tempGeometry = getGeometry(tempElem);
    // set MathDescription
    tempElem = param.getChild(XMLTags.MathDescriptionTag, vcNamespace);
    MathDescription mathDesc = getMathDescription(tempElem, tempGeometry);
    if (tempElem != null) {
        mathmodel.setMathDescription(mathDesc);
    } else {
        throw new XmlParseException("MathDescription missing in this MathModel!");
    }
    // set output functions (outputfunctionContext)
    Element outputFunctionsElement = param.getChild(XMLTags.OutputFunctionsTag, vcNamespace);
    if (outputFunctionsElement != null) {
        ArrayList<AnnotatedFunction> outputFunctions = getOutputFunctions(outputFunctionsElement);
        try {
            // construct OutputFnContext from mathmodel and add output functions that were read in from XML.
            OutputFunctionContext outputFnContext = mathmodel.getOutputFunctionContext();
            for (AnnotatedFunction outputFunction : outputFunctions) {
                outputFnContext.addOutputFunction(outputFunction);
            }
        } catch (PropertyVetoException e) {
            e.printStackTrace(System.out);
            throw new XmlParseException(e);
        }
    }
    // Set simulations contexts (if any)
    List<Element> childList = param.getChildren(XMLTags.SimulationTag, vcNamespace);
    Simulation[] simList = new Simulation[childList.size()];
    int simCounter = 0;
    for (Element simElement : childList) {
        simList[simCounter] = getSimulation(simElement, mathDesc);
        simCounter++;
    }
    try {
        mathmodel.setSimulations(simList);
    } catch (java.beans.PropertyVetoException e) {
        e.printStackTrace();
        throw new XmlParseException("A PropertyVetoException occurred when adding the Simulations to the MathModel " + name, e);
    }
    return mathmodel;
}
Also used : MathModel(cbit.vcell.mathmodel.MathModel) MathDescription(cbit.vcell.math.MathDescription) Element(org.jdom.Element) PropertyVetoException(java.beans.PropertyVetoException) Geometry(cbit.vcell.geometry.Geometry) OutputFunctionContext(cbit.vcell.solver.OutputFunctionContext) PropertyVetoException(java.beans.PropertyVetoException) Simulation(cbit.vcell.solver.Simulation) Version(org.vcell.util.document.Version) RedistributionVersion(cbit.vcell.solvers.mb.MovingBoundarySolverOptions.RedistributionVersion) SimulationVersion(org.vcell.util.document.SimulationVersion) VCellSoftwareVersion(org.vcell.util.document.VCellSoftwareVersion) AnnotatedFunction(cbit.vcell.solver.AnnotatedFunction)

Example 13 with Simulation

use of cbit.vcell.solver.Simulation in project vcell by virtualcell.

the class SimulationContext method addNewSimulation.

/**
 * Sets the simulations property (cbit.vcell.solver.Simulation[]) value.
 * @param simulations The new value for the property.
 * @exception java.beans.PropertyVetoException The exception description.
 * @see #getSimulations
 */
public Simulation addNewSimulation(String simNamePrefix, MathMappingCallback callback, NetworkGenerationRequirements networkGenerationRequirements) throws java.beans.PropertyVetoException {
    refreshMathDescription(callback, networkGenerationRequirements);
    if (bioModel == null) {
        throw new RuntimeException("cannot add simulation, bioModel not set yet");
    }
    // 
    // get free name for new Simulation.
    // 
    Simulation[] sims = bioModel.getSimulations();
    String newSimName = null;
    for (int i = 0; newSimName == null && i < 100; i++) {
        String proposedName = simNamePrefix + i;
        boolean bFound = false;
        for (int j = 0; !bFound && j < sims.length; j++) {
            if (sims[j].getName().equals(proposedName)) {
                bFound = true;
            }
        }
        if (!bFound) {
            newSimName = proposedName;
        }
    }
    if (newSimName == null) {
        throw new RuntimeException("failed to find name for new Simulation");
    }
    // 
    // create new Simulation and add to BioModel.
    // 
    Simulation newSimulation = new Simulation(getMathDescription());
    newSimulation.setName(newSimName);
    bioModel.addSimulation(newSimulation);
    return newSimulation;
}
Also used : Simulation(cbit.vcell.solver.Simulation)

Example 14 with Simulation

use of cbit.vcell.solver.Simulation in project vcell by virtualcell.

the class SimulationContext method copySimulation.

/**
 * Sets the simulations property (cbit.vcell.solver.Simulation[]) value.
 * @param simulations The new value for the property.
 * @exception java.beans.PropertyVetoException The exception description.
 * @see #getSimulations
 */
public Simulation copySimulation(Simulation simulation) throws java.beans.PropertyVetoException {
    if (getMathDescription() == null) {
        throw new RuntimeException("Application " + getName() + " has no generated Math, cannot add simulation");
    }
    if (simulation.getMathDescription() != getMathDescription()) {
        throw new IllegalArgumentException("cannot copy simulation '" + simulation.getName() + "', has different MathDescription than Application");
    }
    if (bioModel == null) {
        throw new RuntimeException("cannot add simulation, bioModel not set yet");
    }
    // 
    // get free name for copied Simulation.
    // 
    Simulation[] sims = bioModel.getSimulations();
    String newSimName = null;
    for (int i = 0; newSimName == null && i < 100; i++) {
        String proposedName = "Copy of " + simulation.getName() + ((i > 0) ? (" " + i) : (""));
        boolean bFound = false;
        for (int j = 0; !bFound && j < sims.length; j++) {
            if (sims[j].getName().equals(proposedName)) {
                bFound = true;
            }
        }
        if (!bFound) {
            newSimName = proposedName;
        }
    }
    if (newSimName == null) {
        throw new RuntimeException("failed to find name for copied Simulation");
    }
    // 
    // create new Simulation and add to BioModel.
    // 
    Simulation newSimulation = new Simulation(simulation);
    newSimulation.setName(newSimName);
    bioModel.addSimulation(newSimulation);
    return newSimulation;
}
Also used : Simulation(cbit.vcell.solver.Simulation)

Example 15 with Simulation

use of cbit.vcell.solver.Simulation in project vcell by virtualcell.

the class FRAPOptData method runRefSimulation.

public KeyValue runRefSimulation(final ClientTaskStatusSupport progressListener) throws Exception {
    BioModel bioModel = null;
    if (progressListener != null) {
        progressListener.setMessage("Running Reference Simulation...");
    }
    try {
        FieldDataIdentifierSpec psfFieldFunc = FRAPStudy.getPSFFieldData(getLocalWorkspace());
        bioModel = FRAPStudy.createNewRefBioModel(expFrapStudy, REFERENCE_DIFF_RATE_STR, getRefTimeStep(), LocalWorkspace.createNewKeyValue(), LocalWorkspace.getDefaultOwner(), psfFieldFunc, expFrapStudy.getStartingIndexForRecovery());
        // change time bound and time step
        Simulation sim = bioModel.getSimulations()[0];
        ROIDataGenerator roiDataGenerator = getExpFrapStudy().getROIDataGenerator(getLocalWorkspace());
        sim.getMathDescription().getPostProcessingBlock().addDataGenerator(roiDataGenerator);
        System.out.println("run FRAP Reference Simulation...");
        // run simulation
        FRAPStudy.runFVSolverStandalone_ref(new File(getLocalWorkspace().getDefaultSimDataDirectory()), bioModel.getSimulation(0), getExpFrapStudy().getFrapDataExternalDataInfo().getExternalDataIdentifier(), getExpFrapStudy().getRoiExternalDataInfo().getExternalDataIdentifier(), psfFieldFunc.getExternalDataIdentifier(), progressListener, true);
        KeyValue referenceSimKeyValue = sim.getVersion().getVersionKey();
        return referenceSimKeyValue;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (bioModel != null && bioModel.getSimulations() != null) {
            FRAPStudy.removeExternalDataAndSimulationFiles(bioModel.getSimulations()[0].getVersion().getVersionKey(), null, null, getLocalWorkspace());
        }
        throw e;
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) Simulation(cbit.vcell.solver.Simulation) FieldDataIdentifierSpec(cbit.vcell.field.FieldDataIdentifierSpec) BioModel(cbit.vcell.biomodel.BioModel) File(java.io.File) UserCancelException(org.vcell.util.UserCancelException)

Aggregations

Simulation (cbit.vcell.solver.Simulation)195 SimulationContext (cbit.vcell.mapping.SimulationContext)57 BioModel (cbit.vcell.biomodel.BioModel)53 MathDescription (cbit.vcell.math.MathDescription)48 KeyValue (org.vcell.util.document.KeyValue)33 Geometry (cbit.vcell.geometry.Geometry)29 MathModel (cbit.vcell.mathmodel.MathModel)27 Expression (cbit.vcell.parser.Expression)26 DataAccessException (org.vcell.util.DataAccessException)26 File (java.io.File)25 ExpressionException (cbit.vcell.parser.ExpressionException)24 IOException (java.io.IOException)24 SimulationJob (cbit.vcell.solver.SimulationJob)23 ArrayList (java.util.ArrayList)23 PropertyVetoException (java.beans.PropertyVetoException)20 UniformOutputTimeSpec (cbit.vcell.solver.UniformOutputTimeSpec)18 XMLSource (cbit.vcell.xml.XMLSource)18 SimulationTask (cbit.vcell.messaging.server.SimulationTask)17 TimeBounds (cbit.vcell.solver.TimeBounds)16 BigString (org.vcell.util.BigString)16