use of cbit.vcell.math.Equation in project vcell by virtualcell.
the class Jacobian method parseMathDesc.
/**
* This method was created by a SmartGuide.
* @exception java.lang.Exception The exception description.
*/
private void parseMathDesc() throws MathException {
Vector equationList = new Vector();
Enumeration enum1 = subDomain.getEquations();
while (enum1.hasMoreElements()) {
Equation equ = (Equation) enum1.nextElement();
if (equ instanceof OdeEquation) {
equationList.addElement(equ);
} else {
throw new MathException("encountered non-ode equation, unsupported");
}
}
Vector variableList = new Vector();
enum1 = mathDesc.getVariables();
while (enum1.hasMoreElements()) {
Variable var = (Variable) enum1.nextElement();
if (var instanceof VolVariable) {
variableList.addElement(var);
}
}
if (equationList.size() != variableList.size()) {
throw new MathException("there are " + equationList.size() + " equations and " + variableList.size() + " variables");
}
numVariables = variableList.size();
rates = new Expression[numVariables];
vars = new VolVariable[numVariables];
for (int i = 0; i < numVariables; i++) {
OdeEquation odeEqu = (OdeEquation) equationList.elementAt(i);
rates[i] = odeEqu.getRateExpression();
vars[i] = (VolVariable) odeEqu.getVariable();
}
}
use of cbit.vcell.math.Equation in project vcell by virtualcell.
the class SundialsSolver method createStateVariables.
/*
This method was created in Visual Age
*/
private StateVariable[] createStateVariables() throws MathException, ExpressionException {
Vector<StateVariable> stateVariables = new Vector<StateVariable>();
// get Ode's from MathDescription and create ODEStateVariables
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
MathDescription mathDescription = simSymbolTable.getSimulation().getMathDescription();
Enumeration<Equation> enum1 = ((SubDomain) mathDescription.getSubDomains().nextElement()).getEquations();
while (enum1.hasMoreElements()) {
Equation equation = (Equation) enum1.nextElement();
if (equation instanceof OdeEquation) {
stateVariables.addElement(new ODEStateVariable((OdeEquation) equation, simSymbolTable));
} else {
throw new MathException("encountered non-ode equation, unsupported");
}
}
// Get sensitivity variables
Variable[] variables = simSymbolTable.getVariables();
Vector<SensVariable> sensVariables = new Vector<SensVariable>();
if (getSensitivityParameter() != null) {
for (int i = 0; i < variables.length; i++) {
if (variables[i] instanceof VolVariable) {
VolVariable volVariable = (VolVariable) variables[i];
SensVariable sv = new SensVariable(volVariable, getSensitivityParameter());
sensVariables.addElement(sv);
}
}
}
if (rateSensitivity == null) {
rateSensitivity = new RateSensitivity(mathDescription, mathDescription.getSubDomains().nextElement());
}
if (jacobian == null) {
jacobian = new Jacobian(mathDescription, mathDescription.getSubDomains().nextElement());
}
for (int v = 0; v < sensVariables.size(); v++) {
stateVariables.addElement(new SensStateVariable((SensVariable) sensVariables.elementAt(v), rateSensitivity, jacobian, sensVariables, simSymbolTable));
}
if (stateVariables.size() == 0) {
throw new MathException("there are no equations defined");
}
StateVariable[] stateVars = (StateVariable[]) BeanUtils.getArray(stateVariables, StateVariable.class);
return (stateVars);
}
use of cbit.vcell.math.Equation in project vcell by virtualcell.
the class OdeFileWriter method createStateVariables.
private void createStateVariables() throws Exception {
Simulation simulation = simTask.getSimulation();
MathDescription mathDescription = simulation.getMathDescription();
SolverTaskDescription solverTaskDescription = simulation.getSolverTaskDescription();
// get Ode's from MathDescription and create ODEStateVariables
Enumeration<Equation> enum1 = mathDescription.getSubDomains().nextElement().getEquations();
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
while (enum1.hasMoreElements()) {
Equation equation = enum1.nextElement();
if (equation instanceof OdeEquation) {
fieldStateVariables.addElement(new ODEStateVariable((OdeEquation) equation, simSymbolTable));
} else {
throw new MathException("encountered non-ode equation, unsupported");
}
}
// Get sensitivity variables
Variable[] variables = simSymbolTable.getVariables();
Vector<SensVariable> sensVariables = new Vector<SensVariable>();
Constant sensitivityParameter = solverTaskDescription.getSensitivityParameter();
if (sensitivityParameter != null) {
Constant origSensParam = sensitivityParameter;
Constant overriddenSensParam = (Constant) simSymbolTable.getVariable(origSensParam.getName());
for (int i = 0; i < variables.length; i++) {
if (variables[i] instanceof VolVariable) {
VolVariable volVariable = (VolVariable) variables[i];
SensVariable sv = new SensVariable(volVariable, overriddenSensParam);
sensVariables.addElement(sv);
}
}
}
if (rateSensitivity == null) {
rateSensitivity = new RateSensitivity(mathDescription, mathDescription.getSubDomains().nextElement());
}
if (jacobian == null) {
jacobian = new Jacobian(mathDescription, mathDescription.getSubDomains().nextElement());
}
// get Jacobian and RateSensitivities from MathDescription and create SensStateVariables
for (int v = 0; v < sensVariables.size(); v++) {
fieldStateVariables.addElement(new SensStateVariable(sensVariables.elementAt(v), rateSensitivity, jacobian, sensVariables, simSymbolTable));
}
}
use of cbit.vcell.math.Equation in project vcell by virtualcell.
the class MathTestingUtilities method constructOdesForSensitivity.
//
// This method is used to solve for sensitivity of variables to a given parameter.
// The mathDescription and the sensitivity parameter are passed as arguments.
// New variables and ODEs are constructed according to the rule listed below and are added to the mathDescription.
// The method returns the modified mathDescription.
//
public static MathDescription constructOdesForSensitivity(MathDescription mathDesc, Constant sensParam) throws ExpressionException, MathException, MappingException {
//
// For each ODE :
//
// dX/dt = F(X, P)
//
// (where P is the sensitivity parameter)
//
// we create two other ODEs :
//
// dX_1/dt = F(X_1, P_1) and
//
// dX_2/dt = F(X_2, P_2)
//
// where P_1 = P + epsilon, and
// P_2 = P - epsilon.
//
// We keep the initial conditions for both the new ODEs to be the same,
// i.e., X_1_init = X_2_init.
//
// Then, solving for X_1 & X_2, sensitivity of X wrt P can be computed as :
//
// dX = (X_1 - X_2)
// -- ----------- .
// dP (P_1 - P_2)
//
//
// REMOVE PRINTS AFTER CHECKING !!!
System.out.println(" \n\n------------ Old Math Description -----------------");
System.out.println(mathDesc.getVCML_database());
if (mathDesc.getGeometry().getDimension() > 0) {
throw new RuntimeException("Suppport for Spatial systems not yet implemented.");
}
VariableHash varHash = new VariableHash();
Enumeration<Variable> enumVar = mathDesc.getVariables();
while (enumVar.hasMoreElements()) {
varHash.addVariable(enumVar.nextElement());
}
//
// Get 2 values of senstivity parameter (P + epsilon) & (P - epsilon)
//
Constant epsilon = new Constant("epsilon", new Expression(sensParam.getConstantValue() * 1e-3));
Constant sensParam1 = new Constant(sensParam.getName() + "_1", new Expression(sensParam.getConstantValue() + epsilon.getConstantValue()));
Constant sensParam2 = new Constant(sensParam.getName() + "_2", new Expression(sensParam.getConstantValue() - epsilon.getConstantValue()));
//
// Iterate through each subdomain (only 1 in compartmental case), and each equation in the subdomain
//
Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
//
// Create a vector of equations to store the 2 equations for each ODE variable in the subdomain.
// Later, add it to the equations list in the subdomain.
//
Vector<Equation> equnsVector = new Vector<Equation>();
Vector<Variable> varsVector = new Vector<Variable>();
Vector<Variable> var1s = new Vector<Variable>();
Vector<Variable> var2s = new Vector<Variable>();
while (subDomainEnum.hasMoreElements()) {
SubDomain subDomain = subDomainEnum.nextElement();
Enumeration<Equation> equationEnum = subDomain.getEquations();
Domain domain = new Domain(subDomain);
while (equationEnum.hasMoreElements()) {
Equation equation = equationEnum.nextElement();
if (equation instanceof OdeEquation) {
OdeEquation odeEquation = (OdeEquation) equation;
// Similar to substituteWithExactSolutions, to bind and substitute functions in the ODE
Expression substitutedRateExp = substituteFunctions(odeEquation.getRateExpression(), mathDesc);
String varName = odeEquation.getVariable().getName();
VolVariable var = new VolVariable(varName, domain);
varsVector.addElement(var);
//
// Create the variable var1, and get the initExpr and rateExpr from the original ODE.
// Substitute the new vars (var1 and param1) in the old initExpr and rateExpr and create a new ODE
//
String varName1 = new String("__" + varName + "_1");
Expression initExpr1 = odeEquation.getInitialExpression();
Expression rateExpr1 = new Expression(substitutedRateExp);
rateExpr1.substituteInPlace(new Expression(varName), new Expression(varName1));
rateExpr1.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam1.getName()));
VolVariable var1 = new VolVariable(varName1, domain);
var1s.addElement(var1);
OdeEquation odeEqun1 = new OdeEquation(var1, initExpr1, rateExpr1);
equnsVector.addElement(odeEqun1);
//
// Create the variable var2, and get the initExpr and rateExpr from the original ODE.
// Substitute the new vars (var2 and param2) in the old initExpr and rateExpr and create a new ODE
//
String varName2 = new String("__" + varName + "_2");
Expression initExpr2 = odeEquation.getInitialExpression();
Expression rateExpr2 = new Expression(substitutedRateExp);
rateExpr2.substituteInPlace(new Expression(varName), new Expression(varName2));
rateExpr2.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam2.getName()));
VolVariable var2 = new VolVariable(varName2, domain);
var2s.addElement(var2);
OdeEquation odeEqun2 = new OdeEquation(var2, initExpr2, rateExpr2);
equnsVector.addElement(odeEqun2);
//
// Create a function for the sensitivity function expression (X1-X2)/(P1-P2), and save in varHash
//
Expression diffVar = Expression.add(new Expression(var1.getName()), Expression.negate(new Expression(var2.getName())));
Expression diffParam = Expression.add(new Expression(sensParam1.getName()), Expression.negate(new Expression(sensParam2.getName())));
Expression sensitivityExpr = Expression.mult(diffVar, Expression.invert(diffParam));
Function sens_Func = new Function("__sens" + varName + "_wrt_" + sensParam.getName(), sensitivityExpr, domain);
varHash.addVariable(epsilon);
varHash.addVariable(sensParam1);
varHash.addVariable(sensParam2);
varHash.addVariable(var1);
varHash.addVariable(var2);
varHash.addVariable(sens_Func);
} else {
// sensitivity not implemented for PDEs or other equation types.
throw new RuntimeException("SolverTest.constructedExactMath(): equation type " + equation.getClass().getName() + " not yet implemented");
}
}
//
// Need to substitute the new variables in the new ODEs.
// i.e., if Rate Expr for ODE_1 for variable X_1 contains variable Y, variable Z, etc.
// Rate Expr is already substituted with X_1, but it also needs substitute Y with Y_1, Z with Z_1, etc.
// So get the volume variables, from the vectors for vars, var_1s and var_2s
// Substitute the rate expressions for the newly added ODEs in equnsVector.
//
Variable[] vars = (Variable[]) BeanUtils.getArray(varsVector, Variable.class);
Variable[] var_1s = (Variable[]) BeanUtils.getArray(var1s, Variable.class);
Variable[] var_2s = (Variable[]) BeanUtils.getArray(var2s, Variable.class);
Vector<Equation> newEqunsVector = new Vector<Equation>();
for (int i = 0; i < equnsVector.size(); i++) {
Equation equn = equnsVector.elementAt(i);
Expression initEx = equn.getInitialExpression();
Expression rateEx = equn.getRateExpression();
for (int j = 0; j < vars.length; j++) {
if (equn.getVariable().getName().endsWith("_1")) {
rateEx.substituteInPlace(new Expression(vars[j].getName()), new Expression(var_1s[j].getName()));
} else if (equn.getVariable().getName().endsWith("_2")) {
rateEx.substituteInPlace(new Expression(vars[j].getName()), new Expression(var_2s[j].getName()));
}
}
OdeEquation odeEqun = new OdeEquation(equn.getVariable(), initEx, rateEx);
newEqunsVector.addElement(odeEqun);
}
//
for (int i = 0; i < newEqunsVector.size(); i++) {
mathDesc.getSubDomain(subDomain.getName()).addEquation((Equation) newEqunsVector.elementAt(i));
}
//
// FAST SYSTEM
// If the subdomain has a fast system, create a new fast system by substituting the high-low variables/parameters
// in the expressions for the fastInvariants and fastRates and adding them to the fast system.
//
Vector<FastInvariant> invarsVector = new Vector<FastInvariant>();
Vector<FastRate> ratesVector = new Vector<FastRate>();
Enumeration<FastInvariant> fastInvarsEnum = null;
Enumeration<FastRate> fastRatesEnum = null;
// Get the fast invariants and fast rates in the system.
FastSystem fastSystem = subDomain.getFastSystem();
if (fastSystem != null) {
fastInvarsEnum = fastSystem.getFastInvariants();
fastRatesEnum = fastSystem.getFastRates();
//
while (fastInvarsEnum.hasMoreElements()) {
FastInvariant fastInvar = fastInvarsEnum.nextElement();
Expression fastInvarExpr = fastInvar.getFunction();
fastInvarExpr = MathUtilities.substituteFunctions(fastInvarExpr, mathDesc);
Expression fastInvarExpr1 = new Expression(fastInvarExpr);
Expression fastInvarExpr2 = new Expression(fastInvarExpr);
for (int i = 0; i < vars.length; i++) {
fastInvarExpr1.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_1s[i].getName()));
fastInvarExpr2.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_2s[i].getName()));
}
fastInvarExpr1.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam1.getName()));
FastInvariant fastInvar1 = new FastInvariant(fastInvarExpr1);
invarsVector.addElement(fastInvar1);
fastInvarExpr2.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam2.getName()));
FastInvariant fastInvar2 = new FastInvariant(fastInvarExpr2);
invarsVector.addElement(fastInvar2);
}
// Add the newly created fast invariants to the existing list of fast invariants in the fast system.
for (int i = 0; i < invarsVector.size(); i++) {
FastInvariant inVar = (FastInvariant) invarsVector.elementAt(i);
fastSystem.addFastInvariant(inVar);
}
//
while (fastRatesEnum.hasMoreElements()) {
FastRate fastRate = fastRatesEnum.nextElement();
Expression fastRateExpr = fastRate.getFunction();
fastRateExpr = MathUtilities.substituteFunctions(fastRateExpr, mathDesc);
Expression fastRateExpr1 = new Expression(fastRateExpr);
Expression fastRateExpr2 = new Expression(fastRateExpr);
for (int i = 0; i < vars.length; i++) {
fastRateExpr1.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_1s[i].getName()));
fastRateExpr2.substituteInPlace(new Expression(vars[i].getName()), new Expression(var_2s[i].getName()));
}
fastRateExpr1.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam1.getName()));
FastRate fastRate1 = new FastRate(fastRateExpr1);
ratesVector.addElement(fastRate1);
fastRateExpr2.substituteInPlace(new Expression(sensParam.getName()), new Expression(sensParam2.getName()));
FastRate fastRate2 = new FastRate(fastRateExpr2);
ratesVector.addElement(fastRate2);
}
// Add the newly created fast rates to the existing list of fast rates in the fast system.
for (int i = 0; i < ratesVector.size(); i++) {
FastRate rate = (FastRate) ratesVector.elementAt(i);
fastSystem.addFastRate(rate);
}
}
}
// Reset all variables in mathDesc.
mathDesc.setAllVariables(varHash.getAlphabeticallyOrderedVariables());
// REMOVE PRINTS AFTER CHECKING
System.out.println(" \n\n------------ New Math Description -----------------");
System.out.println(mathDesc.getVCML_database());
return mathDesc;
}
use of cbit.vcell.math.Equation in project vcell by virtualcell.
the class ConstructedSolutionTemplate method initialize.
/**
* Insert the method's description here.
* Creation date: (5/12/2003 11:04:13 AM)
* @param mathDesc cbit.vcell.math.MathDescription
*/
private void initialize(cbit.vcell.math.MathDescription mathDesc) {
//
// for all valid combinations of variables/domains ... add a solution template with a default solution.
//
java.util.Vector solutionTemplateList = new java.util.Vector();
Enumeration enumSubDomains = mathDesc.getSubDomains();
while (enumSubDomains.hasMoreElements()) {
SubDomain subDomain = (SubDomain) enumSubDomains.nextElement();
Enumeration enumEquations = subDomain.getEquations();
while (enumEquations.hasMoreElements()) {
Equation equation = (Equation) enumEquations.nextElement();
Variable var = equation.getVariable();
String baseName = " " + var.getName() + "_" + subDomain.getName();
String amplitudeName = baseName + "_A";
String tau1Name = baseName + "_tau1";
String tau2Name = baseName + "_tau2";
if (equation instanceof OdeEquation) {
try {
Expression exp = new Expression(amplitudeName + " * (1.0 + exp(-t/" + tau1Name + ")*sin(2*" + Math.PI + "/" + tau2Name + "*t))");
solutionTemplateList.add(new SolutionTemplate(equation.getVariable().getName(), subDomain.getName(), exp));
} catch (cbit.vcell.parser.ExpressionException e) {
e.printStackTrace(System.out);
throw new RuntimeException(e.getMessage());
}
} else if (equation instanceof PdeEquation) {
try {
Expression exp = new Expression(amplitudeName + " * (1.0 + exp(-t/" + tau1Name + ") + " + tau2Name + "*x)");
solutionTemplateList.add(new SolutionTemplate(equation.getVariable().getName(), subDomain.getName(), exp));
} catch (cbit.vcell.parser.ExpressionException e) {
e.printStackTrace(System.out);
throw new RuntimeException(e.getMessage());
}
}
}
}
this.solutionTemplates = (SolutionTemplate[]) org.vcell.util.BeanUtils.getArray(solutionTemplateList, SolutionTemplate.class);
}
Aggregations