Search in sources :

Example 11 with GeometricRegion

use of cbit.vcell.geometry.surface.GeometricRegion in project vcell by virtualcell.

the class TestingFrameworkWindowManager method addTestCases.

/**
 * Insert the method's description here.
 * Creation date: (4/10/2003 11:27:32 AM)
 * @param testCase cbit.vcell.numericstestingframework.TestCase
 */
public String addTestCases(final TestSuiteInfoNew tsInfo, final TestCaseNew[] testCaseArray, int regrRefFlag, ClientTaskStatusSupport pp) {
    if (tsInfo == null) {
        throw new IllegalArgumentException("TestSuiteInfo cannot be null");
    }
    if (testCaseArray == null || testCaseArray.length == 0) {
        throw new IllegalArgumentException("TestCases cannot be null / empty");
    }
    // make modifiable list
    List<TestCaseNew> testCases = new ArrayList<>(Arrays.asList(testCaseArray));
    StringBuffer errors = new StringBuffer();
    // When a testCase (mathmodel/biomodel) is added to a testSuite, a new version of the mathModel/biomodel should be created.
    // Also, the simulations in the original mathmodel/biomodel should be rid of their parent simulation reference.
    pp.setMessage("Getting testSuite");
    pp.setProgress(1);
    TestSuiteNew testSuite = null;
    try {
        testSuite = getRequestManager().getDocumentManager().getTestSuite(tsInfo.getTSKey());
    } catch (Throwable e) {
        throw new RuntimeException("couldn't get test suite " + tsInfo.getTSID() + "\n" + e.getClass().getName() + " mesg=" + e.getMessage() + "\n");
    }
    if (testSuite != null && testSuite.getTSInfoNew().isLocked()) {
        throw new RuntimeException("Cannot addTestCases to locked table");
    }
    if (testSuite != null) {
        // Saving BioModels
        TestCaseNew[] existingTestCases = testSuite.getTestCases();
        java.util.HashMap<KeyValue, BioModel> bioModelHashMap = new java.util.HashMap<KeyValue, BioModel>();
        // if(existingTestCases != null){
        // Find BioModels, Using the same BM reference for sibling Applications
        int pcounter = 0;
        // use iterator to allow removal of test case from collection if exception
        Iterator<TestCaseNew> iter = testCases.iterator();
        while (iter.hasNext()) {
            TestCaseNew testCase = iter.next();
            pp.setProgress(Math.max(1, ((int) ((pcounter++ / (double) (testCases.size() * 3)) * 100))));
            pp.setMessage("Checking " + testCase.getVersion().getName());
            try {
                if (testCase instanceof TestCaseNewBioModel) {
                    TestCaseNewBioModel bioTestCase = (TestCaseNewBioModel) testCase;
                    // 
                    if (bioModelHashMap.get(bioTestCase.getBioModelInfo().getVersion().getVersionKey()) == null) {
                        pp.setMessage("Getting BM " + testCase.getVersion().getName());
                        BioModel bioModel = getRequestManager().getDocumentManager().getBioModel(bioTestCase.getBioModelInfo().getVersion().getVersionKey());
                        if (!bioModel.getVersion().getOwner().equals(getRequestManager().getDocumentManager().getUser())) {
                            throw new Exception("BioModel does not belong to VCELLTESTACCOUNT, cannot proceed with test!");
                        }
                        // 
                        // if biomodel already exists in same testsuite, then use this BioModel edition
                        // 
                        BioModel newBioModel = null;
                        if (existingTestCases != null) {
                            for (int j = 0; newBioModel == null && j < existingTestCases.length; j++) {
                                if (existingTestCases[j] instanceof TestCaseNewBioModel) {
                                    TestCaseNewBioModel existingTestCaseBioModel = (TestCaseNewBioModel) existingTestCases[j];
                                    // 
                                    if (existingTestCaseBioModel.getBioModelInfo().getVersion().getBranchID().equals(bioTestCase.getBioModelInfo().getVersion().getBranchID())) {
                                        // 
                                        if (existingTestCaseBioModel.getBioModelInfo().getVersion().getVersionKey().equals(bioTestCase.getBioModelInfo().getVersion().getVersionKey())) {
                                            // 
                                            // same, store this "unchanged" in bioModelHashMap
                                            // 
                                            newBioModel = bioModel;
                                        } else {
                                            // 
                                            throw new Exception("can't add new test case using (" + bioTestCase.getBioModelInfo().getVersion().getName() + " " + bioTestCase.getBioModelInfo().getVersion().getDate() + ")\n" + "a test case already exists with different edition of same BioModel dated " + existingTestCaseBioModel.getBioModelInfo().getVersion().getDate());
                                        }
                                    }
                                }
                            }
                        }
                        if (newBioModel == null) {
                            pp.setMessage("Saving BM " + testCase.getVersion().getName());
                            // 
                            // some older models have membrane voltage variable names which are not unique
                            // (e.g. membranes 'pm' and 'nm' both have membrane voltage variables named 'Voltage_Membrane0')
                            // 
                            // if this is the case, we will try to repair the conflict (for math testing purposes only) by renaming the voltage variables to their default values.
                            // 
                            // Ordinarily, the conflict will be identified as an "Error" issue and the user will be prompted to repair before saving or math generation.
                            // 
                            bioModel.refreshDependencies();
                            boolean bFoundIdentifierConflictUponLoading = hasDuplicateIdentifiers(bioModel);
                            if (bFoundIdentifierConflictUponLoading) {
                                // 
                                // look for two MembraneVoltage instances with same variable name, rename all
                                // 
                                HashSet<String> membraneVoltageVarNames = new HashSet<String>();
                                ArrayList<MembraneVoltage> membraneVoltageVars = new ArrayList<MembraneVoltage>();
                                for (Structure struct : bioModel.getModel().getStructures()) {
                                    if (struct instanceof Membrane) {
                                        MembraneVoltage membraneVoltage = ((Membrane) struct).getMembraneVoltage();
                                        if (membraneVoltage != null) {
                                            membraneVoltageVars.add(membraneVoltage);
                                            membraneVoltageVarNames.add(membraneVoltage.getName());
                                        }
                                    }
                                }
                                if (membraneVoltageVars.size() != membraneVoltageVarNames.size()) {
                                    // rename them all to the default names
                                    for (MembraneVoltage memVoltage : membraneVoltageVars) {
                                        memVoltage.setName(Membrane.getDefaultMembraneVoltageName(memVoltage.getMembrane().getName()));
                                    }
                                }
                            }
                            SimulationContext[] simContexts = bioModel.getSimulationContexts();
                            for (int j = 0; j < simContexts.length; j++) {
                                simContexts[j].clearVersion();
                                GeometrySurfaceDescription gsd = simContexts[j].getGeometry().getGeometrySurfaceDescription();
                                if (gsd != null) {
                                    GeometricRegion[] grArr = gsd.getGeometricRegions();
                                    if (grArr == null) {
                                        gsd.updateAll();
                                    }
                                }
                                MathMapping mathMapping = simContexts[j].createNewMathMapping();
                                // for older models that do not have absolute compartment sizes set, but have relative sizes (SVR/VF); or if there is only one compartment with size not set,
                                // compute absolute compartment sizes using relative sizes and assuming a default value of '1' for one of the compartments.
                                // Otherwise, the math generation will fail, since for the relaxed topology (VCell 5.3 and later) absolute compartment sizes are required.
                                GeometryContext gc = simContexts[j].getGeometryContext();
                                if (simContexts[j].getGeometry().getDimension() == 0 && ((gc.isAllSizeSpecifiedNull() && !gc.isAllVolFracAndSurfVolSpecifiedNull()) || (gc.getModel().getStructures().length == 1 && gc.isAllSizeSpecifiedNull()))) {
                                    // choose the first structure in model and set its size to '1'.
                                    Structure struct = simContexts[j].getModel().getStructure(0);
                                    double structSize = 1.0;
                                    StructureSizeSolver.updateAbsoluteStructureSizes(simContexts[j], struct, structSize, struct.getStructureSize().getUnitDefinition());
                                }
                                simContexts[j].setMathDescription(mathMapping.getMathDescription());
                            }
                            Simulation[] sims = bioModel.getSimulations();
                            String[] simNames = new String[sims.length];
                            for (int j = 0; j < sims.length; j++) {
                                // prevents parent simulation (from the original mathmodel) reference connection
                                // Otherwise it will refer to data from previous (parent) simulation.
                                sims[j].clearVersion();
                                simNames[j] = sims[j].getName();
                            // if(sims[j].getSolverTaskDescription().getSolverDescription().equals(SolverDescription.FiniteVolume)){
                            // sims[j].getSolverTaskDescription().setSolverDescription(SolverDescription.FiniteVolumeStandalone);
                            // }
                            }
                            newBioModel = getRequestManager().getDocumentManager().save(bioModel, simNames);
                        }
                        bioModelHashMap.put(bioTestCase.getBioModelInfo().getVersion().getVersionKey(), newBioModel);
                    }
                }
            } catch (Throwable e) {
                String identifier = testCase.getVersion() != null ? "Name=" + testCase.getVersion().getName() : "TCKey=" + testCase.getTCKey();
                if (lg.isInfoEnabled()) {
                    lg.info(identifier, e);
                }
                errors.append("Error collecting BioModel for TestCase " + identifier + '\n' + e.getClass().getName() + " " + e.getMessage() + '\n');
                // remove to avoid further processing attempts
                iter.remove();
            }
        }
        // }
        // then process each BioModelTestCase individually
        // if(bioModelHashMap != null){
        pcounter = 0;
        for (TestCaseNew testCase : testCases) {
            pp.setProgress(Math.max(1, ((int) ((pcounter++ / (double) (testCases.size() * 3)) * 100))));
            pp.setMessage("Checking " + testCase.getVersion().getName());
            try {
                AddTestCasesOP testCaseOP = null;
                if (testCase instanceof TestCaseNewBioModel) {
                    pp.setMessage("Processing BM " + testCase.getVersion().getName());
                    TestCaseNewBioModel bioTestCase = (TestCaseNewBioModel) testCase;
                    BioModel newBioModel = (BioModel) bioModelHashMap.get(bioTestCase.getBioModelInfo().getVersion().getVersionKey());
                    if (newBioModel == null) {
                        throw new Exception("BioModel not found");
                    }
                    SimulationContext simContext = null;
                    for (int j = 0; j < newBioModel.getSimulationContexts().length; j++) {
                        if (newBioModel.getSimulationContext(j).getName().equals(bioTestCase.getSimContextName())) {
                            simContext = newBioModel.getSimulationContext(j);
                        }
                    }
                    Simulation[] newSimulations = simContext.getSimulations();
                    AddTestCriteriaOPBioModel[] testCriteriaOPs = new AddTestCriteriaOPBioModel[newSimulations.length];
                    for (int j = 0; j < newSimulations.length; j++) {
                        TestCriteriaNewBioModel tcritOrigForSimName = null;
                        for (int k = 0; bioTestCase.getTestCriterias() != null && k < bioTestCase.getTestCriterias().length; k += 1) {
                            if (bioTestCase.getTestCriterias()[k].getSimInfo().getName().equals(newSimulations[j].getName())) {
                                tcritOrigForSimName = (TestCriteriaNewBioModel) bioTestCase.getTestCriterias()[k];
                                break;
                            }
                        }
                        KeyValue regressionBioModelKey = null;
                        KeyValue regressionBioModelSimKey = null;
                        if (bioTestCase.getType().equals(TestCaseNew.REGRESSION)) {
                            if (regrRefFlag == TestingFrameworkWindowManager.COPY_REGRREF) {
                                regressionBioModelKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionBioModelInfo() != null ? tcritOrigForSimName.getRegressionBioModelInfo().getVersion().getVersionKey() : null);
                                regressionBioModelSimKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionSimInfo() != null ? tcritOrigForSimName.getRegressionSimInfo().getVersion().getVersionKey() : null);
                            } else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNORIGINAL_REGRREF) {
                                regressionBioModelKey = (tcritOrigForSimName != null ? bioTestCase.getBioModelInfo().getVersion().getVersionKey() : null);
                                regressionBioModelSimKey = (tcritOrigForSimName != null ? tcritOrigForSimName.getSimInfo().getVersion().getVersionKey() : null);
                            } else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNNEW_REGRREF) {
                                regressionBioModelKey = newBioModel.getVersion().getVersionKey();
                                regressionBioModelSimKey = newSimulations[j].getVersion().getVersionKey();
                            } else {
                                throw new IllegalArgumentException(this.getClass().getName() + ".addTestCases(...) BIOMODEL Unknown Regression Operation Flag");
                            }
                        }
                        testCriteriaOPs[j] = new AddTestCriteriaOPBioModel(testCase.getTCKey(), newSimulations[j].getVersion().getVersionKey(), regressionBioModelKey, regressionBioModelSimKey, (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxAbsError() : new Double(1e-16)), (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxRelError() : new Double(1e-9)), null);
                    }
                    testCaseOP = new AddTestCasesOPBioModel(new BigDecimal(tsInfo.getTSKey().toString()), newBioModel.getVersion().getVersionKey(), simContext.getKey(), bioTestCase.getType(), bioTestCase.getAnnotation(), testCriteriaOPs);
                    getRequestManager().getDocumentManager().doTestSuiteOP(testCaseOP);
                }
            } catch (Throwable e) {
                errors.append("Error processing Biomodel for TestCase " + (testCase.getVersion() != null ? "Name=" + testCase.getVersion().getName() : "TCKey=" + testCase.getTCKey()) + "\n" + e.getClass().getName() + " " + e.getMessage() + "\n");
            }
        }
        // }
        // Process MathModels
        pcounter = 0;
        for (TestCaseNew testCase : testCases) {
            pp.setProgress(Math.max(1, ((int) ((pcounter++ / (double) (testCases.size() * 3)) * 100))));
            pp.setMessage("Checking " + testCase.getVersion().getName());
            try {
                AddTestCasesOP testCaseOP = null;
                if (testCase instanceof TestCaseNewMathModel) {
                    TestCaseNewMathModel mathTestCase = (TestCaseNewMathModel) testCase;
                    pp.setMessage("Getting MathModel " + testCase.getVersion().getName());
                    MathModel mathModel = getRequestManager().getDocumentManager().getMathModel(mathTestCase.getMathModelInfo().getVersion().getVersionKey());
                    if (!mathModel.getVersion().getOwner().equals(getRequestManager().getDocumentManager().getUser())) {
                        throw new Exception("MathModel does not belong to VCELLTESTACCOUNT, cannot proceed with test!");
                    }
                    Simulation[] sims = mathModel.getSimulations();
                    String[] simNames = new String[sims.length];
                    for (int j = 0; j < sims.length; j++) {
                        // prevents parent simulation (from the original mathmodel) reference connection
                        // Otherwise it will refer to data from previous (parent) simulation.
                        sims[j].clearVersion();
                        simNames[j] = sims[j].getName();
                    // if(sims[j].getSolverTaskDescription().getSolverDescription().equals(SolverDescription.FiniteVolume)){
                    // sims[j].getSolverTaskDescription().setSolverDescription(SolverDescription.FiniteVolumeStandalone);
                    // }
                    }
                    pp.setMessage("Saving MathModel " + testCase.getVersion().getName());
                    MathModel newMathModel = getRequestManager().getDocumentManager().save(mathModel, simNames);
                    Simulation[] newSimulations = newMathModel.getSimulations();
                    AddTestCriteriaOPMathModel[] testCriteriaOPs = new AddTestCriteriaOPMathModel[newSimulations.length];
                    for (int j = 0; j < newSimulations.length; j++) {
                        TestCriteriaNewMathModel tcritOrigForSimName = null;
                        for (int k = 0; mathTestCase.getTestCriterias() != null && k < mathTestCase.getTestCriterias().length; k += 1) {
                            if (mathTestCase.getTestCriterias()[k].getSimInfo().getName().equals(newSimulations[j].getName())) {
                                tcritOrigForSimName = (TestCriteriaNewMathModel) mathTestCase.getTestCriterias()[k];
                                break;
                            }
                        }
                        KeyValue regressionMathModelKey = null;
                        KeyValue regressionMathModelSimKey = null;
                        if (mathTestCase.getType().equals(TestCaseNew.REGRESSION)) {
                            if (regrRefFlag == TestingFrameworkWindowManager.COPY_REGRREF) {
                                regressionMathModelKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionMathModelInfo() != null ? tcritOrigForSimName.getRegressionMathModelInfo().getVersion().getVersionKey() : null);
                                regressionMathModelSimKey = (tcritOrigForSimName != null && tcritOrigForSimName.getRegressionSimInfo() != null ? tcritOrigForSimName.getRegressionSimInfo().getVersion().getVersionKey() : null);
                            } else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNORIGINAL_REGRREF) {
                                regressionMathModelKey = (tcritOrigForSimName != null ? mathTestCase.getMathModelInfo().getVersion().getVersionKey() : null);
                                regressionMathModelSimKey = (tcritOrigForSimName != null ? tcritOrigForSimName.getSimInfo().getVersion().getVersionKey() : null);
                            } else if (regrRefFlag == TestingFrameworkWindowManager.ASSIGNNEW_REGRREF) {
                                regressionMathModelKey = newMathModel.getVersion().getVersionKey();
                                regressionMathModelSimKey = newSimulations[j].getVersion().getVersionKey();
                            } else {
                                throw new IllegalArgumentException(this.getClass().getName() + ".addTestCases(...) MATHMODEL Unknown Regression Operation Flag");
                            }
                        }
                        testCriteriaOPs[j] = new AddTestCriteriaOPMathModel(testCase.getTCKey(), newSimulations[j].getVersion().getVersionKey(), regressionMathModelKey, regressionMathModelSimKey, (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxAbsError() : new Double(1e-16)), (tcritOrigForSimName != null ? tcritOrigForSimName.getMaxRelError() : new Double(1e-9)), null);
                    }
                    testCaseOP = new AddTestCasesOPMathModel(new BigDecimal(tsInfo.getTSKey().toString()), newMathModel.getVersion().getVersionKey(), mathTestCase.getType(), mathTestCase.getAnnotation(), testCriteriaOPs);
                    getRequestManager().getDocumentManager().doTestSuiteOP(testCaseOP);
                }
            } catch (Throwable e) {
                errors.append("Error processing MathModel for TestCase " + (testCase.getVersion() != null ? "Name=" + testCase.getVersion().getName() : "TCKey=" + testCase.getTCKey()) + "\n" + e.getClass().getName() + " " + e.getMessage() + "\n");
            }
        }
    }
    if (errors.length() > 0) {
        return errors.toString();
    }
    return null;
}
Also used : AddTestCriteriaOPBioModel(cbit.vcell.numericstest.AddTestCriteriaOPBioModel) AddTestCasesOPBioModel(cbit.vcell.numericstest.AddTestCasesOPBioModel) ArrayList(java.util.ArrayList) TestCriteriaNewMathModel(cbit.vcell.numericstest.TestCriteriaNewMathModel) GeometryContext(cbit.vcell.mapping.GeometryContext) HashSet(java.util.HashSet) TestSuiteNew(cbit.vcell.numericstest.TestSuiteNew) TestCaseNew(cbit.vcell.numericstest.TestCaseNew) GeometricRegion(cbit.vcell.geometry.surface.GeometricRegion) AddTestCriteriaOPMathModel(cbit.vcell.numericstest.AddTestCriteriaOPMathModel) MathMapping(cbit.vcell.mapping.MathMapping) MathModel(cbit.vcell.mathmodel.MathModel) AddTestCasesOPMathModel(cbit.vcell.numericstest.AddTestCasesOPMathModel) TestCaseNewMathModel(cbit.vcell.numericstest.TestCaseNewMathModel) TestCriteriaNewMathModel(cbit.vcell.numericstest.TestCriteriaNewMathModel) EditTestCriteriaOPMathModel(cbit.vcell.numericstest.EditTestCriteriaOPMathModel) AddTestCriteriaOPMathModel(cbit.vcell.numericstest.AddTestCriteriaOPMathModel) KeyValue(org.vcell.util.document.KeyValue) GeometrySurfaceDescription(cbit.vcell.geometry.surface.GeometrySurfaceDescription) TestCriteriaNewBioModel(cbit.vcell.numericstest.TestCriteriaNewBioModel) AddTestCasesOP(cbit.vcell.numericstest.AddTestCasesOP) TestCaseNewBioModel(cbit.vcell.numericstest.TestCaseNewBioModel) AddTestCasesOPMathModel(cbit.vcell.numericstest.AddTestCasesOPMathModel) Membrane(cbit.vcell.model.Membrane) Structure(cbit.vcell.model.Structure) SimulationContext(cbit.vcell.mapping.SimulationContext) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) DataAccessException(org.vcell.util.DataAccessException) UserCancelException(org.vcell.util.UserCancelException) BigDecimal(java.math.BigDecimal) Simulation(cbit.vcell.solver.Simulation) MembraneVoltage(cbit.vcell.model.Membrane.MembraneVoltage) TestCaseNewBioModel(cbit.vcell.numericstest.TestCaseNewBioModel) TestCriteriaNewBioModel(cbit.vcell.numericstest.TestCriteriaNewBioModel) AddTestCasesOPBioModel(cbit.vcell.numericstest.AddTestCasesOPBioModel) BioModel(cbit.vcell.biomodel.BioModel) EditTestCriteriaOPBioModel(cbit.vcell.numericstest.EditTestCriteriaOPBioModel) AddTestCriteriaOPBioModel(cbit.vcell.numericstest.AddTestCriteriaOPBioModel) TestCaseNewMathModel(cbit.vcell.numericstest.TestCaseNewMathModel)

Example 12 with GeometricRegion

use of cbit.vcell.geometry.surface.GeometricRegion in project vcell by virtualcell.

the class CartesianMesh method createSimpleCartesianMesh.

public static CartesianMesh createSimpleCartesianMesh(Geometry geometry, Map<Polygon, MembraneElement> polygonMembaneElementMap) throws IOException, MathFormatException {
    GeometrySurfaceDescription geometrySurfaceDescription = geometry.getGeometrySurfaceDescription();
    RegionImage regionImage = geometrySurfaceDescription.getRegionImage();
    ISize iSize = new ISize(regionImage.getNumX(), regionImage.getNumY(), regionImage.getNumZ());
    CartesianMesh mesh = createSimpleCartesianMesh(geometry.getOrigin(), geometry.getExtent(), iSize, regionImage);
    GeometricRegion[] geometricRegions = geometrySurfaceDescription.getGeometricRegions();
    if (geometricRegions != null) {
        int memRegionCount = 0;
        for (int i = 0; i < geometricRegions.length; i++) {
            if (geometricRegions[i] instanceof VolumeGeometricRegion) {
                VolumeGeometricRegion vgr = (VolumeGeometricRegion) geometricRegions[i];
                mesh.meshRegionInfo.mapVolumeRegionToSubvolume(vgr.getRegionID(), vgr.getSubVolume().getHandle(), vgr.getSize(), vgr.getName());
            } else if (geometricRegions[i] instanceof SurfaceGeometricRegion) {
                SurfaceGeometricRegion sgr = (SurfaceGeometricRegion) geometricRegions[i];
                GeometricRegion[] neighbors = sgr.getAdjacentGeometricRegions();
                VolumeGeometricRegion insideRegion = (VolumeGeometricRegion) neighbors[0];
                VolumeGeometricRegion outsideRegion = (VolumeGeometricRegion) neighbors[1];
                mesh.meshRegionInfo.mapMembraneRegionToVolumeRegion(memRegionCount, insideRegion.getRegionID(), outsideRegion.getRegionID(), sgr.getSize());
                memRegionCount++;
            }
        }
    }
    SurfaceCollection surfaceCollection = geometrySurfaceDescription.getSurfaceCollection();
    if (surfaceCollection != null) {
        int numMembraneElement = surfaceCollection.getTotalPolygonCount();
        mesh.membraneElements = new MembraneElement[numMembraneElement];
        boolean bMembraneEdgeNeighborsAvailable = surfaceCollection.getMembraneEdgeNeighbors() != null && surfaceCollection.getMembraneEdgeNeighbors().length == surfaceCollection.getSurfaceCount();
        int[] membraneElementMapMembraneRegion = new int[numMembraneElement];
        mesh.meshRegionInfo.mapMembraneElementsToMembraneRegions(membraneElementMapMembraneRegion);
        int memCount = 0;
        // original values when no membraneedgeneighbors
        int[] membraneNeighbors = new int[] { 0, 0, 0, 0 };
        for (int i = 0; i < surfaceCollection.getSurfaceCount(); i++) {
            Surface surface = surfaceCollection.getSurfaces(i);
            bMembraneEdgeNeighborsAvailable = bMembraneEdgeNeighborsAvailable && surfaceCollection.getMembraneEdgeNeighbors()[i].length == surface.getPolygonCount();
            for (int j = 0; j < surface.getPolygonCount(); j++) {
                if (bMembraneEdgeNeighborsAvailable) {
                    membraneNeighbors = new int[MembraneElement.MAX_POSSIBLE_NEIGHBORS];
                    Arrays.fill(membraneNeighbors, MembraneElement.NEIGHBOR_UNDEFINED);
                    for (int k = 0; k < surfaceCollection.getMembraneEdgeNeighbors()[i][j].size(); k++) {
                        membraneNeighbors[k] = surfaceCollection.getMembraneEdgeNeighbors()[i][j].get(k).getMasterPolygonIndex();
                    }
                }
                Quadrilateral polygon = (Quadrilateral) surface.getPolygons(j);
                int volNeighbor1Region = regionImage.getRegionInfoFromOffset(polygon.getVolIndexNeighbor1()).getRegionIndex();
                int volNeighbor2Region = regionImage.getRegionInfoFromOffset(polygon.getVolIndexNeighbor2()).getRegionIndex();
                HashMap<Integer, int[]> map = mesh.getMembraneRegionMapSubvolumesInOut();
                Set<Entry<Integer, int[]>> entries = map.entrySet();
                for (Entry<Integer, int[]> entry : entries) {
                    int[] volNeighbors = entry.getValue();
                    if (volNeighbors[0] == volNeighbor1Region && volNeighbors[1] == volNeighbor2Region || volNeighbors[1] == volNeighbor1Region && volNeighbors[0] == volNeighbor2Region) {
                        membraneElementMapMembraneRegion[memCount] = entry.getKey();
                        break;
                    }
                }
                mesh.membraneElements[memCount] = new MembraneElement(memCount, polygon.getVolIndexNeighbor1(), polygon.getVolIndexNeighbor2(), membraneNeighbors[0], membraneNeighbors[1], membraneNeighbors[2], membraneNeighbors[3], MembraneElement.AREA_UNDEFINED, 0, 0, 0, 0, 0, 0);
                if (polygonMembaneElementMap != null) {
                    polygonMembaneElementMap.put(polygon, mesh.membraneElements[memCount]);
                }
                memCount++;
            }
        }
    }
    return mesh;
}
Also used : SurfaceCollection(cbit.vcell.geometry.surface.SurfaceCollection) GeometrySurfaceDescription(cbit.vcell.geometry.surface.GeometrySurfaceDescription) ISize(org.vcell.util.ISize) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion) GeometricRegion(cbit.vcell.geometry.surface.GeometricRegion) Surface(cbit.vcell.geometry.surface.Surface) Quadrilateral(cbit.vcell.geometry.surface.Quadrilateral) Entry(java.util.Map.Entry) RegionImage(cbit.vcell.geometry.RegionImage) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion)

Example 13 with GeometricRegion

use of cbit.vcell.geometry.surface.GeometricRegion in project vcell by virtualcell.

the class GeomDbDriver method getSurfaceDescription.

/**
 * Insert the method's description here.
 * Creation date: (7/29/00 2:10:42 PM)
 * @param con java.sql.Connection
 * @param geom cbit.vcell.geometry.Geometry
 */
private void getSurfaceDescription(Connection con, Geometry geom) throws SQLException, DataAccessException {
    // System.out.println(sql);
    Statement stmt = con.createStatement();
    try {
        String sql = null;
        // 
        // read sampleSize and filterFrequency from GeometrySurfaceTable.
        // 
        sql = " SELECT " + geoSurfaceTable.getTableName() + ".* " + " FROM " + geoSurfaceTable.getTableName() + " WHERE " + geoSurfaceTable.geometryRef.getQualifiedColName() + " = " + geom.getVersion().getVersionKey();
        System.out.println(sql);
        ResultSet rset = stmt.executeQuery(sql);
        if (rset.next()) {
            geoSurfaceTable.populateGeometrySurfaceDescription(rset, geom.getGeometrySurfaceDescription());
            rset.close();
        } else {
            if (lg.isWarnEnabled()) {
                lg.warn("surface description not found for geometry " + geom.getVersion().toString());
            }
            rset.close();
            return;
        }
        // 
        // read volume regions from GeometricRegionTable
        // 
        sql = " SELECT " + geoRegionTable.name.getQualifiedColName() + ", " + geoRegionTable.size.getQualifiedColName() + ", " + geoRegionTable.sizeUnit.getQualifiedColName() + ", " + geoRegionTable.subVolumeRef.getQualifiedColName() + ", " + geoRegionTable.regionID.getQualifiedColName() + " FROM " + geoRegionTable.getTableName() + " WHERE " + geoRegionTable.geometryRef.getQualifiedColName() + " = " + geom.getVersion().getVersionKey() + " AND " + geoRegionTable.type + " = " + GeometricRegionTable.TYPE_VOLUME;
        System.out.println(sql);
        rset = stmt.executeQuery(sql);
        Vector<GeometricRegion> regionList = new Vector<GeometricRegion>();
        while (rset.next()) {
            VolumeGeometricRegion volumeRegion = geoRegionTable.getVolumeRegion(rset, geom);
            regionList.add(volumeRegion);
        }
        VolumeGeometricRegion[] volumeRegions = (VolumeGeometricRegion[]) BeanUtils.getArray(regionList, VolumeGeometricRegion.class);
        // 
        // read surface regions from GeometricRegionTable
        // 
        sql = " SELECT " + "surfTable." + geoRegionTable.name.getUnqualifiedColName() + ", " + "surfTable." + geoRegionTable.size.getUnqualifiedColName() + ", " + "surfTable." + geoRegionTable.sizeUnit.getUnqualifiedColName() + ", " + "vol1Table.name as " + GeometricRegionTable.VOLUME1_NAME_COLUMN + ", " + "vol2Table.name as " + GeometricRegionTable.VOLUME2_NAME_COLUMN + " " + " FROM " + geoRegionTable.getTableName() + " surfTable, " + geoRegionTable.getTableName() + " vol1Table, " + geoRegionTable.getTableName() + " vol2Table " + " WHERE surfTable." + geoRegionTable.geometryRef.getUnqualifiedColName() + " = " + geom.getVersion().getVersionKey() + " AND vol1Table.id = surfTable." + geoRegionTable.volRegion1.getUnqualifiedColName() + " AND vol2Table.id = surfTable." + geoRegionTable.volRegion2.getUnqualifiedColName() + " AND surfTable." + geoRegionTable.type.getUnqualifiedColName() + " = " + GeometricRegionTable.TYPE_SURFACE;
        System.out.println(sql);
        rset = stmt.executeQuery(sql);
        while (rset.next()) {
            SurfaceGeometricRegion surfaceRegion = geoRegionTable.getSurfaceRegion(rset, volumeRegions, geom.getUnitSystem());
            regionList.add(surfaceRegion);
        }
        // 
        // set regions onto the geometrySurfaceDescription
        // 
        GeometricRegion[] regions = (GeometricRegion[]) BeanUtils.getArray(regionList, GeometricRegion.class);
        geom.getGeometrySurfaceDescription().setGeometricRegions(regions);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new DataAccessException(e.toString());
    } finally {
        // Release resources include resultset
        stmt.close();
    }
}
Also used : Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Vector(java.util.Vector) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) PermissionException(org.vcell.util.PermissionException) ObjectNotFoundException(org.vcell.util.ObjectNotFoundException) PropertyVetoException(java.beans.PropertyVetoException) SQLException(java.sql.SQLException) DependencyException(org.vcell.util.DependencyException) GifParsingException(cbit.image.GifParsingException) RecordChangedException(cbit.sql.RecordChangedException) ImageException(cbit.image.ImageException) DataAccessException(org.vcell.util.DataAccessException) ExpressionException(cbit.vcell.parser.ExpressionException) DataAccessException(org.vcell.util.DataAccessException) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion) GeometricRegion(cbit.vcell.geometry.surface.GeometricRegion) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion)

Example 14 with GeometricRegion

use of cbit.vcell.geometry.surface.GeometricRegion in project vcell by virtualcell.

the class GeomDbDriver method insertGeometrySurfaceDescriptionSQL.

/**
 * This method was created in VisualAge.
 * @param vcimage cbit.image.VCImage
 * @param userid java.lang.String
 * @exception java.rmi.RemoteException The exception description.
 */
// default access for this method, to allow retrofitting surfaces to geometries.
void insertGeometrySurfaceDescriptionSQL(InsertHashtable hash, Connection con, Geometry geom, KeyValue geomKey) throws SQLException, cbit.image.ImageException, DataAccessException, ObjectNotFoundException {
    String sql;
    GeometrySurfaceDescription geoSurfaceDescription = geom.getGeometrySurfaceDescription();
    // 
    // store GeometrySurfaceDescription (sampleSize and filterFrequency for now)
    // 
    KeyValue newGeomSurfDescKey = keyFactory.getNewKey(con);
    sql = "INSERT INTO " + geoSurfaceTable.getTableName() + " " + geoSurfaceTable.getSQLColumnList() + " VALUES " + geoSurfaceTable.getSQLValueList(newGeomSurfDescKey, geoSurfaceDescription, geomKey);
    // System.out.println(sql);
    updateCleanSQL(con, sql);
    // 
    // store GeometricRegions
    // 
    GeometricRegion[] regions = geoSurfaceDescription.getGeometricRegions();
    // }
    if (regions == null) {
        System.out.println("Geometry " + geom.getName() + "(" + geom.getVersion() + ") doesn't have region information");
        throw new DataAccessException("geometry '" + geom.getName() + " didn't have region information");
    }
    // 
    for (int i = 0; i < regions.length; i++) {
        if (regions[i] instanceof VolumeGeometricRegion) {
            VolumeGeometricRegion volumeRegion = (VolumeGeometricRegion) regions[i];
            if (hash.getDatabaseKey(volumeRegion) == null) {
                KeyValue newVolumeRegionKey = keyFactory.getNewKey(con);
                KeyValue subvolumeKey = hash.getDatabaseKey(volumeRegion.getSubVolume());
                sql = "INSERT INTO " + geoRegionTable.getTableName() + " " + geoRegionTable.getSQLColumnList() + " VALUES " + geoRegionTable.getSQLValueList(newVolumeRegionKey, volumeRegion, subvolumeKey, geomKey);
                // System.out.println(sql);
                updateCleanSQL(con, sql);
                hash.put(volumeRegion, newVolumeRegionKey);
            }
        }
    }
    // 
    for (int i = 0; i < regions.length; i++) {
        if (regions[i] instanceof SurfaceGeometricRegion) {
            SurfaceGeometricRegion surfaceRegion = (SurfaceGeometricRegion) regions[i];
            if (hash.getDatabaseKey(surfaceRegion) == null) {
                KeyValue newSurfaceRegionKey = keyFactory.getNewKey(con);
                KeyValue volumeRegion1Key = hash.getDatabaseKey((VolumeGeometricRegion) surfaceRegion.getAdjacentGeometricRegions()[0]);
                KeyValue volumeRegion2Key = hash.getDatabaseKey((VolumeGeometricRegion) surfaceRegion.getAdjacentGeometricRegions()[1]);
                sql = "INSERT INTO " + geoRegionTable.getTableName() + " " + geoRegionTable.getSQLColumnList() + " VALUES " + geoRegionTable.getSQLValueList(newSurfaceRegionKey, surfaceRegion, volumeRegion1Key, volumeRegion2Key, geomKey);
                // System.out.println(sql);
                updateCleanSQL(con, sql);
                hash.put(surfaceRegion, newSurfaceRegionKey);
            }
        }
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) GeometrySurfaceDescription(cbit.vcell.geometry.surface.GeometrySurfaceDescription) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) DataAccessException(org.vcell.util.DataAccessException) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion) GeometricRegion(cbit.vcell.geometry.surface.GeometricRegion) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion)

Example 15 with GeometricRegion

use of cbit.vcell.geometry.surface.GeometricRegion in project vcell by virtualcell.

the class SimulationContext method copySimulationContext.

public static SimulationContext copySimulationContext(SimulationContext srcSimContext, String newSimulationContextName, boolean bSpatial, Application simContextType) throws java.beans.PropertyVetoException, ExpressionException, MappingException, GeometryException, ImageException {
    Geometry newClonedGeometry = new Geometry(srcSimContext.getGeometry());
    newClonedGeometry.precomputeAll(new GeometryThumbnailImageFactoryAWT());
    // if stoch copy to ode, we need to check is stoch is using particles. If yes, should convert particles to concentraton.
    // the other 3 cases are fine. ode->ode, ode->stoch, stoch-> stoch
    SimulationContext destSimContext = new SimulationContext(srcSimContext, newClonedGeometry, simContextType);
    if (srcSimContext.getApplicationType() == Application.NETWORK_STOCHASTIC && !srcSimContext.isUsingConcentration() && simContextType == Application.NETWORK_DETERMINISTIC) {
        try {
            destSimContext.convertSpeciesIniCondition(true);
        } catch (MappingException e) {
            e.printStackTrace();
            throw new java.beans.PropertyVetoException(e.getMessage(), null);
        }
    }
    if (srcSimContext.getGeometry().getDimension() > 0 && !bSpatial) {
        // copy the size over
        destSimContext.setGeometry(new Geometry("nonspatial", 0));
        StructureMapping[] srcStructureMappings = srcSimContext.getGeometryContext().getStructureMappings();
        StructureMapping[] destStructureMappings = destSimContext.getGeometryContext().getStructureMappings();
        for (StructureMapping destStructureMapping : destStructureMappings) {
            for (StructureMapping srcStructureMapping : srcStructureMappings) {
                if (destStructureMapping.getStructure() == srcStructureMapping.getStructure()) {
                    if (srcStructureMapping.getUnitSizeParameter() != null) {
                        Expression sizeRatio = srcStructureMapping.getUnitSizeParameter().getExpression();
                        GeometryClass srcGeometryClass = srcStructureMapping.getGeometryClass();
                        GeometricRegion[] srcGeometricRegions = srcSimContext.getGeometry().getGeometrySurfaceDescription().getGeometricRegions(srcGeometryClass);
                        if (srcGeometricRegions != null) {
                            double size = 0;
                            for (GeometricRegion srcGeometricRegion : srcGeometricRegions) {
                                size += srcGeometricRegion.getSize();
                            }
                            destStructureMapping.getSizeParameter().setExpression(Expression.mult(sizeRatio, new Expression(size)));
                        }
                    }
                    break;
                }
            }
        }
        // If changing spatial to non-spatial
        // set diffusion to 0, velocity and boundary to null
        // srcSimContext.getReactionContext().getspe
        Parameter[] allParameters = destSimContext.getAllParameters();
        if (allParameters != null && allParameters.length > 0) {
            for (int i = 0; i < allParameters.length; i++) {
                if (allParameters[i] instanceof SpeciesContextSpecParameter) {
                    SpeciesContextSpecParameter speciesContextSpecParameter = (SpeciesContextSpecParameter) allParameters[i];
                    int role = speciesContextSpecParameter.getRole();
                    if (role == SpeciesContextSpec.ROLE_DiffusionRate) {
                        speciesContextSpecParameter.setExpression(new Expression(0));
                    } else if (role == SpeciesContextSpec.ROLE_BoundaryValueXm || role == SpeciesContextSpec.ROLE_BoundaryValueXp || role == SpeciesContextSpec.ROLE_BoundaryValueYm || role == SpeciesContextSpec.ROLE_BoundaryValueYp || role == SpeciesContextSpec.ROLE_BoundaryValueZm || role == SpeciesContextSpec.ROLE_BoundaryValueZp) {
                        speciesContextSpecParameter.setExpression(null);
                    } else if (role == SpeciesContextSpec.ROLE_VelocityX || role == SpeciesContextSpec.ROLE_VelocityY || role == SpeciesContextSpec.ROLE_VelocityZ) {
                        speciesContextSpecParameter.setExpression(null);
                    }
                }
            }
        }
    }
    destSimContext.fixFlags();
    destSimContext.setName(newSimulationContextName);
    return destSimContext;
}
Also used : GeometryClass(cbit.vcell.geometry.GeometryClass) SurfaceGeometricRegion(cbit.vcell.geometry.surface.SurfaceGeometricRegion) VolumeGeometricRegion(cbit.vcell.geometry.surface.VolumeGeometricRegion) GeometricRegion(cbit.vcell.geometry.surface.GeometricRegion) Geometry(cbit.vcell.geometry.Geometry) PropertyVetoException(java.beans.PropertyVetoException) GeometryThumbnailImageFactoryAWT(cbit.vcell.geometry.GeometryThumbnailImageFactoryAWT) Expression(cbit.vcell.parser.Expression) Parameter(cbit.vcell.model.Parameter) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter)

Aggregations

GeometricRegion (cbit.vcell.geometry.surface.GeometricRegion)24 VolumeGeometricRegion (cbit.vcell.geometry.surface.VolumeGeometricRegion)22 SurfaceGeometricRegion (cbit.vcell.geometry.surface.SurfaceGeometricRegion)21 SubVolume (cbit.vcell.geometry.SubVolume)11 GeometrySurfaceDescription (cbit.vcell.geometry.surface.GeometrySurfaceDescription)11 ISize (org.vcell.util.ISize)10 SurfaceClass (cbit.vcell.geometry.SurfaceClass)8 PropertyVetoException (java.beans.PropertyVetoException)8 ArrayList (java.util.ArrayList)8 Expression (cbit.vcell.parser.Expression)7 ExpressionException (cbit.vcell.parser.ExpressionException)7 ImageException (cbit.image.ImageException)6 Geometry (cbit.vcell.geometry.Geometry)6 Origin (org.vcell.util.Origin)6 AnalyticSubVolume (cbit.vcell.geometry.AnalyticSubVolume)5 GeometryClass (cbit.vcell.geometry.GeometryClass)5 GeometrySpec (cbit.vcell.geometry.GeometrySpec)5 RegionInfo (cbit.vcell.geometry.RegionImage.RegionInfo)5 CompartmentSubDomain (cbit.vcell.math.CompartmentSubDomain)5 MembraneSubDomain (cbit.vcell.math.MembraneSubDomain)5