Search in sources :

Example 31 with Coordinate

use of org.vcell.util.Coordinate in project vcell by virtualcell.

the class ITextWriter method writeMembraneMapping.

protected void writeMembraneMapping(Section simContextSection, SimulationContext simContext) throws DocumentException {
    GeometryContext geoContext = simContext.getGeometryContext();
    if (geoContext == null) {
        return;
    }
    Section memMapSection = null;
    Table memMapTable = null;
    StructureMapping[] structMappings = geoContext.getStructureMappings();
    for (int i = 0; i < structMappings.length; i++) {
        MembraneMapping memMapping = null;
        if (structMappings[i] instanceof FeatureMapping) {
            continue;
        } else {
            memMapping = (MembraneMapping) structMappings[i];
        }
        String structName = memMapping.getStructure().getName();
        String initVoltage = "";
        Expression tempExp = memMapping.getInitialVoltageParameter().getExpression();
        VCUnitDefinition tempUnit = memMapping.getInitialVoltageParameter().getUnitDefinition();
        if (tempExp != null) {
            initVoltage = tempExp.infix();
            if (tempUnit != null) {
                initVoltage += "   " + tempUnit.getSymbolUnicode();
            }
        }
        String spCap = "";
        tempExp = memMapping.getSpecificCapacitanceParameter().getExpression();
        tempUnit = memMapping.getSpecificCapacitanceParameter().getUnitDefinition();
        if (tempExp != null) {
            spCap = tempExp.infix();
            if (tempUnit != null) {
                spCap += "   " + tempUnit.getSymbolUnicode();
            }
        }
        if (memMapTable == null) {
            memMapTable = getTable(4, 100, 1, 3, 3);
            memMapTable.addCell(createCell("Electrical Mapping - Membrane Potential", getBold(DEF_HEADER_FONT_SIZE), 4, 1, Element.ALIGN_CENTER, true));
            memMapTable.addCell(createHeaderCell("Membrane", getBold(), 1));
            memMapTable.addCell(createHeaderCell("Calculate V (T/F)", getBold(), 1));
            memMapTable.addCell(createHeaderCell("V initial", getBold(), 1));
            memMapTable.addCell(createHeaderCell("Specific Capacitance", getBold(), 1));
            memMapTable.endHeaders();
        }
        memMapTable.addCell(createCell(structName, getFont()));
        memMapTable.addCell(createCell((memMapping.getCalculateVoltage() ? " T " : " F "), getFont()));
        memMapTable.addCell(createCell(initVoltage, getFont()));
        memMapTable.addCell(createCell(spCap, getFont()));
    }
    if (memMapTable != null) {
        memMapSection = simContextSection.addSection("Membrane Mapping For " + simContext.getName(), simContextSection.numberDepth() + 1);
        memMapSection.add(memMapTable);
    }
    int[] widths = { 1, 1, 1, 5, 8 };
    Table electTable = null;
    ElectricalStimulus[] electricalStimuli = simContext.getElectricalStimuli();
    for (int j = 0; j < electricalStimuli.length; j++) {
        if (j == 0) {
            electTable = getTable(5, 100, 1, 3, 3);
            electTable.addCell(createCell("Electrical Mapping - Electrical Stimulus", getBold(DEF_HEADER_FONT_SIZE), 5, 1, Element.ALIGN_CENTER, true));
            electTable.addCell(createHeaderCell("Stimulus Name", getBold(), 1));
            electTable.addCell(createHeaderCell("Current Name", getBold(), 1));
            electTable.addCell(createHeaderCell("Clamp Type", getBold(), 1));
            electTable.addCell(createHeaderCell("Voltage/Current Density", getBold(), 1));
            electTable.addCell(createHeaderCell("Clamp Device", getBold(), 1));
            electTable.endHeaders();
        }
        String stimName = electricalStimuli[j].getName();
        String currName = "";
        String clampType = "", expStr = "";
        Expression tempExp = null;
        VCUnitDefinition tempUnit = null;
        if (electricalStimuli[j] instanceof CurrentDensityClampStimulus) {
            CurrentDensityClampStimulus stimulus = (CurrentDensityClampStimulus) electricalStimuli[j];
            LocalParameter currentDensityParameter = stimulus.getCurrentDensityParameter();
            tempExp = currentDensityParameter.getExpression();
            tempUnit = currentDensityParameter.getUnitDefinition();
            clampType = "Current Density (deprecated)";
        } else if (electricalStimuli[j] instanceof TotalCurrentClampStimulus) {
            TotalCurrentClampStimulus stimulus = (TotalCurrentClampStimulus) electricalStimuli[j];
            LocalParameter totalCurrentParameter = stimulus.getCurrentParameter();
            tempExp = totalCurrentParameter.getExpression();
            tempUnit = totalCurrentParameter.getUnitDefinition();
            clampType = "Current";
        } else if (electricalStimuli[j] instanceof VoltageClampStimulus) {
            VoltageClampStimulus stimulus = (VoltageClampStimulus) electricalStimuli[j];
            Parameter voltageParameter = stimulus.getVoltageParameter();
            tempExp = voltageParameter.getExpression();
            tempUnit = voltageParameter.getUnitDefinition();
            clampType = "Voltage";
        }
        if (tempExp != null) {
            expStr = tempExp.infix();
            if (tempUnit != null) {
                expStr += "   " + tempUnit.getSymbolUnicode();
            }
        }
        electTable.addCell(createCell(stimName, getFont()));
        electTable.addCell(createCell(currName, getFont()));
        electTable.addCell(createCell(clampType, getFont()));
        electTable.addCell(createCell(expStr, getFont()));
        // add electrode info
        Electrode electrode = electricalStimuli[j].getElectrode();
        if (electrode == null) {
            electTable.addCell(createCell("N/A", getFont()));
        } else {
            Coordinate c = electrode.getPosition();
            String location = c.getX() + ", " + c.getY() + ", " + c.getZ();
            String featureName = electrode.getFeature().getName();
            electTable.addCell(createCell("(" + location + ") in " + featureName, getFont()));
        }
    }
    if (electTable != null) {
        if (memMapSection == null) {
            memMapSection = simContextSection.addSection("Membrane Mapping For " + simContext.getName(), 1);
        }
        electTable.setWidths(widths);
        memMapSection.add(electTable);
    }
    // add temperature
    Table tempTable = getTable(1, 75, 1, 3, 3);
    tempTable.setAlignment(Table.ALIGN_LEFT);
    tempTable.addCell(createCell("Temperature: " + simContext.getTemperatureKelvin() + " K", getFont()));
    if (memMapSection != null) {
        memMapSection.add(tempTable);
    }
}
Also used : MembraneMapping(cbit.vcell.mapping.MembraneMapping) Table(com.lowagie.text.Table) Electrode(cbit.vcell.mapping.Electrode) CurrentDensityClampStimulus(cbit.vcell.mapping.CurrentDensityClampStimulus) Section(com.lowagie.text.Section) StructureMapping(cbit.vcell.mapping.StructureMapping) TotalCurrentClampStimulus(cbit.vcell.mapping.TotalCurrentClampStimulus) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) ElectricalStimulus(cbit.vcell.mapping.ElectricalStimulus) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) FeatureMapping(cbit.vcell.mapping.FeatureMapping) Expression(cbit.vcell.parser.Expression) Coordinate(org.vcell.util.Coordinate) VoltageClampStimulus(cbit.vcell.mapping.VoltageClampStimulus) Parameter(cbit.vcell.model.Parameter) SpeciesContextSpecParameter(cbit.vcell.mapping.SpeciesContextSpec.SpeciesContextSpecParameter) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) GeometryContext(cbit.vcell.mapping.GeometryContext)

Example 32 with Coordinate

use of org.vcell.util.Coordinate in project vcell by virtualcell.

the class FunctionRangeGenerator method getFunctionStatistics.

// 
// note: functionExp should already be flattened to only have symbols for state variables, x, y, z, and t
// 
public static FunctionStatistics getFunctionStatistics(Expression functionExp, VarStatistics[] varStatistics, double[] times, CartesianMesh cartesianMesh, BitSet inDomainBitSetOrig, VariableType variableType) throws /*,int numSamplesPerDim*/
Exception {
    ArrayList<Integer> inDomainIndexesInit = new ArrayList<>();
    // make list of all indexes in domain
    for (int i = inDomainBitSetOrig.nextSetBit(0); i >= 0; i = inDomainBitSetOrig.nextSetBit(i + 1)) {
        if (i == Integer.MAX_VALUE) {
            break;
        }
        inDomainIndexesInit.add(i);
    }
    if (varStatistics.length == 0) {
        double constantValue = functionExp.evaluateConstant();
        double[] minValues = new double[times.length];
        Arrays.fill(minValues, constantValue);
        double[] maxValues = new double[times.length];
        Arrays.fill(maxValues, constantValue);
        return new FunctionStatistics(minValues, maxValues);
    }
    if (varStatistics[0].minValuesOverTime.length != times.length) {
        // happens if viewing data of running sim
        double[] temp = new double[varStatistics[0].minValuesOverTime.length];
        System.arraycopy(times, 0, temp, 0, temp.length);
        times = temp;
    }
    // int numVars = varStatistics.length;
    // Math.min(1000, b)
    long numSamples = (inDomainIndexesInit.size() < 10000 ? inDomainIndexesInit.size() : 10000);
    // long numSamples = numVars*numSamplesPerDim;
    ArrayList<String> symbols = new ArrayList<>();
    // if (functionExp.hasSymbol("x")){
    // numSamples *= numSamplesPerDim;
    // }
    // if (functionExp.hasSymbol("y")){
    // numSamples *= numSamplesPerDim;
    // }
    // if (functionExp.hasSymbol("z")){
    // numSamples *= numSamplesPerDim;
    // }
    // boolean bSampleSpace = functionExp.hasSymbol(ReservedVariable.X.getSyntax()) || functionExp.hasSymbol(ReservedVariable.Y.getSyntax()) || functionExp.hasSymbol(ReservedVariable.Z.getSyntax());
    // 
    // establishes order of values when evaluating, values={t,x,y,z,var1,var2, ... varN}
    // 
    symbols.add("t");
    symbols.add("x");
    symbols.add("y");
    symbols.add("z");
    for (VarStatistics varStat : varStatistics) {
        symbols.add(varStat.stateVariableName);
    }
    SimpleSymbolTable symTable = new SimpleSymbolTable(symbols.toArray(new String[0]));
    functionExp.bindExpression(symTable);
    // loop through time, at each time sample state variables (and x,y,z if necessary) to estimate
    // the min and max values for that time.
    Random rand = new Random(0);
    double[] values = new double[symbols.size()];
    double[] minFunctionValues = new double[times.length];
    double[] maxFunctionValues = new double[times.length];
    // Extent extent = cartesianMesh.getExtent();
    for (int tIndex = 0; tIndex < times.length; tIndex++) {
        ArrayList<Integer> inDomainIndexes = new ArrayList<>(inDomainIndexesInit);
        values[0] = times[tIndex];
        double minValue = Double.POSITIVE_INFINITY;
        double maxValue = Double.NEGATIVE_INFINITY;
        for (int sample = 0; sample < numSamples; sample++) {
            Coordinate coord = null;
            int rndIndex = rand.nextInt(inDomainIndexes.size());
            int index = inDomainIndexes.remove(rndIndex);
            if (variableType.equals(VariableType.MEMBRANE)) {
                coord = cartesianMesh.getCoordinateFromMembraneIndex(index);
            } else if (variableType.equals(VariableType.VOLUME)) {
                coord = cartesianMesh.getCoordinateFromVolumeIndex(index);
            } else {
                throw new Exception("Not implemented " + variableType.getTypeName());
            }
            values[1] = coord.getX();
            values[2] = coord.getY();
            values[3] = coord.getZ();
            for (int varIndex = 0; varIndex < varStatistics.length; varIndex++) {
                double s = rand.nextDouble();
                values[4 + varIndex] = s * varStatistics[varIndex].minValuesOverTime[tIndex] + (1 - s) * varStatistics[varIndex].maxValuesOverTime[tIndex];
            }
            double evaluation = functionExp.evaluateVector(values);
            minValue = Math.min(minValue, evaluation);
            maxValue = Math.max(maxValue, evaluation);
        }
        minFunctionValues[tIndex] = minValue;
        maxFunctionValues[tIndex] = maxValue;
    // System.out.println("tIndex="+tIndex+" min="+minValue+" max="+maxValue);
    }
    FunctionStatistics functionStats = new FunctionStatistics(minFunctionValues, maxFunctionValues);
    return functionStats;
}
Also used : ArrayList(java.util.ArrayList) SimpleSymbolTable(cbit.vcell.parser.SimpleSymbolTable) Random(java.util.Random) Coordinate(org.vcell.util.Coordinate)

Example 33 with Coordinate

use of org.vcell.util.Coordinate in project vcell by virtualcell.

the class ParticleDataBlock method readParticleData.

private void readParticleData(List<String> lines) throws DataAccessException {
    String lastSpecies = null;
    List<Coordinate> working = null;
    for (String line : lines) {
        try {
            CommentStringTokenizer st = new CommentStringTokenizer(line);
            String sp = st.nextToken();
            if (!sp.equals(lastSpecies)) {
                lastSpecies = sp;
                working = fetch(sp);
            }
            double x = Double.parseDouble(st.nextToken());
            double y = Double.parseDouble(st.nextToken());
            double z = Double.parseDouble(st.nextToken());
            Coordinate c = new Coordinate(x, y, z);
            working.add(c);
        } catch (Exception exc) {
            throw new DataAccessException("Particle data file invalid. " + exc.getMessage());
        }
    }
}
Also used : Coordinate(org.vcell.util.Coordinate) CommentStringTokenizer(org.vcell.util.CommentStringTokenizer) IOException(java.io.IOException) DataAccessException(org.vcell.util.DataAccessException) DataAccessException(org.vcell.util.DataAccessException)

Example 34 with Coordinate

use of org.vcell.util.Coordinate in project vcell by virtualcell.

the class SpatialSelectionMembrane method getIndexSamples.

/**
 * Insert the method's description here.
 * Creation date: (7/18/01 5:59:31 PM)
 * @return int[]
 */
public SSHelper getIndexSamples() {
    int[] membraneSegmentSelectionIndexes = getCurveSelectionInfo().getSegmentsInSelectionOrder();
    cbit.vcell.geometry.SampledCurve sampledCurve = (cbit.vcell.geometry.SampledCurve) getCurveSelectionInfo().getCurve();
    if (membraneSegmentSelectionIndexes.length == 0) {
        return null;
    }
    if (membraneSegmentSelectionIndexes.length == 1) {
        org.vcell.util.Coordinate[] twoCP = sampledCurve.getControlPointsForSegment(membraneSegmentSelectionIndexes[0]);
        return new SSHelper(new Coordinate[] { twoCP[0], twoCP[1] }, new int[] { fieldSampledDataIndexes[membraneSegmentSelectionIndexes[0]], fieldSampledDataIndexes[membraneSegmentSelectionIndexes[0]] }, getVariableType(), null);
    }
    int[] indexes = new int[membraneSegmentSelectionIndexes.length + 1];
    Coordinate[][] segCPArr = new Coordinate[membraneSegmentSelectionIndexes.length][];
    Coordinate[] wcArr = new Coordinate[indexes.length];
    for (int i = 0; i < membraneSegmentSelectionIndexes.length; i += 1) {
        segCPArr[i] = sampledCurve.getControlPointsForSegment(membraneSegmentSelectionIndexes[i]);
    }
    for (int i = 0; i < membraneSegmentSelectionIndexes.length; i += 1) {
        org.vcell.util.Coordinate[] twoCP = segCPArr[i];
        Coordinate firstSelDirection = null;
        Coordinate secondSelDirection = null;
        if (i != (membraneSegmentSelectionIndexes.length - 1)) {
            org.vcell.util.Coordinate[] twoCPNext = segCPArr[i + 1];
            if (twoCP[0].compareEqual(twoCPNext[0]) || twoCP[0].compareEqual(twoCPNext[1])) {
                firstSelDirection = twoCP[1];
                secondSelDirection = twoCP[0];
            } else {
                firstSelDirection = twoCP[0];
                secondSelDirection = twoCP[1];
            }
        } else {
            org.vcell.util.Coordinate[] twoCPPrev = segCPArr[i - 1];
            if (twoCP[0].compareEqual(twoCPPrev[0]) || twoCP[0].compareEqual(twoCPPrev[1])) {
                firstSelDirection = twoCP[0];
                secondSelDirection = twoCP[1];
            } else {
                firstSelDirection = twoCP[1];
                secondSelDirection = twoCP[0];
            }
        }
        indexes[i] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
        wcArr[i] = firstSelDirection;
        if (i == (membraneSegmentSelectionIndexes.length - 1)) {
            indexes[i + 1] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
            wcArr[i + 1] = secondSelDirection;
        }
    // if(i == 0){
    // indexes[2*i] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
    // wcArr[2*i] = firstSelDirection;
    // indexes[(2*i)+1] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
    // wcArr[(2*i)+1] = offsetCoordinate(secondSelDirection,firstSelDirection);
    // }else if (i  == (membraneSegmentSelectionIndexes.length-1)){
    // indexes[2*i] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
    // wcArr[2*i] = offsetCoordinate(firstSelDirection,secondSelDirection);
    // indexes[(2*i)+1] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
    // wcArr[(2*i)+1] = secondSelDirection;
    // }else{
    // indexes[2*i] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
    // wcArr[2*i] = offsetCoordinate(firstSelDirection,secondSelDirection);
    // indexes[(2*i)+1] = fieldSampledDataIndexes[membraneSegmentSelectionIndexes[i]];
    // wcArr[(2*i)+1] = offsetCoordinate(secondSelDirection,firstSelDirection);
    // }
    }
    return new SSHelper(wcArr, indexes, getVariableType(), null);
}
Also used : Coordinate(org.vcell.util.Coordinate)

Example 35 with Coordinate

use of org.vcell.util.Coordinate in project vcell by virtualcell.

the class SpatialSelectionVolume method lineMeshFaceIntersect3D.

/**
 * Insert the method's description here.
 * Creation date: (10/6/2004 8:09:38 AM)
 * @return cbit.vcell.geometry.Coordinate
 */
private static Coordinate lineMeshFaceIntersect3D(Coordinate mesh1, Coordinate mesh2, Coordinate cp1, Coordinate cp2) {
    // http://astronomy.swin.edu.au/~pbourke/geometry/planeline/
    // mesh1,mesh2 are CartesianMesh Coordinates from from
    // adjacent elements that share a face (points on the face Normal)
    // 
    // cp1,cp2 are points on a line to test intersection
    // N is plane normal vector
    // P3 is point on plane
    // 
    // P1,P2 are points on line
    // P = P1 + u(P2-P1) -- equation of line
    // 
    // determine u where line intersects plane
    // u = (N dot (P3-P1))/(N dot (P2-P1));
    // epsilon
    final double EPS = 1.0E-12;
    cbit.vcell.render.Vect3d p1 = new cbit.vcell.render.Vect3d(cp1.getX(), cp1.getY(), cp1.getZ());
    cbit.vcell.render.Vect3d p2 = new cbit.vcell.render.Vect3d(cp2.getX(), cp2.getY(), cp2.getZ());
    cbit.vcell.render.Vect3d n1 = new cbit.vcell.render.Vect3d(mesh1.getX(), mesh1.getY(), mesh1.getZ());
    cbit.vcell.render.Vect3d n2 = new cbit.vcell.render.Vect3d(mesh2.getX(), mesh2.getY(), mesh2.getZ());
    cbit.vcell.render.Vect3d n = cbit.vcell.render.Vect3d.sub(n2, n1);
    cbit.vcell.render.Vect3d p3 = new cbit.vcell.render.Vect3d(n);
    p3.scale(.5);
    p3.add(n1);
    n.unit();
    cbit.vcell.render.Vect3d p3minp1 = cbit.vcell.render.Vect3d.sub(p3, p1);
    cbit.vcell.render.Vect3d p2minp1 = cbit.vcell.render.Vect3d.sub(p2, p1);
    double denominator = n.dot(p2minp1);
    // if(denominator == 0.0){
    if (Math.abs(denominator) < EPS) {
        // line paralell to plane or on plane
        return null;
    }
    double numerator = n.dot(p3minp1);
    double u = numerator / denominator;
    p2minp1.scale(u);
    p1.add(p2minp1);
    Coordinate finalCoord = new Coordinate(p1.getX(), p1.getY(), p1.getZ());
    return finalCoord;
}
Also used : Coordinate(org.vcell.util.Coordinate)

Aggregations

Coordinate (org.vcell.util.Coordinate)81 CoordinateIndex (org.vcell.util.CoordinateIndex)16 SampledCurve (cbit.vcell.geometry.SampledCurve)11 SinglePoint (cbit.vcell.geometry.SinglePoint)11 ControlPointCurve (cbit.vcell.geometry.ControlPointCurve)10 CurveSelectionInfo (cbit.vcell.geometry.CurveSelectionInfo)6 Expression (cbit.vcell.parser.Expression)6 CartesianMesh (cbit.vcell.solvers.CartesianMesh)6 Vector (java.util.Vector)6 Extent (org.vcell.util.Extent)6 VariableType (cbit.vcell.math.VariableType)5 ISize (org.vcell.util.ISize)5 AnalyticSubVolume (cbit.vcell.geometry.AnalyticSubVolume)4 Curve (cbit.vcell.geometry.Curve)4 SubVolume (cbit.vcell.geometry.SubVolume)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Origin (org.vcell.util.Origin)4 VCImageUncompressed (cbit.image.VCImageUncompressed)3 Line (cbit.vcell.geometry.Line)3