Search in sources :

Example 1 with ParameterVariable

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

the class FiniteVolumeFileWriter method write.

/**
 * Insert the method's description here.
 * Creation date: (5/9/2005 2:52:48 PM)
 * @throws MathException
 * @throws ExpressionException
 * @throws DataAccessException
 * @throws IOException
 */
public void write(String[] parameterNames) throws Exception {
    Variable[] originalVars = null;
    Simulation simulation = simTask.getSimulation();
    MathDescription mathDesc = simulation.getMathDescription();
    if (bChomboSolver) {
        writeJMSParamters();
        writeSimulationParamters();
        writeModelDescription();
        writeChomboSpec();
        writeVariables();
        writePostProcessingBlock();
        writeCompartments();
        writeMembranes();
    } else {
        if (simTask.getSimulation().isSerialParameterScan()) {
            originalVars = (Variable[]) BeanUtils.getArray(mathDesc.getVariables(), Variable.class);
            Variable[] allVars = (Variable[]) BeanUtils.getArray(mathDesc.getVariables(), Variable.class);
            MathOverrides mathOverrides = simulation.getMathOverrides();
            String[] scanParameters = mathOverrides.getOverridenConstantNames();
            for (int i = 0; i < scanParameters.length; i++) {
                String scanParameter = scanParameters[i];
                Variable mathVariable = mathDesc.getVariable(scanParameter);
                // 
                if (mathVariable instanceof Constant) {
                    Constant origConstant = (Constant) mathVariable;
                    for (int j = 0; j < allVars.length; j++) {
                        if (allVars[j].equals(origConstant)) {
                            allVars[j] = new ParameterVariable(origConstant.getName());
                            break;
                        }
                    }
                }
            }
            mathDesc.setAllVariables(allVars);
        }
        writeJMSParamters();
        writeSimulationParamters();
        writeModelDescription();
        writeMeshFile();
        writeVariables();
        if (mathDesc.isSpatialHybrid()) {
            writeSmoldyn();
        }
        writeParameters(parameterNames);
        writeSerialParameterScans();
        writeFieldData();
        writePostProcessingBlock();
        writeCompartments();
        writeMembranes();
        if (originalVars != null) {
            mathDesc.setAllVariables(originalVars);
        }
    }
}
Also used : MathOverrides(cbit.vcell.solver.MathOverrides) FilamentVariable(cbit.vcell.math.FilamentVariable) VolVariable(cbit.vcell.math.VolVariable) ReservedVariable(cbit.vcell.math.ReservedVariable) ParameterVariable(cbit.vcell.math.ParameterVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) MemVariable(cbit.vcell.math.MemVariable) Variable(cbit.vcell.math.Variable) Simulation(cbit.vcell.solver.Simulation) MathDescription(cbit.vcell.math.MathDescription) Constant(cbit.vcell.math.Constant) PseudoConstant(cbit.vcell.math.PseudoConstant) ParameterVariable(cbit.vcell.math.ParameterVariable)

Example 2 with ParameterVariable

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

the class OdeFileWriter method createSymbolTable.

/**
 * OdeFileCoder constructor comment.
 * @throws Exception
 */
private void createSymbolTable() throws Exception {
    // 
    // Create symbol table for binding sensitivity variable expressions. (Cannot bind to simulation,
    // since it does not have the sensitivity variables corresponding to the volume variables).
    // 
    varsSymbolTable = new VariableSymbolTable();
    // SymbolTableEntry.index doesn't matter ... just code generating binding by var names not index.
    varsSymbolTable.addVar(ReservedVariable.TIME);
    int count = 0;
    Variable[] variables = simTask.getSimulationJob().getSimulationSymbolTable().getVariables();
    for (int i = 0; i < variables.length; i++) {
        if (variables[i] instanceof VolVariable) {
            VolVariable vVar = (VolVariable) variables[i];
            vVar.setIndex(count);
            varsSymbolTable.addVar(vVar);
            count++;
        } else if (variables[i] instanceof Function) {
            Function func = (Function) variables[i];
            func.setIndex(count);
            varsSymbolTable.addVar(func);
            count++;
        } else if (variables[i] instanceof Constant) {
            Constant constant = (Constant) variables[i];
            constant.setIndex(count);
            varsSymbolTable.addVar(constant);
            count++;
        } else if (variables[i] instanceof ParameterVariable) {
            ParameterVariable param = (ParameterVariable) variables[i];
            param.setIndex(count);
            varsSymbolTable.addVar(param);
            count++;
        }
    }
    // Get the vector of sensVariables, needed for creating SensStateVariables
    Vector<SensStateVariable> sensVars = new Vector<SensStateVariable>();
    for (int i = 0; i < getStateVariableCount(); i++) {
        if (simTask.getSimulation().getSolverTaskDescription().getSensitivityParameter() != null) {
            if (getStateVariable(i) instanceof SensStateVariable) {
                sensVars.addElement((SensStateVariable) getStateVariable(i));
            }
        }
    }
    for (int j = count; j < (count + sensVars.size()); j++) {
        SensVariable sVar = (SensVariable) (sensVars.elementAt(j - count).getVariable());
        sVar.setIndex(j);
        varsSymbolTable.addVar(sVar);
    }
}
Also used : Function(cbit.vcell.math.Function) ReservedVariable(cbit.vcell.math.ReservedVariable) ParameterVariable(cbit.vcell.math.ParameterVariable) Variable(cbit.vcell.math.Variable) VolVariable(cbit.vcell.math.VolVariable) VolVariable(cbit.vcell.math.VolVariable) Constant(cbit.vcell.math.Constant) VariableSymbolTable(cbit.vcell.parser.VariableSymbolTable) ParameterVariable(cbit.vcell.math.ParameterVariable) Vector(java.util.Vector)

Example 3 with ParameterVariable

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

the class ModelOptimizationMapping method computeOptimizationSpec.

/**
 * Insert the method's description here.
 * Creation date: (8/22/2005 9:26:52 AM)
 * @return cbit.vcell.opt.OptimizationSpec
 * @param modelOptimizationSpec cbit.vcell.modelopt.ModelOptimizationSpec
 */
MathSymbolMapping computeOptimizationSpec() throws MathException, MappingException {
    if (getModelOptimizationSpec().getReferenceData() == null) {
        System.out.println("no referenced data defined");
        return null;
    }
    OptimizationSpec optSpec = new OptimizationSpec();
    optSpec.setComputeProfileDistributions(modelOptimizationSpec.isComputeProfileDistributions());
    parameterMappings = null;
    // 
    // get original MathDescription (later to be substituted for local/global parameters).
    // 
    SimulationContext simContext = modelOptimizationSpec.getSimulationContext();
    MathMapping mathMapping = simContext.createNewMathMapping();
    MathDescription origMathDesc = null;
    mathSymbolMapping = null;
    try {
        origMathDesc = mathMapping.getMathDescription();
        mathSymbolMapping = mathMapping.getMathSymbolMapping();
    } catch (MatrixException e) {
        e.printStackTrace(System.out);
        throw new MappingException(e.getMessage());
    } catch (ModelException e) {
        e.printStackTrace(System.out);
        throw new MappingException(e.getMessage());
    } catch (ExpressionException e) {
        e.printStackTrace(System.out);
        throw new MathException(e.getMessage());
    }
    // 
    // create objective function (mathDesc and data)
    // 
    ReferenceData referenceData = getRemappedReferenceData(mathMapping);
    if (referenceData == null) {
        throw new RuntimeException("no referenced data defined");
    }
    // 
    // get parameter mappings
    // 
    ParameterMappingSpec[] parameterMappingSpecs = modelOptimizationSpec.getParameterMappingSpecs();
    Vector<ParameterMapping> parameterMappingList = new Vector<ParameterMapping>();
    Variable[] allVars = (Variable[]) BeanUtils.getArray(origMathDesc.getVariables(), Variable.class);
    for (int i = 0; i < parameterMappingSpecs.length; i++) {
        cbit.vcell.model.Parameter modelParameter = parameterMappingSpecs[i].getModelParameter();
        String mathSymbol = null;
        Variable mathVariable = null;
        if (mathSymbolMapping != null) {
            Variable variable = mathSymbolMapping.getVariable(modelParameter);
            if (variable != null) {
                mathSymbol = variable.getName();
            }
            if (mathSymbol != null) {
                mathVariable = origMathDesc.getVariable(mathSymbol);
            }
        }
        if (mathVariable != null) {
            if (parameterMappingSpecs[i].isSelected()) {
                if (parameterMappingSpecs[i].getHigh() < parameterMappingSpecs[i].getLow()) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
                }
                if (parameterMappingSpecs[i].getCurrent() < parameterMappingSpecs[i].getLow()) {
                    throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is smaller than its lower bound.");
                }
                if (parameterMappingSpecs[i].getCurrent() > parameterMappingSpecs[i].getHigh()) {
                    throw new MathException("The initial guess of '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is greater than its upper bound.");
                }
                if (parameterMappingSpecs[i].getLow() < 0) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All lower bounds must not be negative.");
                }
                if (Double.isInfinite(parameterMappingSpecs[i].getLow())) {
                    throw new MathException("The lower bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Lower bounds must not be infinity.");
                }
                if (parameterMappingSpecs[i].getHigh() <= 0) {
                    throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is negative. All upper bounds must be positive.");
                }
                if (Double.isInfinite(parameterMappingSpecs[i].getHigh())) {
                    throw new MathException("The upper bound for Parameter '" + parameterMappingSpecs[i].getModelParameter().getName() + "' is infinity. Upper bounds must not be infinity.");
                }
            }
            double low = parameterMappingSpecs[i].isSelected() && parameterMappingSpecs[i].getLow() == 0 ? 1e-8 : parameterMappingSpecs[i].getLow();
            double high = parameterMappingSpecs[i].getHigh();
            double scale = Math.abs(parameterMappingSpecs[i].getCurrent()) < 1.0E-10 ? 1.0 : Math.abs(parameterMappingSpecs[i].getCurrent());
            double current = parameterMappingSpecs[i].getCurrent();
            low = Math.min(low, current);
            high = Math.max(high, current);
            Parameter optParameter = new Parameter(mathSymbol, low, high, scale, current);
            ParameterMapping parameterMapping = new ParameterMapping(modelParameter, optParameter, mathVariable);
            // 
            if (mathVariable instanceof Constant) {
                Constant origConstant = (Constant) mathVariable;
                for (int j = 0; j < allVars.length; j++) {
                    if (allVars[j].equals(origConstant)) {
                        if (parameterMappingSpecs[i].isSelected()) {
                            allVars[j] = new ParameterVariable(origConstant.getName());
                        } else {
                            allVars[j] = new Constant(origConstant.getName(), new Expression(optParameter.getInitialGuess()));
                        }
                        break;
                    }
                }
            }
            // 
            if (parameterMappingSpecs[i].isSelected()) {
                parameterMappingList.add(parameterMapping);
            }
        }
    }
    parameterMappings = (ParameterMapping[]) BeanUtils.getArray(parameterMappingList, ParameterMapping.class);
    try {
        origMathDesc.setAllVariables(allVars);
    } catch (ExpressionBindingException e) {
        e.printStackTrace(System.out);
        throw new MathException(e.getMessage());
    }
    // 
    for (int i = 0; i < parameterMappings.length; i++) {
        optSpec.addParameter(parameterMappings[i].getOptParameter());
    }
    Vector<Issue> issueList = new Vector<Issue>();
    IssueContext issueContext = new IssueContext();
    optSpec.gatherIssues(issueContext, issueList);
    for (int i = 0; i < issueList.size(); i++) {
        Issue issue = issueList.elementAt(i);
        if (issue.getSeverity() == Issue.SEVERITY_ERROR) {
            throw new RuntimeException(issue.getMessage());
        }
    }
    // 
    // 
    // 
    optimizationSpec = optSpec;
    return mathSymbolMapping;
}
Also used : ParameterVariable(cbit.vcell.math.ParameterVariable) Variable(cbit.vcell.math.Variable) Issue(org.vcell.util.Issue) MathDescription(cbit.vcell.math.MathDescription) Constant(cbit.vcell.math.Constant) ParameterVariable(cbit.vcell.math.ParameterVariable) ExpressionException(cbit.vcell.parser.ExpressionException) MappingException(cbit.vcell.mapping.MappingException) MatrixException(cbit.vcell.matrix.MatrixException) IssueContext(org.vcell.util.IssueContext) OptimizationSpec(cbit.vcell.opt.OptimizationSpec) Vector(java.util.Vector) ModelException(cbit.vcell.model.ModelException) SimulationContext(cbit.vcell.mapping.SimulationContext) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) SimpleReferenceData(cbit.vcell.opt.SimpleReferenceData) ReferenceData(cbit.vcell.opt.ReferenceData) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) MathMapping(cbit.vcell.mapping.MathMapping) Parameter(cbit.vcell.opt.Parameter)

Aggregations

Constant (cbit.vcell.math.Constant)3 ParameterVariable (cbit.vcell.math.ParameterVariable)3 Variable (cbit.vcell.math.Variable)3 MathDescription (cbit.vcell.math.MathDescription)2 ReservedVariable (cbit.vcell.math.ReservedVariable)2 VolVariable (cbit.vcell.math.VolVariable)2 Vector (java.util.Vector)2 MappingException (cbit.vcell.mapping.MappingException)1 MathMapping (cbit.vcell.mapping.MathMapping)1 SimulationContext (cbit.vcell.mapping.SimulationContext)1 FilamentVariable (cbit.vcell.math.FilamentVariable)1 Function (cbit.vcell.math.Function)1 MathException (cbit.vcell.math.MathException)1 MemVariable (cbit.vcell.math.MemVariable)1 MembraneParticleVariable (cbit.vcell.math.MembraneParticleVariable)1 MembraneRandomVariable (cbit.vcell.math.MembraneRandomVariable)1 MembraneRegionVariable (cbit.vcell.math.MembraneRegionVariable)1 PseudoConstant (cbit.vcell.math.PseudoConstant)1 RandomVariable (cbit.vcell.math.RandomVariable)1 VolumeParticleVariable (cbit.vcell.math.VolumeParticleVariable)1