Search in sources :

Example 36 with CompartmentSubDomain

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();
}
Also used : 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) MembraneRegionVariable(cbit.vcell.math.MembraneRegionVariable) MathDescription(cbit.vcell.math.MathDescription) ISize(org.vcell.util.ISize) ArrayList(java.util.ArrayList) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) RandomVariable(cbit.vcell.math.RandomVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) MembraneRandomVariable(cbit.vcell.math.MembraneRandomVariable) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) SubDomain(cbit.vcell.math.SubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) PdeEquation(cbit.vcell.math.PdeEquation) VolumeRegionVariable(cbit.vcell.math.VolumeRegionVariable) MemVariable(cbit.vcell.math.MemVariable) MembraneParticleVariable(cbit.vcell.math.MembraneParticleVariable) Vector(java.util.Vector) Enumeration(java.util.Enumeration) VariableType(cbit.vcell.math.VariableType) VolVariable(cbit.vcell.math.VolVariable) SimulationSymbolTable(cbit.vcell.solver.SimulationSymbolTable) MeasureEquation(cbit.vcell.math.MeasureEquation) PdeEquation(cbit.vcell.math.PdeEquation) VolumeRegionEquation(cbit.vcell.math.VolumeRegionEquation) MembraneRegionEquation(cbit.vcell.math.MembraneRegionEquation) Equation(cbit.vcell.math.Equation) MathException(cbit.vcell.math.MathException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) VolumeParticleVariable(cbit.vcell.math.VolumeParticleVariable) VolumeRandomVariable(cbit.vcell.math.VolumeRandomVariable) FilamentVariable(cbit.vcell.math.FilamentVariable) File(java.io.File)

Example 37 with CompartmentSubDomain

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;
}
Also used : CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Element(org.jdom.Element) PointSubDomain(cbit.vcell.math.PointSubDomain)

Example 38 with CompartmentSubDomain

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);
    }
}
Also used : CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Element(org.jdom.Element) PointSubDomain(cbit.vcell.math.PointSubDomain) PdeEquation(cbit.vcell.math.PdeEquation) Equation(cbit.vcell.math.Equation) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException)

Example 39 with CompartmentSubDomain

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);
}
Also used : VolVariable(cbit.vcell.math.VolVariable) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ExpressionMathMLParser(cbit.vcell.parser.ExpressionMathMLParser) ExpressionException(cbit.vcell.parser.ExpressionException) OdeEquation(cbit.vcell.math.OdeEquation) Expression(cbit.vcell.parser.Expression) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) Domain(cbit.vcell.math.Variable.Domain)

Example 40 with CompartmentSubDomain

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;
}
Also used : SimulationTask(cbit.vcell.messaging.server.SimulationTask) FeaturePhaseVol(cbit.vcell.solvers.CartesianMeshChombo.FeaturePhaseVol) VCSimulationDataIdentifier(cbit.vcell.solver.VCSimulationDataIdentifier) CartesianMeshChombo(cbit.vcell.solvers.CartesianMeshChombo) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) FilamentSubDomain(cbit.vcell.math.FilamentSubDomain) SubDomain(cbit.vcell.math.SubDomain) PointSubDomain(cbit.vcell.math.PointSubDomain) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) ChomboFiles(org.vcell.vis.io.ChomboFiles) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)45 MembraneSubDomain (cbit.vcell.math.MembraneSubDomain)29 SubDomain (cbit.vcell.math.SubDomain)24 Expression (cbit.vcell.parser.Expression)21 MathDescription (cbit.vcell.math.MathDescription)19 ExpressionException (cbit.vcell.parser.ExpressionException)17 SubVolume (cbit.vcell.geometry.SubVolume)16 MathException (cbit.vcell.math.MathException)16 Variable (cbit.vcell.math.Variable)16 ArrayList (java.util.ArrayList)15 Equation (cbit.vcell.math.Equation)12 VolVariable (cbit.vcell.math.VolVariable)11 Constant (cbit.vcell.math.Constant)10 SurfaceClass (cbit.vcell.geometry.SurfaceClass)9 OdeEquation (cbit.vcell.math.OdeEquation)9 PdeEquation (cbit.vcell.math.PdeEquation)9 SolverException (cbit.vcell.solver.SolverException)9 Function (cbit.vcell.math.Function)8 MemVariable (cbit.vcell.math.MemVariable)8 PropertyVetoException (java.beans.PropertyVetoException)8