use of cbit.vcell.math.CompartmentSubDomain in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeVariables.
/**
*# Variables : type name unit time_dependent_flag advection_flag solve_whole_mesh_flag solve_regions
*VARIABLE_BEGIN
*VOLUME_ODE rB uM
*VOLUME_PDE rf uM false false
*VOLUME_PDE r uM false false
*VOLUME_ODE rfB uM
*VARIABLE_END
* @throws MathException
* @throws ExpressionException
* @throws IOException
*/
private void writeVariables() throws MathException, ExpressionException, IOException {
SimulationSymbolTable simSymbolTable = simTask.getSimulationJob().getSimulationSymbolTable();
printWriter.println("# Variables : type name domain time_dependent_flag advection_flag grad_flag solve_whole_mesh_flag solve_regions");
printWriter.println(FVInputFileKeyword.VARIABLE_BEGIN);
MathDescription mathDesc = simSymbolTable.getSimulation().getMathDescription();
Variable[] vars = simSymbolTable.getVariables();
ArrayList<RandomVariable> rvList = new ArrayList<RandomVariable>();
for (int i = 0; i < vars.length; i++) {
String varName = vars[i].getName();
String domainName = vars[i].getDomain() == null ? null : vars[i].getDomain().getName();
if (vars[i] instanceof VolumeRandomVariable || vars[i] instanceof MembraneRandomVariable) {
rvList.add((RandomVariable) vars[i]);
} else if (vars[i] instanceof VolVariable) {
if (bChomboSolver && domainName == null) {
throw new MathException(simTask.getSimulation().getSolverTaskDescription().getSolverDescription().getDisplayLabel() + " requires that every variable is defined in a single domain");
}
VolVariable volVar = (VolVariable) vars[i];
if (mathDesc.isPDE(volVar)) {
boolean hasTimeVaryingDiffusionOrAdvection = simSymbolTable.hasTimeVaryingDiffusionOrAdvection(volVar);
final boolean hasVelocity = mathDesc.hasVelocity(volVar);
final boolean hasGradient = mathDesc.hasGradient(volVar);
if (mathDesc.isPdeSteady(volVar)) {
printWriter.print("VOLUME_PDE_STEADY ");
} else {
printWriter.print("VOLUME_PDE ");
}
printWriter.print(varName + " " + domainName + " " + hasTimeVaryingDiffusionOrAdvection + " " + hasVelocity + " " + hasGradient);
} else {
printWriter.print("VOLUME_ODE " + varName + " " + domainName);
}
if (domainName == null) {
Vector<SubDomain> listOfSubDomains = new Vector<SubDomain>();
int totalNumCompartments = 0;
Enumeration<SubDomain> subDomainEnum = mathDesc.getSubDomains();
while (subDomainEnum.hasMoreElements()) {
SubDomain subDomain = subDomainEnum.nextElement();
if (subDomain instanceof CompartmentSubDomain) {
CompartmentSubDomain compartmentSubDomain = (CompartmentSubDomain) subDomain;
totalNumCompartments++;
Equation varEquation = subDomain.getEquation(vars[i]);
if (varEquation != null) {
if (!(varEquation instanceof PdeEquation) || !((PdeEquation) varEquation).isDummy(simSymbolTable, compartmentSubDomain)) {
listOfSubDomains.add(compartmentSubDomain);
}
}
}
}
if ((totalNumCompartments == listOfSubDomains.size()) || (listOfSubDomains.size() == 0 && simTask.getSimulation().getSolverTaskDescription().getSolverDescription().equals(SolverDescription.SundialsPDE))) {
printWriter.print(" true");
} else {
printWriter.print(" false");
for (int j = 0; j < listOfSubDomains.size(); j++) {
CompartmentSubDomain compartmentSubDomain = (CompartmentSubDomain) listOfSubDomains.elementAt(j);
printWriter.print(" " + compartmentSubDomain.getName());
}
}
printWriter.println();
} else {
printWriter.println(" false " + domainName);
}
} else if (vars[i] instanceof VolumeParticleVariable) {
printWriter.println(FVInputFileKeyword.VOLUME_PARTICLE + " " + varName + " " + domainName);
} else if (vars[i] instanceof MembraneParticleVariable) {
printWriter.println(FVInputFileKeyword.MEMBRANE_PARTICLE + " " + varName + " " + domainName);
} else if (vars[i] instanceof VolumeRegionVariable) {
printWriter.println("VOLUME_REGION " + varName + " " + domainName);
} else if (vars[i] instanceof MemVariable) {
if (bChomboSolver && domainName == null) {
throw new MathException(simTask.getSimulation().getSolverTaskDescription().getSolverDescription().getDisplayLabel() + " requires that every variable is defined in a single domain");
}
MemVariable memVar = (MemVariable) vars[i];
if (mathDesc.isPDE(memVar)) {
printWriter.println("MEMBRANE_PDE " + varName + " " + domainName + " " + simSymbolTable.hasTimeVaryingDiffusionOrAdvection(memVar));
} else {
printWriter.println("MEMBRANE_ODE " + varName + " " + domainName);
}
} else if (vars[i] instanceof MembraneRegionVariable) {
printWriter.println("MEMBRANE_REGION " + varName + " " + domainName);
} else if (vars[i] instanceof FilamentVariable) {
throw new RuntimeException("Filament application not supported yet");
}
}
int numRandomVariables = rvList.size();
if (numRandomVariables > 0) {
ISize samplingSize = simTask.getSimulation().getMeshSpecification().getSamplingSize();
String[] varNameArr = new String[numRandomVariables];
VariableType[] varTypeArr = new VariableType[numRandomVariables];
double[][] dataArr = new double[numRandomVariables][];
for (int i = 0; i < numRandomVariables; i++) {
RandomVariable rv = rvList.get(i);
varNameArr[i] = rv.getName();
int numRandomNumbers = 0;
if (rv instanceof VolumeRandomVariable) {
printWriter.print("VOLUME_RANDOM");
varTypeArr[i] = VariableType.VOLUME;
numRandomNumbers = samplingSize.getXYZ();
} else if (rv instanceof MembraneRandomVariable) {
printWriter.print("MEMBRANE_RANDOM");
varTypeArr[i] = VariableType.MEMBRANE;
numRandomNumbers = resampledGeometry.getGeometrySurfaceDescription().getSurfaceCollection().getTotalPolygonCount();
} else {
throw new RuntimeException("Unknown RandomVariable type");
}
printWriter.println(" " + varNameArr[i]);
dataArr[i] = generateRandomNumbers(rv, numRandomNumbers);
}
File rvFile = new File(workingDirectory, simTask.getSimulationJobID() + RANDOM_VARIABLE_FILE_EXTENSION);
DataSet.writeNew(rvFile, varNameArr, varTypeArr, samplingSize, dataArr);
}
printWriter.println(FVInputFileKeyword.VARIABLE_END);
printWriter.println();
}
use of cbit.vcell.math.CompartmentSubDomain in project vcell by virtualcell.
the class MovingBoundaryFileWriter method getXMLSubdomain.
private Element getXMLSubdomain(SubDomain sd) throws ExpressionException, MathException {
Element e = new Element(MBXmlTags.subdomain.name());
e.setAttribute(MBTags.name, sd.getName());
if (sd instanceof CompartmentSubDomain) {
e.setAttribute(MBXmlTags.type.name(), MBXmlTags.volume.name());
} else if (sd instanceof PointSubDomain) {
e.setAttribute(MBXmlTags.type.name(), MBXmlTags.point.name());
Element x = new Element(MBXmlTags.positionX.name());
setExpression(x, ((PointSubDomain) sd).getPositionX(), VariableDomain.VARIABLEDOMAIN_POINT);
e.addContent(x);
Element y = new Element(MBXmlTags.positionY.name());
setExpression(y, ((PointSubDomain) sd).getPositionY(), VariableDomain.VARIABLEDOMAIN_POINT);
e.addContent(y);
}
return e;
}
use of cbit.vcell.math.CompartmentSubDomain in project vcell by virtualcell.
the class MovingBoundaryFileWriter method manageCompartment.
private void manageCompartment(Element e, SubDomain sd) {
try {
if (!sd.getEquationCollection().isEmpty()) {
Element se = getXMLSubdomain(sd);
if (sd instanceof CompartmentSubDomain || sd instanceof PointSubDomain) {
for (Equation equation : sd.getEquationCollection()) {
System.out.println("add this: " + equation.getVariable().getName());
se.addContent(getSpecies(equation));
}
e.addContent(se);
}
}
} catch (Exception exc) {
throw new RuntimeException("error managing compartment", exc);
}
}
use of cbit.vcell.math.CompartmentSubDomain in project vcell by virtualcell.
the class CellQuanVCTranslator method addCompartmentSubDomain.
/**
* addCompartmentSubDomain : we pick the variable name by reading through the mathML.
* Redundancy in variable name with the volume variable?
*/
protected void addCompartmentSubDomain() throws Exception {
String csdName = sRoot.getAttributeValue(CELLMLTags.name, sAttNamespace);
CompartmentSubDomain csd = new CompartmentSubDomain(csdName, CompartmentSubDomain.NON_SPATIAL_PRIORITY);
Iterator<?> compElementIter = sRoot.getChildren(CELLMLTags.COMPONENT, sNamespace).iterator();
// JDOMTreeWalker walker = new JDOMTreeWalker(sRoot, new ElementFilter(CELLMLTags.COMPONENT));
Element comp, math;
String compName, varName, mangledName;
while (compElementIter.hasNext()) {
comp = (Element) compElementIter.next();
compName = comp.getAttributeValue(CELLMLTags.name, sAttNamespace);
@SuppressWarnings("unchecked") Iterator<Element> mathIter = comp.getChildren(CELLMLTags.MATH, mathns).iterator();
while (mathIter.hasNext()) {
math = mathIter.next();
Element apply, apply2, apply3, ci;
// allow multiple 'apply' children.
@SuppressWarnings("unchecked") Iterator<Element> applyIter = math.getChildren(MathMLTags.APPLY, mathns).iterator();
while (applyIter.hasNext()) {
apply = applyIter.next();
@SuppressWarnings("unchecked") ArrayList<Element> list = new ArrayList<Element>(apply.getChildren());
if (list.size() < 3)
continue;
if (!(list.get(0)).getName().equals(MathMLTags.EQUAL))
continue;
apply2 = list.get(1);
if (!apply2.getName().equals(MathMLTags.APPLY))
continue;
@SuppressWarnings("unchecked") ArrayList<Element> list2 = new ArrayList<Element>(apply2.getChildren());
if (list2.size() < 3)
continue;
if (!(list2.get(0)).getName().equals(MathMLTags.DIFFERENTIAL))
continue;
// skip the time variable
ci = list2.get(2);
varName = ci.getTextTrim();
// can be a constant
apply3 = list.get(2);
mangledName = nm.getMangledName(compName, varName);
Element trimmedMath = new Element(CELLMLTags.MATH, mathns).addContent(apply3.detach());
fixMathMLBug(trimmedMath);
Expression rateExp = null;
try {
rateExp = (new ExpressionMathMLParser(null)).fromMathML(trimmedMath);
rateExp = processMathExp(comp, rateExp);
rateExp = rateExp.flatten();
nl.mangleString(rateExp.infix());
} catch (ExpressionException e) {
e.printStackTrace(System.out);
throw new RuntimeException(e.getMessage());
}
Expression initExp = new Expression(getInitial(comp, varName));
Domain domain = null;
OdeEquation ode = new OdeEquation(new VolVariable(mangledName, domain), initExp, rateExp);
csd.addEquation(ode);
}
}
}
mathDescription.addSubDomain(csd);
}
use of cbit.vcell.math.CompartmentSubDomain in project vcell by virtualcell.
the class SimulationData method getChomboFiles.
@Override
public ChomboFiles getChomboFiles() throws IOException, XmlParseException, ExpressionException {
if (chomboFileIterationIndices == null) {
throw new RuntimeException("SimulationData.chomboFileIterationIndices is null, can't process Chombo HDF5 files");
}
if (!(getVcDataId() instanceof VCSimulationDataIdentifier)) {
throw new RuntimeException("SimulationData.getVcDataId() is not a VCSimulationDataIdentifier (type is " + getVcDataId().getClass().getName() + "), can't process chombo HDF5 files");
}
VCSimulationDataIdentifier vcDataID = (VCSimulationDataIdentifier) getVcDataId();
String expectedMeshfile = vcDataID.getID() + ".mesh.hdf5";
File meshFile = amplistorHelper.getFile(expectedMeshfile);
ChomboFiles chomboFiles = new ChomboFiles(vcDataID.getSimulationKey(), vcDataID.getJobIndex(), meshFile);
String simtaskFilePath = vcDataID.getID() + "_0.simtask.xml";
File simtaskFile = amplistorHelper.getFile(simtaskFilePath);
if (!simtaskFile.exists()) {
throw new RuntimeException("Chombo dataset mission .simtask.xml file, please rerun");
}
String xmlString = FileUtils.readFileToString(simtaskFile);
SimulationTask simTask = XmlHelper.XMLToSimTask(xmlString);
if (!simTask.getSimulation().getSolverTaskDescription().getChomboSolverSpec().isSaveChomboOutput() && !simTask.getSimulation().getSolverTaskDescription().isParallel()) {
throw new RuntimeException("Export of Chombo simulations to VTK requires chombo data, select 'Chombo' data format in simulation solver options and rerun simulation.");
}
CartesianMeshChombo chomboMesh = (CartesianMeshChombo) mesh;
FeaturePhaseVol[] featurePhaseVols = chomboMesh.getFeaturePhaseVols();
for (int timeIndex : chomboFileIterationIndices) {
if (featurePhaseVols == null) {
// for old format which doesn't have featurephasevols, we need to try ivol up to 20 for each feature
Enumeration<SubDomain> subdomainEnum = simTask.getSimulation().getMathDescription().getSubDomains();
while (subdomainEnum.hasMoreElements()) {
SubDomain subDomain = subdomainEnum.nextElement();
if (subDomain instanceof CompartmentSubDomain) {
for (int ivol = 0; ivol < 20; ++ivol) {
// can be many vol, let us try 20
findChomboFeatureVolFile(chomboFiles, vcDataID, subDomain.getName(), ivol, timeIndex);
}
}
}
} else {
// note: some feature + ivol doesn't have a file if there are no variables defined in that feature
for (FeaturePhaseVol pfv : featurePhaseVols) {
findChomboFeatureVolFile(chomboFiles, vcDataID, pfv.feature, pfv.ivol, timeIndex);
}
}
}
return chomboFiles;
}
Aggregations