Search in sources :

Example 76 with MathException

use of cbit.vcell.math.MathException in project vcell by virtualcell.

the class SmoldynFileWriter method writeInitialConcentration.

private int writeInitialConcentration(ParticleInitialConditionConcentration initialConcentration, SubDomain subDomain, Variable variable, String variableName, StringBuilder sb) throws ExpressionException, MathException {
    SimpleSymbolTable simpleSymbolTable = new SimpleSymbolTable(new String[] { ReservedVariable.X.getName(), ReservedVariable.Y.getName(), ReservedVariable.Z.getName() });
    Expression disExpression = new Expression(initialConcentration.getDistribution());
    disExpression.bindExpression(simulationSymbolTable);
    disExpression = simulationSymbolTable.substituteFunctions(disExpression).flatten();
    disExpression.bindExpression(simpleSymbolTable);
    double[] values = new double[3];
    if (dimension == 1) {
        if (disExpression.getSymbolBinding(ReservedVariable.Y.getName()) != null || disExpression.getSymbolBinding(ReservedVariable.Z.getName()) != null) {
            throw new MathException(VCellErrorMessages.getSmoldynWrongCoordinates("'y' or 'z'", dimension, variable, disExpression));
        }
    } else if (dimension == 2) {
        if (disExpression.getSymbolBinding(ReservedVariable.Z.getName()) != null) {
            throw new MathException(VCellErrorMessages.getSmoldynWrongCoordinates("'z'", dimension, variable, disExpression));
        }
    }
    int totalCount = 0;
    StringBuilder localsb = new StringBuilder();
    if (subDomain instanceof CompartmentSubDomain) {
        MeshSpecification meshSpecification = simulation.getMeshSpecification();
        ISize sampleSize = meshSpecification.getSamplingSize();
        int numX = sampleSize.getX();
        int numY = dimension < 2 ? 1 : sampleSize.getY();
        int numZ = dimension < 3 ? 1 : sampleSize.getZ();
        boolean bCellCentered = simulation.hasCellCenteredMesh();
        double dx = meshSpecification.getDx(bCellCentered);
        double dy = meshSpecification.getDy(bCellCentered);
        double dz = meshSpecification.getDz(bCellCentered);
        Origin origin = resampledGeometry.getGeometrySpec().getOrigin();
        double ox = origin.getX();
        double oy = origin.getY();
        double oz = origin.getZ();
        Extent extent = resampledGeometry.getExtent();
        double ex = extent.getX();
        double ey = extent.getY();
        double ez = extent.getZ();
        int offset = 0;
        for (int k = 0; k < numZ; k++) {
            double centerz = oz + k * dz;
            double loz = Math.max(oz, centerz - dz / 2);
            double hiz = Math.min(oz + ez, centerz + dz / 2);
            double lz = hiz - loz;
            values[2] = centerz;
            for (int j = 0; j < numY; j++) {
                double centery = oy + j * dy;
                double loy = Math.max(oy, centery - dy / 2);
                double hiy = Math.min(oy + ey, centery + dy / 2);
                values[1] = centery;
                double ly = hiy - loy;
                for (int i = 0; i < numX; i++) {
                    int regionIndex = resampledGeometry.getGeometrySurfaceDescription().getRegionImage().getRegionInfoFromOffset(offset).getRegionIndex();
                    offset++;
                    GeometricRegion region = resampledGeometry.getGeometrySurfaceDescription().getGeometricRegions(regionIndex);
                    if (region instanceof VolumeGeometricRegion) {
                        if (!((VolumeGeometricRegion) region).getSubVolume().getName().equals(subDomain.getName())) {
                            continue;
                        }
                    }
                    double centerx = ox + i * dx;
                    double lox = Math.max(ox, centerx - dx / 2);
                    double hix = Math.min(ox + ex, centerx + dx / 2);
                    double lx = hix - lox;
                    values[0] = centerx;
                    double volume = lx;
                    if (dimension > 1) {
                        volume *= ly;
                        if (dimension > 2) {
                            volume *= lz;
                        }
                    }
                    double expectedCount = disExpression.evaluateVector(values) * volume;
                    if (expectedCount <= 0) {
                        continue;
                    }
                    long count = dist.nextPoisson(expectedCount);
                    if (count <= 0) {
                        continue;
                    }
                    totalCount += count;
                    localsb.append(SmoldynVCellMapper.SmoldynKeyword.mol + " " + count + " " + variableName + " " + (float) lox + "-" + (float) hix);
                    if (lg.isDebugEnabled()) {
                        lg.debug("Component subdomain " + variableName + " count " + count);
                    }
                    if (dimension > 1) {
                        localsb.append(" " + loy + "-" + hiy);
                        if (dimension > 2) {
                            localsb.append(" " + loz + "-" + hiz);
                        }
                    }
                    localsb.append("\n");
                }
            }
        }
        // otherwise we append the distributed molecules in different small boxes
        try {
            subsituteFlattenToConstant(disExpression);
            sb.append(SmoldynVCellMapper.SmoldynKeyword.compartment_mol);
            sb.append(" " + totalCount + " " + variableName + " " + subDomain.getName() + "\n");
        } catch (// can not be evaluated to a constant
        Exception e) {
            sb.append(localsb);
        }
    } else if (subDomain instanceof MembraneSubDomain) {
        ArrayList<TrianglePanel> trianglePanelList = membraneSubdomainTriangleMap.get(subDomain);
        for (TrianglePanel trianglePanel : trianglePanelList) {
            Triangle triangle = trianglePanel.triangle;
            switch(dimension) {
                case 1:
                    values[0] = triangle.getNodes(0).getX();
                    break;
                case 2:
                    {
                        double centroidX = triangle.getNodes(0).getX();
                        double centroidY = triangle.getNodes(0).getY();
                        if (triangle.getNodes(0).getX() == triangle.getNodes(1).getX() && triangle.getNodes(0).getY() == triangle.getNodes(1).getY()) {
                            centroidX += triangle.getNodes(2).getX();
                            centroidY += triangle.getNodes(2).getY();
                        } else {
                            centroidX += triangle.getNodes(1).getX();
                            centroidY += triangle.getNodes(1).getY();
                        }
                        values[0] = centroidX / 2;
                        values[1] = centroidY / 2;
                        break;
                    }
                case 3:
                    {
                        double centroidX = triangle.getNodes(0).getX() + triangle.getNodes(1).getX() + triangle.getNodes(2).getX();
                        double centroidY = triangle.getNodes(0).getY() + triangle.getNodes(1).getY() + triangle.getNodes(2).getY();
                        double centroidZ = triangle.getNodes(0).getZ() + triangle.getNodes(1).getZ() + triangle.getNodes(2).getZ();
                        values[0] = centroidX / 3;
                        values[1] = centroidY / 3;
                        values[2] = centroidZ / 3;
                        break;
                    }
            }
            double expectedCount = disExpression.evaluateVector(values) * triangle.getArea();
            if (expectedCount <= 0) {
                continue;
            }
            long count = dist.nextPoisson(expectedCount);
            if (count <= 0) {
                continue;
            }
            totalCount += count;
            if (lg.isDebugEnabled()) {
                lg.debug("Membrane subdomain " + subDomain.getName() + ' ' + variableName + " count " + count);
            }
            localsb.append(SmoldynVCellMapper.SmoldynKeyword.surface_mol + " " + count + " " + variableName + " " + subDomain.getName() + " " + SmoldynVCellMapper.SmoldynKeyword.tri + " " + trianglePanel.name + "\n");
        }
        // otherwise we append the distributed molecules in different small boxes
        try {
            subsituteFlattenToConstant(disExpression);
            sb.append(SmoldynVCellMapper.SmoldynKeyword.surface_mol);
            sb.append(" " + totalCount + " " + variableName + " " + subDomain.getName() + " " + SmoldynVCellMapper.SmoldynKeyword.all + " " + SmoldynVCellMapper.SmoldynKeyword.all + "\n");
        } catch (// can not be evaluated to a constant
        Exception e) {
            sb.append(localsb);
        }
    }
    if (lg.isDebugEnabled()) {
        lg.debug("Subdomain " + subDomain.getName() + ' ' + variableName + " total count " + totalCount);
    }
    return totalCount;
}
Also used : Origin(org.vcell.util.Origin) MembraneSubDomain(cbit.vcell.math.MembraneSubDomain) Extent(org.vcell.util.Extent) ISize(org.vcell.util.ISize) ArrayList(java.util.ArrayList) Triangle(cbit.vcell.geometry.surface.Triangle) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) MeshSpecification(cbit.vcell.solver.MeshSpecification) ProgrammingException(org.vcell.util.ProgrammingException) GeometryException(cbit.vcell.geometry.GeometryException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) PropertyVetoException(java.beans.PropertyVetoException) DivideByZeroException(cbit.vcell.parser.DivideByZeroException) ImageException(cbit.image.ImageException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) SolverException(cbit.vcell.solver.SolverException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) GeometricRegion(cbit.vcell.geometry.surface.GeometricRegion) SimpleSymbolTable(cbit.vcell.parser.SimpleSymbolTable) Expression(cbit.vcell.parser.Expression) MathException(cbit.vcell.math.MathException) CompartmentSubDomain(cbit.vcell.math.CompartmentSubDomain)

Example 77 with MathException

use of cbit.vcell.math.MathException in project vcell by virtualcell.

the class CartesianMeshFileReader method readFromFiles.

public CartesianMesh readFromFiles(VCellSimFiles vcellSimFiles) throws IOException, MathException {
    // 
    // read meshFile and parse into 'mesh' object
    // 
    BufferedReader meshReader = null;
    BufferedReader meshMetricsReader = null;
    try {
        meshReader = new BufferedReader(new FileReader(vcellSimFiles.cartesianMeshFile));
        CommentStringTokenizer meshST = new CommentStringTokenizer(meshReader);
        CommentStringTokenizer membraneMeshMetricsST = null;
        if (vcellSimFiles.meshMetricsFile != null) {
            meshMetricsReader = new BufferedReader(new FileReader(vcellSimFiles.meshMetricsFile));
            membraneMeshMetricsST = new CommentStringTokenizer(meshMetricsReader);
        }
        MembraneMeshMetrics membraneMeshMetrics = null;
        SubdomainInfo subdomainInfo = null;
        if (membraneMeshMetricsST != null) {
            membraneMeshMetrics = readMembraneMeshMetrics(membraneMeshMetricsST);
        }
        if (vcellSimFiles.subdomainFile != null) {
            subdomainInfo = SubdomainInfo.read(vcellSimFiles.subdomainFile);
        }
        CartesianMesh mesh = readCartesianMesh(meshST, membraneMeshMetrics, subdomainInfo);
        return mesh;
    } finally {
        if (meshReader != null) {
            try {
                meshReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (meshMetricsReader != null) {
            try {
                meshMetricsReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : CartesianMesh(org.vcell.vis.vcell.CartesianMesh) MembraneMeshMetrics(org.vcell.vis.vcell.MembraneMeshMetrics) BufferedReader(java.io.BufferedReader) CommentStringTokenizer(org.vcell.util.CommentStringTokenizer) FileReader(java.io.FileReader) SubdomainInfo(org.vcell.vis.vcell.SubdomainInfo) IOException(java.io.IOException) MathFormatException(cbit.vcell.math.MathFormatException) MathException(cbit.vcell.math.MathException)

Example 78 with MathException

use of cbit.vcell.math.MathException in project vcell by virtualcell.

the class ChomboVtkFileWriter method writeEmptyMeshFiles.

public File[] writeEmptyMeshFiles(ChomboFiles chomboFiles, File destinationDirectory, ProgressListener progressListener) throws IOException, MathException, DataAccessException {
    ArrayList<File> meshFiles = new ArrayList<File>();
    int timeIndex = 0;
    HashMap<String, VisMesh> domainMeshMap = new HashMap<String, VisMesh>();
    ChomboDataset chomboDataset;
    try {
        chomboDataset = ChomboFileReader.readDataset(chomboFiles, chomboFiles.getTimeIndices().get(timeIndex));
    } catch (Exception e) {
        throw new DataAccessException("failed to read Chombo Dataset: " + e.getMessage(), e);
    }
    for (ChomboCombinedVolumeMembraneDomain chomboCombinedVolumeMembraneDomain : chomboDataset.getCombinedVolumeMembraneDomains()) {
        ChomboMeshData chomboMeshData = chomboCombinedVolumeMembraneDomain.getChomboMeshData();
        ChomboMeshMapping chomboMeshMapping = new ChomboMeshMapping();
        VisMesh visMesh = domainMeshMap.get(chomboCombinedVolumeMembraneDomain.getVolumeDomainName());
        if (visMesh == null) {
            visMesh = chomboMeshMapping.fromMeshData(chomboMeshData, chomboCombinedVolumeMembraneDomain);
            domainMeshMap.put(chomboCombinedVolumeMembraneDomain.getVolumeDomainName(), visMesh);
        }
        String volumeDomainName = chomboCombinedVolumeMembraneDomain.getVolumeDomainName();
        // 
        // write volume mesh file
        // 
        {
            File volumeMeshFile = getVtuMeshFileName(destinationDirectory, chomboFiles, volumeDomainName);
            File chomboIndexDataFile = getChomboIndexDataFileName(destinationDirectory, chomboFiles, volumeDomainName);
            VtkService.getInstance().writeChomboVolumeVtkGridAndIndexData(visMesh, volumeDomainName, volumeMeshFile, chomboIndexDataFile);
            meshFiles.add(volumeMeshFile);
        }
        if (chomboMeshData.getMembraneVarData().size() > 0) {
            // 
            // write membrane mesh file
            // 
            String membraneDomainName = chomboCombinedVolumeMembraneDomain.getMembraneDomainName();
            File membraneMeshFile = getVtuMeshFileName(destinationDirectory, chomboFiles, membraneDomainName);
            File membraneIndexDataFile = getChomboIndexDataFileName(destinationDirectory, chomboFiles, membraneDomainName);
            VtkService.getInstance().writeChomboMembraneVtkGridAndIndexData(visMesh, membraneDomainName, membraneMeshFile, membraneIndexDataFile);
            meshFiles.add(membraneMeshFile);
        }
    }
    return meshFiles.toArray(new File[0]);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) DivideByZeroException(cbit.vcell.parser.DivideByZeroException) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) ExpressionBindingException(cbit.vcell.parser.ExpressionBindingException) MathException(cbit.vcell.math.MathException) VisMesh(org.vcell.vis.vismesh.thrift.VisMesh) ChomboCombinedVolumeMembraneDomain(org.vcell.vis.chombo.ChomboDataset.ChomboCombinedVolumeMembraneDomain) ChomboMeshData(org.vcell.vis.chombo.ChomboMeshData) File(java.io.File) DataAccessException(org.vcell.util.DataAccessException) ChomboDataset(org.vcell.vis.chombo.ChomboDataset)

Aggregations

MathException (cbit.vcell.math.MathException)78 ExpressionException (cbit.vcell.parser.ExpressionException)46 Expression (cbit.vcell.parser.Expression)38 Variable (cbit.vcell.math.Variable)33 VolVariable (cbit.vcell.math.VolVariable)26 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)18 MemVariable (cbit.vcell.math.MemVariable)18 PropertyVetoException (java.beans.PropertyVetoException)18 DataAccessException (org.vcell.util.DataAccessException)16 MathDescription (cbit.vcell.math.MathDescription)15 Vector (java.util.Vector)15 Element (org.jdom.Element)15 IOException (java.io.IOException)14 Function (cbit.vcell.math.Function)13 SimulationSymbolTable (cbit.vcell.solver.SimulationSymbolTable)13 MembraneParticleVariable (cbit.vcell.math.MembraneParticleVariable)12 ParticleVariable (cbit.vcell.math.ParticleVariable)12 VolumeParticleVariable (cbit.vcell.math.VolumeParticleVariable)12 ArrayList (java.util.ArrayList)12 MappingException (cbit.vcell.mapping.MappingException)11