use of cbit.vcell.math.MathException in project vcell by virtualcell.
the class XmlReader method addResevedSymbols.
/**
* This method returns a Kinetics object from a XML Element based on the value of the kinetics type attribute.
* Creation date: (3/19/2001 4:42:04 PM)
* @return cbit.vcell.model.Kinetics
* @param param org.jdom.Element
*/
private void addResevedSymbols(VariableHash varHash, Model model) throws XmlParseException {
//
try {
// add reserved symbols
varHash.addVariable(new Constant(model.getPI_CONSTANT().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getFARADAY_CONSTANT().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getFARADAY_CONSTANT_NMOLE().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getGAS_CONSTANT().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getKMILLIVOLTS().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getN_PMOLE().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getKMOLE().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getTEMPERATURE().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getK_GHK().getName(), new Expression(0.0)));
varHash.addVariable(new Constant(model.getTIME().getName(), new Expression(0.0)));
} catch (MathException e) {
e.printStackTrace(System.out);
throw new XmlParseException("error reordering parameters according to dependencies", e);
}
}
use of cbit.vcell.math.MathException in project vcell by virtualcell.
the class MergedData method getSimDataBlock.
/**
* This method was created in VisualAge.
* @return cbit.vcell.simdata.DataBlock
* @param user cbit.vcell.server.User
* @param simID java.lang.String
*/
public SimDataBlock getSimDataBlock(OutputContext outputContext, String varName, double time) throws DataAccessException, IOException {
VCDataIdentifier vcDataID = getVCDataIdentifierFromDataId(varName);
if (vcDataID == null) {
return null;
}
DataSetIdentifier varDatasetID = getDataSetIdentifier(varName);
int actualVarNameIndx = varName.indexOf(".");
String actualVarName = varName.substring(actualVarNameIndx + 1);
SimDataBlock simDataBlk = null;
//
if (vcDataID.getID().equals(datasetsIDList[0].getID())) {
simDataBlk = getDatasetControllerImpl().getSimDataBlock(outputContext, vcDataID, actualVarName, time);
return simDataBlk;
} else {
// TIME INTERPOLATION of datablock
double[] timeArray = getDatasetControllerImpl().getDataSetTimes(vcDataID);
boolean bTimesEqual = checkTimeArrays(timeArray);
double[] timeResampledData = null;
int timeArrayCounter = 0;
long lastModified = Long.MIN_VALUE;
if (bTimesEqual) {
// If time arrays for both datasets are equal, no need to resample/interpolate in time, just obtain the datablock
simDataBlk = getDatasetControllerImpl().getSimDataBlock(outputContext, vcDataID, actualVarName, time);
timeResampledData = simDataBlk.getData();
} else {
while ((timeArrayCounter < timeArray.length - 2) && (time > timeArray[timeArrayCounter + 1])) {
timeArrayCounter++;
}
SimDataBlock simDataBlock_1 = getDatasetControllerImpl().getSimDataBlock(outputContext, vcDataID, actualVarName, timeArray[timeArrayCounter]);
double[] data_1 = simDataBlock_1.getData();
if ((timeArrayCounter + 1) < (timeArray.length - 1)) {
SimDataBlock simDataBlock_2 = getDatasetControllerImpl().getSimDataBlock(outputContext, vcDataID, actualVarName, timeArray[timeArrayCounter + 1]);
lastModified = simDataBlock_2.getPDEDataInfo().getTimeStamp();
double[] data_2 = simDataBlock_2.getData();
timeResampledData = new double[data_1.length];
//
for (int m = 0; m < data_1.length; m++) {
timeResampledData[m] = data_1[m] + (data_2[m] - data_1[m]) * (time - timeArray[timeArrayCounter]) / (timeArray[timeArrayCounter + 1] - timeArray[timeArrayCounter]);
}
} else {
// past end of array, zero order interpolation
lastModified = simDataBlock_1.getPDEDataInfo().getTimeStamp();
timeResampledData = data_1;
}
}
// SPATIAL INTERPOLATION
CartesianMesh refMesh = null;
CartesianMesh mesh = null;
try {
refMesh = getDatasetControllerImpl().getMesh(datasetsIDList[0]);
mesh = getDatasetControllerImpl().getMesh(vcDataID);
} catch (MathException e) {
e.printStackTrace(System.out);
throw new RuntimeException("Could not get Mesh for reference or given dataID");
}
double[] spaceResampledData = null;
// Check origin and extent of reference and given dataset. If they don't match, cannot resample spatially
if (!mesh.getExtent().compareEqual(refMesh.getExtent()) || !mesh.getOrigin().compareEqual(refMesh.getOrigin())) {
throw new RuntimeException("Different origins and/or extents for the 2 geometries. Cannot compare the 2 simulations");
}
// Get dimension of geometry for reference and given datasets (using mesh variables!) to use appropriate
// resampling algorithm. Check if mesh sizes for the 2 datasets are equal, then there is no need to
// spatially resample the second dataset.
int dimension = mesh.getGeometryDimension();
int refDimension = refMesh.getGeometryDimension();
if (mesh.getSizeX() != refMesh.getSizeX() || mesh.getSizeY() != refMesh.getSizeY() || mesh.getSizeZ() != refMesh.getSizeZ()) {
if (varDatasetID.getVariableType().equals(VariableType.VOLUME)) {
if (dimension == 1 && refDimension == 1) {
spaceResampledData = MathTestingUtilities.resample1DSpatial(timeResampledData, mesh, refMesh);
} else if (dimension == 2 && refDimension == 2) {
spaceResampledData = MathTestingUtilities.resample2DSpatial(timeResampledData, mesh, refMesh);
} else if (dimension == 3 && refDimension == 3) {
spaceResampledData = MathTestingUtilities.resample3DSpatial(timeResampledData, mesh, refMesh);
} else {
throw new RuntimeException("Comparison of 2 simulations with different geometry dimensions are not handled at this time!");
}
} else {
throw new RuntimeException("spatial resampling for variable type: " + varDatasetID.getVariableType().getTypeName() + " not supported");
}
} else {
//
if (varDatasetID.getVariableType().equals(VariableType.MEMBRANE)) {
//
if (membraneIndexMapping == null) {
membraneIndexMapping = mesh.getMembraneIndexMapping(refMesh);
}
spaceResampledData = new double[timeResampledData.length];
for (int i = 0; i < timeResampledData.length; i++) {
spaceResampledData[i] = timeResampledData[membraneIndexMapping[i]];
}
} else {
//
// no reordering needed for other variable types.
//
spaceResampledData = timeResampledData;
}
}
if (simDataBlk != null) {
lastModified = simDataBlk.getPDEDataInfo().getTimeStamp();
}
PDEDataInfo pdeDataInfo = new PDEDataInfo(vcDataID.getOwner(), vcDataID.getID(), varName, time, lastModified);
if (spaceResampledData != null) {
return new SimDataBlock(pdeDataInfo, spaceResampledData, varDatasetID.getVariableType());
} else {
return null;
}
}
}
use of cbit.vcell.math.MathException in project vcell by virtualcell.
the class DefaultODESolver method createStateVariables.
/**
* This method was created in VisualAge.
*/
private Vector<StateVariable> createStateVariables() throws MathException, ExpressionException {
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
Simulation sim = simSymbolTable.getSimulation();
Vector<StateVariable> stateVariables = new Vector<StateVariable>();
// get Ode's from MathDescription and create ODEStateVariables
Enumeration<Equation> enum1 = getSubDomain().getEquations();
while (enum1.hasMoreElements()) {
Equation equation = enum1.nextElement();
if (equation instanceof OdeEquation) {
stateVariables.addElement(new ODEStateVariable((OdeEquation) equation, simSymbolTable));
} else {
throw new MathException("encountered non-ode equation, unsupported");
}
}
MathDescription mathDescription = sim.getMathDescription();
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 < fieldSensVariables.size(); v++) {
stateVariables.addElement(new SensStateVariable(fieldSensVariables.elementAt(v), rateSensitivity, jacobian, fieldSensVariables, simSymbolTable));
}
if (stateVariables.size() == 0) {
throw new MathException("there are no equations defined");
}
return (stateVariables);
}
use of cbit.vcell.math.MathException in project vcell by virtualcell.
the class DefaultODESolver method integrate.
/**
* This method was created by a SmartGuide.
*/
protected void integrate() throws SolverException, UserStopException, IOException {
try {
SolverTaskDescription taskDescription = simTask.getSimulation().getSolverTaskDescription();
double timeStep = taskDescription.getTimeStep().getDefaultTimeStep();
fieldCurrentTime = taskDescription.getTimeBounds().getStartingTime();
// before computation begins, settle fast equilibrium
if (getFastAlgebraicSystem() != null) {
fieldValueVectors.copyValues(0, 1);
getFastAlgebraicSystem().initVars(getValueVector(0), getValueVector(1));
getFastAlgebraicSystem().solveSystem(getValueVector(0), getValueVector(1));
fieldValueVectors.copyValues(1, 0);
}
// check for failure
check(getValueVector(0));
updateResultSet();
//
int iteration = 0;
while (fieldCurrentTime < taskDescription.getTimeBounds().getEndingTime()) {
checkForUserStop();
step(fieldCurrentTime, timeStep);
// update (old = new)
fieldValueVectors.copyValuesDown();
// compute fast system
if (getFastAlgebraicSystem() != null) {
fieldValueVectors.copyValues(0, 1);
getFastAlgebraicSystem().initVars(getValueVector(0), getValueVector(1));
getFastAlgebraicSystem().solveSystem(getValueVector(0), getValueVector(1));
fieldValueVectors.copyValues(1, 0);
}
// check for failure
check(getValueVector(0));
// fieldCurrentTime += timeStep;
iteration++;
fieldCurrentTime = taskDescription.getTimeBounds().getStartingTime() + iteration * timeStep;
// Print results if it coincides with a save interval...
if (taskDescription.getOutputTimeSpec().isDefault()) {
int keepEvery = ((DefaultOutputTimeSpec) taskDescription.getOutputTimeSpec()).getKeepEvery();
if ((iteration % keepEvery) == 0) {
updateResultSet();
}
}
}
// store last time point
if (taskDescription.getOutputTimeSpec().isDefault()) {
int keepEvery = ((DefaultOutputTimeSpec) taskDescription.getOutputTimeSpec()).getKeepEvery();
if ((iteration % keepEvery) != 0)
updateResultSet();
}
} catch (ExpressionException | MathException e) {
throw new SolverException("Solver failed: " + e.getMessage(), e);
}
}
use of cbit.vcell.math.MathException in project vcell by virtualcell.
the class RateSensitivity 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 constantList = new Vector();
enum1 = mathDesc.getVariables();
while (enum1.hasMoreElements()) {
Variable var = (Variable) enum1.nextElement();
if (var instanceof Constant) {
constantList.addElement(var);
}
}
numConstants = constantList.size();
numRates = equationList.size();
rates = new Expression[numRates];
vars = new Variable[numRates];
consts = new Constant[numConstants];
for (int i = 0; i < numRates; i++) {
OdeEquation odeEqu = (OdeEquation) equationList.elementAt(i);
rates[i] = odeEqu.getRateExpression();
vars[i] = odeEqu.getVariable();
}
for (int i = 0; i < numConstants; i++) {
consts[i] = (Constant) constantList.elementAt(i);
}
}
Aggregations