use of cbit.vcell.math.PdeEquation in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeMembrane_VarContext_Equation.
/**
*EQUATION_BEGIN rf
*INITIAL 5.0;
*RATE ( - ((0.02 * ( - rB - rfB + (20.0 * ((x > -3.0) && (x < 3.0) && (y > -5.0) && (y < 5.0))) + _VCell_FieldData_0) * rf) - (0.1 * rfB)) - (50.0 * rf * ((x > -5.0) && (x < 5.0) && (y > -5.0) && (y < 5.0))));
*DIFFUSION 10.0;
*BOUNDARY_XM 5.0;
*BOUNDARY_XP 5.0;
*BOUNDARY_YM 5.0;
*BOUNDARY_YP 5.0;
*EQUATION_END
* @throws ExpressionException
* @throws MathException
*/
private void writeMembrane_VarContext_Equation(MembraneSubDomain memSubDomain, Equation equation) throws ExpressionException, MathException {
printWriter.println("EQUATION_BEGIN " + equation.getVariable().getName());
printWriter.println("INITIAL " + subsituteExpression(equation.getInitialExpression(), VariableDomain.VARIABLEDOMAIN_MEMBRANE).infix() + ";");
Expression rateExpression = subsituteExpression(equation.getRateExpression(), VariableDomain.VARIABLEDOMAIN_MEMBRANE);
printWriter.println("RATE " + replaceVolumeVariable(simTask, memSubDomain, rateExpression) + ";");
if (bChomboSolver && equation.getExactSolution() != null) {
printWriter.println(FVInputFileKeyword.EXACT + " " + subsituteExpression(equation.getExactSolution(), VariableDomain.VARIABLEDOMAIN_VOLUME).infix() + ";");
}
if (equation instanceof PdeEquation) {
printWriter.println("DIFFUSION " + subsituteExpression(((PdeEquation) equation).getDiffusionExpression(), VariableDomain.VARIABLEDOMAIN_MEMBRANE).infix() + ";");
PdeEquation pde = (PdeEquation) equation;
BoundaryConditionType[] bctypes = new BoundaryConditionType[] { memSubDomain.getInsideCompartment().getBoundaryConditionXm(), memSubDomain.getInsideCompartment().getBoundaryConditionXp(), memSubDomain.getInsideCompartment().getBoundaryConditionYm(), memSubDomain.getInsideCompartment().getBoundaryConditionYp(), memSubDomain.getInsideCompartment().getBoundaryConditionZm(), memSubDomain.getInsideCompartment().getBoundaryConditionZp() };
writeBoundaryValues(bctypes, pde, VariableDomain.VARIABLEDOMAIN_MEMBRANE);
}
// if (simulation.getSolverTaskDescription().getSolverDescription().equals(SolverDescription.SundialsPDE)) {
// StringBuffer rateDerivativeString = new StringBuffer();
// Variable[] vars = simulation.getVariables();
// int count = 0;
// for (Variable var : vars) {
// if (var instanceof MemVariable) {
// Expression exp = rateExpression.differentiate(var.getName());
// rateDerivativeString.append(exp.flatten().infix() + ";\n");
// count ++;
// }
// }
// printWriter.println("RATE_DERIVATIVES " + count);
// printWriter.print(rateDerivativeString);
// }
printWriter.println("EQUATION_END");
printWriter.println();
}
use of cbit.vcell.math.PdeEquation in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeCompartment_VarContext.
/**
* Insert the method's description here.
* Creation date: (5/9/2005 2:52:48 PM)
* @throws ExpressionException
*/
private void writeCompartment_VarContext(CompartmentSubDomain volSubDomain) throws ExpressionException {
Simulation simulation = simTask.getSimulation();
//
// get list of volVariables participating in PDEs (anywhere).
//
Vector<VolVariable> pdeVolVariableList = new Vector<VolVariable>();
Variable[] variables = simTask.getSimulationJob().getSimulationSymbolTable().getVariables();
for (int i = 0; i < variables.length; i++) {
if (variables[i] instanceof VolVariable && simulation.getMathDescription().isPDE((VolVariable) variables[i])) {
pdeVolVariableList.add((VolVariable) variables[i]);
}
}
Enumeration<Equation> enum_equ = volSubDomain.getEquations();
while (enum_equ.hasMoreElements()) {
Equation equation = enum_equ.nextElement();
// for chombo solver, only write equations for variables that are defined in this compartment
if (!bChomboSolver || equation.getVariable().getDomain().getName().equals(volSubDomain.getName())) {
if (equation instanceof VolumeRegionEquation) {
writeCompartmentRegion_VarContext_Equation(volSubDomain, (VolumeRegionEquation) equation);
} else if (equation instanceof MeasureEquation) {
throw new RuntimeException("Measure Equation " + equation.getClass().getSimpleName() + " not yet supported in FiniteVolume solvers");
} else {
writeCompartment_VarContext_Equation(volSubDomain, equation);
}
}
if (equation instanceof PdeEquation) {
pdeVolVariableList.remove(equation.getVariable());
}
}
//
if (!bChomboSolver) {
for (int i = 0; i < pdeVolVariableList.size(); i++) {
VolVariable volVar = pdeVolVariableList.elementAt(i);
boolean bSteady = simulation.getMathDescription().isPdeSteady(volVar);
PdeEquation dummyPdeEquation = new PdeEquation(volVar, bSteady, new Expression(0.0), new Expression(0.0), new Expression(0.0));
writeCompartment_VarContext_Equation(volSubDomain, dummyPdeEquation);
}
}
}
use of cbit.vcell.math.PdeEquation in project vcell by virtualcell.
the class ComsolModelBuilder method getVCCModel.
public static VCCModel getVCCModel(SimulationJob vcellSimJob) throws ExpressionException {
MathDescription vcellMathDesc = vcellSimJob.getSimulation().getMathDescription();
Geometry vcellGeometry = vcellMathDesc.getGeometry();
GeometrySpec vcellGeometrySpec = vcellGeometry.getGeometrySpec();
int vcellDim = vcellGeometrySpec.getDimension();
VCCModel model = new VCCModel("Model", vcellDim);
model.modelpath = "D:\\Developer\\eclipse\\workspace_refactor\\comsol_java\\src";
model.comments = "Untitled\n\n";
VCCModelNode comp1 = new VCCModelNode("comp1");
model.modelnodes.add(comp1);
// if (vcellDim != 2){
// throw new RuntimeException("expecting 2D simulation");
// }
//
// assume initial geometry is circle centered at 0.5, 0.5 of radius 0.3
//
// String comsolOutsideDomainName = "dif1";
// String comsolInsideDomainName = "c1";
VCCGeomSequence geom1 = new VCCGeomSequence("geom1", vcellDim);
model.geometrysequences.add(geom1);
VCCMeshSequence mesh1 = new VCCMeshSequence("mesh1", geom1);
model.meshes.add(mesh1);
VCCStudy std1 = new VCCStudy("std1");
model.study = std1;
TimeBounds timeBounds = vcellSimJob.getSimulation().getSolverTaskDescription().getTimeBounds();
TimeStep timeStep = vcellSimJob.getSimulation().getSolverTaskDescription().getTimeStep();
String beginTime = Double.toString(timeBounds.getStartingTime());
String endTime = Double.toString(timeBounds.getEndingTime());
String step = Double.toString(timeStep.getDefaultTimeStep());
VCCStudyFeature time = new VCCTransientStudyFeature("time", beginTime, step, endTime);
std1.features.add(time);
if (vcellGeometrySpec.getImage() != null) {
throw new RuntimeException("image-based geometries not yet supported by VCell's COMSOL model builder");
}
if (vcellGeometrySpec.getNumSubVolumes() == 0) {
throw new RuntimeException("no subvolumes defined in geometry");
}
if (vcellGeometrySpec.getNumAnalyticOrCSGSubVolumes() != vcellGeometrySpec.getNumSubVolumes()) {
throw new RuntimeException("only analytic and CSG subvolumes currently supported by VCell's COMSOL model builder");
}
//
// add geometry for all subvolumes
//
HashMap<String, VCCGeomFeature> subvolumeNameFeatureMap = new HashMap<String, VCCGeomFeature>();
SubVolume[] subVolumes = vcellGeometrySpec.getSubVolumes();
for (int i = 0; i < subVolumes.length; i++) {
SubVolume subvolume = subVolumes[i];
if (subvolume instanceof CSGObject) {
CSGObject vcellCSGObject = (CSGObject) subvolume;
CSGNode vcellCSGNode = vcellCSGObject.getRoot();
ArrayList<VCCGeomFeature> geomFeatureList = new ArrayList<VCCGeomFeature>();
VCCGeomFeature feature = csgVisitor(vcellCSGNode, geomFeatureList, subvolume.getName());
geom1.geomfeatures.addAll(geomFeatureList);
if (i == 0) {
// first subvolume (on top in ordinals) doesn't need any differencing
subvolumeNameFeatureMap.put(subvolume.getName(), feature);
} else {
// have to subtract union of prior subvolumes
ArrayList<VCCGeomFeature> priorFeatures = new ArrayList<VCCGeomFeature>();
for (int j = 0; j < i; j++) {
CSGObject priorCSGObject = (CSGObject) subVolumes[j];
CSGNode priorCSGNode = priorCSGObject.getRoot();
geomFeatureList.clear();
VCCGeomFeature priorFeature = csgVisitor(priorCSGNode, geomFeatureList, subvolume.getName());
priorFeatures.add(priorFeature);
geom1.geomfeatures.addAll(geomFeatureList);
}
VCCDifference diff = new VCCDifference("diff" + subvolume.getName(), Keep.off);
diff.input.add(feature);
diff.input2.addAll(priorFeatures);
geom1.geomfeatures.add(diff);
subvolumeNameFeatureMap.put(subvolume.getName(), diff);
}
} else {
throw new RuntimeException("only CSG subvolumes currently supported by VCell's COMSOL model builder");
}
}
//
// add geometry for all surfaceClasses
//
HashMap<String, VCCGeomFeature> surfaceclassNameFeatureMap = new HashMap<String, VCCGeomFeature>();
SurfaceClass[] surfaceClasses = vcellGeometry.getGeometrySurfaceDescription().getSurfaceClasses();
for (int i = 0; i < surfaceClasses.length; i++) {
SurfaceClass surfaceClass = surfaceClasses[i];
Set<SubVolume> adjacentSubvolumes = surfaceClass.getAdjacentSubvolumes();
if (adjacentSubvolumes.size() != 2) {
throw new RuntimeException("expecting two adjacent subvolumes for surface " + surfaceClass.getName() + " in COMSOL model builder");
}
// find adjacent Geometry Features (for subvolumes)
Iterator<SubVolume> svIter = adjacentSubvolumes.iterator();
SubVolume subvolume0 = svIter.next();
SubVolume subvolume1 = svIter.next();
ArrayList<VCCGeomFeature> adjacentFeatures = new ArrayList<VCCGeomFeature>();
adjacentFeatures.add(subvolumeNameFeatureMap.get(subvolume0.getName()));
adjacentFeatures.add(subvolumeNameFeatureMap.get(subvolume1.getName()));
String name = "inter_" + subvolume0.getName() + "_" + subvolume1.getName();
// surfaces are dimension N-1
int entitydim = vcellDim - 1;
VCCIntersectionSelection intersect_subvolumes = new VCCIntersectionSelection(name, entitydim);
intersect_subvolumes.input.addAll(adjacentFeatures);
geom1.geomfeatures.add(intersect_subvolumes);
surfaceclassNameFeatureMap.put(surfaceClass.getName(), intersect_subvolumes);
}
SimulationSymbolTable symbolTable = new SimulationSymbolTable(vcellSimJob.getSimulation(), vcellSimJob.getJobIndex());
//
for (SubDomain subDomain : Collections.list(vcellMathDesc.getSubDomains())) {
for (Equation equ : subDomain.getEquationCollection()) {
if (equ instanceof PdeEquation || equ instanceof OdeEquation) {
VCCGeomFeature geomFeature = null;
final int dim;
if (subDomain instanceof CompartmentSubDomain) {
geomFeature = subvolumeNameFeatureMap.get(subDomain.getName());
dim = vcellDim;
} else if (subDomain instanceof MembraneSubDomain) {
geomFeature = surfaceclassNameFeatureMap.get(subDomain.getName());
dim = vcellDim - 1;
} else {
throw new RuntimeException("subdomains of type '" + subDomain.getClass().getSimpleName() + "' not yet supported in COMSOL model builder");
}
if (geomFeature == null) {
throw new RuntimeException("cannot find COMSOL geometry feature named " + subDomain.getName() + " in COMSOL model builder");
}
VCCConvectionDiffusionEquation cdeq = new VCCConvectionDiffusionEquation("cdeq_" + equ.getVariable().getName(), geom1, geomFeature, dim);
cdeq.fieldName = equ.getVariable().getName();
cdeq.initial = MathUtilities.substituteModelParameters(equ.getInitialExpression(), symbolTable).flatten().infix();
cdeq.sourceTerm_f = MathUtilities.substituteModelParameters(equ.getRateExpression(), symbolTable).flatten().infix();
if (equ instanceof PdeEquation) {
PdeEquation pde = (PdeEquation) equ;
cdeq.diffTerm_c = MathUtilities.substituteModelParameters(pde.getDiffusionExpression(), symbolTable).flatten().infix();
if (subDomain instanceof CompartmentSubDomain) {
CompartmentSubDomain compartmentSubdomain = (CompartmentSubDomain) subDomain;
ArrayList<String> be = new ArrayList<String>();
if (pde.getVelocityX() != null) {
be.add(MathUtilities.substituteModelParameters(pde.getVelocityX(), symbolTable).flatten().infix());
} else {
be.add("0");
}
if (vcellDim >= 2) {
if (pde.getVelocityY() != null) {
be.add(MathUtilities.substituteModelParameters(pde.getVelocityY(), symbolTable).flatten().infix());
} else {
be.add("0");
}
}
if (vcellDim == 3) {
if (pde.getVelocityY() != null) {
be.add(MathUtilities.substituteModelParameters(pde.getVelocityZ(), symbolTable).flatten().infix());
} else {
be.add("0");
}
}
cdeq.advection_be = be.toArray(new String[vcellDim]);
//
// look for membrane boundary conditions for this variable
//
MembraneSubDomain[] membraneSubdomains = vcellMathDesc.getMembraneSubDomains(compartmentSubdomain);
for (MembraneSubDomain membraneSubdomain : membraneSubdomains) {
JumpCondition jumpCondition = membraneSubdomain.getJumpCondition((VolVariable) pde.getVariable());
if (jumpCondition != null) {
Expression fluxExpr = null;
if (membraneSubdomain.getInsideCompartment() == compartmentSubdomain) {
fluxExpr = jumpCondition.getInFluxExpression();
} else if (membraneSubdomain.getOutsideCompartment() == compartmentSubdomain) {
fluxExpr = jumpCondition.getOutFluxExpression();
}
String name = equ.getVariable().getName() + "_flux_" + membraneSubdomain.getName();
VCCGeomFeature selection = surfaceclassNameFeatureMap.get(membraneSubdomain.getName());
VCCFluxBoundary fluxBoundary = new VCCFluxBoundary(name, selection, vcellDim - 1);
fluxBoundary.flux_g = MathUtilities.substituteModelParameters(fluxExpr, symbolTable).flatten().infix();
cdeq.features.add(fluxBoundary);
}
}
}
}
model.physics.add(cdeq);
}
}
}
//
return model;
}
use of cbit.vcell.math.PdeEquation in project vcell by virtualcell.
the class XmlReader method getPdeEquation.
/**
* This method returns a PdeEquation from a XML element.
* Creation date: (4/26/2001 12:11:14 PM)
* @return cbit.vcell.math.PdeEquation
* @param param org.jdom.Element
* @exception cbit.vcell.xml.XmlParseException The exception description.
*/
private PdeEquation getPdeEquation(Element param, MathDescription mathDesc) throws XmlParseException {
// Retrieve the variable reference
String name = unMangle(param.getAttributeValue(XMLTags.NameAttrTag));
boolean bSteady = false;
String bSteadyAttr = param.getAttributeValue(XMLTags.SteadyTag);
if (bSteadyAttr != null && bSteadyAttr.equals("1")) {
bSteady = true;
}
Variable varref = mathDesc.getVariable(name);
if (varref == null) {
throw new XmlParseException("The variable " + name + " for a PdeEquation, could not be resolved!");
}
PdeEquation pdeEquation = null;
try {
// Retrieve the initial expression
String temp = param.getChildText(XMLTags.InitialTag, vcNamespace);
Expression initialExp = null;
if (temp != null && temp.length() > 0) {
initialExp = unMangleExpression(temp);
}
// Retrieve the Rate Expression
temp = param.getChildText(XMLTags.RateTag, vcNamespace);
Expression rateExp = null;
if (temp != null && temp.length() > 0) {
rateExp = unMangleExpression(temp);
}
// Retrieve the diffusion rate expression
temp = param.getChildText(XMLTags.DiffusionTag, vcNamespace);
Expression difExp = null;
if (temp != null && temp.length() > 0) {
difExp = unMangleExpression(temp);
}
// *** Create new PdeEquation object ****
pdeEquation = new PdeEquation(varref, bSteady, initialExp, rateExp, difExp);
// ***** *****
// add specific solutions expressions
String solType = param.getAttributeValue(XMLTags.SolutionTypeTag);
if (solType.equalsIgnoreCase(XMLTags.ExactTypeTag)) {
String solutionExp = param.getChildText(XMLTags.SolutionExpressionTag, vcNamespace);
if (solutionExp != null && solutionExp.length() > 0) {
Expression expression = unMangleExpression(solutionExp);
pdeEquation.setExactSolution(expression);
}
}
// Retrieve Boudaries (if any)
Element tempelement = param.getChild(XMLTags.BoundariesTag, vcNamespace);
if (tempelement != null) {
Expression newexp = null;
// Xm
temp = tempelement.getAttributeValue(XMLTags.BoundaryAttrValueXm);
if (temp != null) {
newexp = unMangleExpression(temp);
pdeEquation.setBoundaryXm(newexp);
}
// Xp
temp = tempelement.getAttributeValue(XMLTags.BoundaryAttrValueXp);
if (temp != null) {
newexp = unMangleExpression(temp);
pdeEquation.setBoundaryXp(newexp);
}
// Ym
temp = tempelement.getAttributeValue(XMLTags.BoundaryAttrValueYm);
if (temp != null) {
newexp = unMangleExpression(temp);
pdeEquation.setBoundaryYm(newexp);
}
// Yp
temp = tempelement.getAttributeValue(XMLTags.BoundaryAttrValueYp);
if (temp != null) {
newexp = unMangleExpression(temp);
pdeEquation.setBoundaryYp(newexp);
}
// Zm
temp = tempelement.getAttributeValue(XMLTags.BoundaryAttrValueZm);
if (temp != null) {
newexp = unMangleExpression(temp);
pdeEquation.setBoundaryZm(newexp);
}
// Zp
temp = tempelement.getAttributeValue(XMLTags.BoundaryAttrValueZp);
if (temp != null) {
newexp = unMangleExpression(temp);
pdeEquation.setBoundaryZp(newexp);
}
}
// process BoundaryConditionValues
{
Iterator<Element> iterator = param.getChildren(XMLTags.BoundaryConditionValueTag, vcNamespace).iterator();
if (iterator != null) {
while (iterator.hasNext()) {
tempelement = (Element) iterator.next();
try {
pdeEquation.addBoundaryConditionValue(getBoundaryConditionValue(tempelement, pdeEquation));
} catch (MathException e) {
e.printStackTrace();
throw new XmlParseException("A MathException was fired when adding a BoundaryConditionValue to the compartmentSubDomain " + name, e);
}
}
}
}
{
// add Velocity
Element velocityE = param.getChild(XMLTags.VelocityTag, vcNamespace);
if (velocityE != null) {
String tempStr = null;
boolean dummyVel = true;
tempStr = velocityE.getAttributeValue(XMLTags.XAttrTag);
if (tempStr != null) {
// all velocity dimensions are optional.
pdeEquation.setVelocityX(unMangleExpression(tempStr));
if (dummyVel) {
dummyVel = false;
}
}
tempStr = velocityE.getAttributeValue(XMLTags.YAttrTag);
if (tempStr != null) {
pdeEquation.setVelocityY(unMangleExpression(tempStr));
if (dummyVel) {
dummyVel = false;
}
}
tempStr = velocityE.getAttributeValue(XMLTags.ZAttrTag);
if (tempStr != null) {
pdeEquation.setVelocityZ(unMangleExpression(tempStr));
if (dummyVel) {
dummyVel = false;
}
}
if (dummyVel) {
throw new XmlParseException("Void Velocity element found under PDE for: " + name);
}
}
}
{
// add Grad
Element gradElement = param.getChild(XMLTags.GradientTag, vcNamespace);
if (gradElement != null) {
String tempStr = null;
tempStr = gradElement.getAttributeValue(XMLTags.XAttrTag);
if (tempStr != null) {
// all grad dimensions are optional.
pdeEquation.setGradientX(unMangleExpression(tempStr));
}
tempStr = gradElement.getAttributeValue(XMLTags.YAttrTag);
if (tempStr != null) {
pdeEquation.setGradientY(unMangleExpression(tempStr));
}
tempStr = gradElement.getAttributeValue(XMLTags.ZAttrTag);
if (tempStr != null) {
pdeEquation.setGradientZ(unMangleExpression(tempStr));
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new XmlParseException(e);
}
return pdeEquation;
}
use of cbit.vcell.math.PdeEquation in project vcell by virtualcell.
the class MovingBoundaryFileWriter method getSpecies.
// TODO: flatten here
private Element getSpecies(Equation eq) throws ExpressionException, MathException {
Element e = new Element(MBTags.species);
e.setAttribute(MBTags.name, eq.getVariable().getName());
VariableDomain varDomain = null;
if (eq.getVariable() instanceof VolVariable) {
e.setAttribute(MBXmlTags.type.name(), MBXmlTags.volume.name());
varDomain = VariableDomain.VARIABLEDOMAIN_VOLUME;
} else if (eq.getVariable() instanceof PointVariable) {
e.setAttribute(MBXmlTags.type.name(), MBXmlTags.point.name());
varDomain = VariableDomain.VARIABLEDOMAIN_POINT;
}
Element e1 = null;
e1 = new Element(MBTags.initial);
Expression ex = eq.getInitialExpression();
setExpression(e1, ex, varDomain);
e.addContent(e1);
e1 = new Element(MBTags.source);
ex = eq.getRateExpression();
setExpression(e1, ex, varDomain);
e.addContent(e1);
if (eq instanceof PdeEquation) {
e1 = new Element(MBTags.diffusion);
ex = ((PdeEquation) eq).getDiffusionExpression();
setExpression(e1, ex, varDomain);
e.addContent(e1);
ex = ((PdeEquation) eq).getVelocityX();
if (ex != null) {
e1 = new Element(MBTags.advectVelocityFunctionX);
setExpression(e1, ex, varDomain);
e.addContent(e1);
}
ex = ((PdeEquation) eq).getVelocityY();
if (ex != null) {
e1 = new Element(MBTags.advectVelocityFunctionY);
setExpression(e1, ex, varDomain);
e.addContent(e1);
}
}
return e;
}
Aggregations