Search in sources :

Example 6 with TotalCurrentClampStimulus

use of cbit.vcell.mapping.TotalCurrentClampStimulus in project vcell by virtualcell.

the class CurrentClampElectricalDevice method initializeParameters.

private void initializeParameters() throws ExpressionException {
    ElectricalDevice.ElectricalDeviceParameter[] parameters = new ElectricalDevice.ElectricalDeviceParameter[3];
    // 
    // set the transmembrane current (total current, if necessary derive it from the current density).
    // 
    ElectricalDeviceParameter transMembraneCurrent = null;
    ModelUnitSystem modelUnitSystem = mathMapping.getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition currentUnit = modelUnitSystem.getCurrentUnit();
    if (currentClampStimulus instanceof TotalCurrentClampStimulus) {
        TotalCurrentClampStimulus stimulus = (TotalCurrentClampStimulus) currentClampStimulus;
        LocalParameter currentParameter = stimulus.getCurrentParameter();
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], new Expression(currentParameter.getExpression()), ROLE_TransmembraneCurrent, currentUnit);
    } else if (currentClampStimulus instanceof CurrentDensityClampStimulus) {
        CurrentDensityClampStimulus stimulus = (CurrentDensityClampStimulus) currentClampStimulus;
        LocalParameter currentDensityParameter = stimulus.getCurrentDensityParameter();
        // 
        // here we have to determine the expression for current (from current density).
        // 
        Feature feature1 = currentClampStimulus.getElectrode().getFeature();
        Feature feature2 = mathMapping.getSimulationContext().getGroundElectrode().getFeature();
        Membrane membrane = null;
        StructureTopology structTopology = mathMapping.getSimulationContext().getModel().getStructureTopology();
        if (structTopology.getParentStructure(feature1) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature1)) == feature2) {
            membrane = ((Membrane) structTopology.getParentStructure(feature1));
        } else if (structTopology.getParentStructure(feature2) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature2)) == feature1) {
            membrane = ((Membrane) structTopology.getParentStructure(feature2));
        }
        if (membrane == null) {
            throw new RuntimeException("current clamp based on current density crosses multiple membranes, unable to " + "determine single membrane to convert current density into current in Application '" + mathMapping.getSimulationContext().getName() + "'.");
        }
        MembraneMapping membraneMapping = (MembraneMapping) mathMapping.getSimulationContext().getGeometryContext().getStructureMapping(membrane);
        StructureMappingParameter sizeParameter = membraneMapping.getSizeParameter();
        Expression area = null;
        if (mathMapping.getSimulationContext().getGeometry().getDimension() == 0 && (sizeParameter.getExpression() == null || sizeParameter.getExpression().isZero())) {
            area = membraneMapping.getNullSizeParameterValue();
        } else {
            area = new Expression(sizeParameter, mathMapping.getNameScope());
        }
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], Expression.mult(new Expression(currentDensityParameter.getExpression()), area), ROLE_TransmembraneCurrent, currentUnit);
    } else {
        throw new RuntimeException("unexpected current clamp stimulus type : " + currentClampStimulus.getClass().getName());
    }
    ElectricalDeviceParameter totalCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TotalCurrent], new Expression(transMembraneCurrent, getNameScope()), ROLE_TotalCurrent, currentUnit);
    ElectricalDeviceParameter voltage = new ElectricalDeviceParameter(DefaultNames[ROLE_Voltage], null, ROLE_Voltage, modelUnitSystem.getVoltageUnit());
    parameters[0] = totalCurrent;
    parameters[1] = transMembraneCurrent;
    parameters[2] = voltage;
    // 
    // add any user-defined parameters
    // 
    LocalParameter[] stimulusParameters = currentClampStimulus.getLocalParameters();
    for (int i = 0; stimulusParameters != null && i < stimulusParameters.length; i++) {
        if (stimulusParameters[i].getRole() == ElectricalStimulus.ElectricalStimulusParameterType.UserDefined) {
            ElectricalDeviceParameter newParam = new ElectricalDeviceParameter(stimulusParameters[i].getName(), new Expression(stimulusParameters[i].getExpression()), ROLE_UserDefined, stimulusParameters[i].getUnitDefinition());
            parameters = (ElectricalDeviceParameter[]) BeanUtils.addElement(parameters, newParam);
        }
    }
    setParameters(parameters);
}
Also used : MembraneMapping(cbit.vcell.mapping.MembraneMapping) StructureTopology(cbit.vcell.model.Model.StructureTopology) CurrentDensityClampStimulus(cbit.vcell.mapping.CurrentDensityClampStimulus) StructureMappingParameter(cbit.vcell.mapping.StructureMapping.StructureMappingParameter) TotalCurrentClampStimulus(cbit.vcell.mapping.TotalCurrentClampStimulus) Feature(cbit.vcell.model.Feature) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 7 with TotalCurrentClampStimulus

use of cbit.vcell.mapping.TotalCurrentClampStimulus in project vcell by virtualcell.

the class CurrentClampElectricalDevice method initializeParameters.

private void initializeParameters() throws ExpressionException {
    ElectricalDevice.ElectricalDeviceParameter[] parameters = new ElectricalDevice.ElectricalDeviceParameter[3];
    // 
    // set the transmembrane current (total current, if necessary derive it from the current density).
    // 
    ElectricalDeviceParameter transMembraneCurrent = null;
    ModelUnitSystem modelUnitSystem = mathMapping_4_8.getSimulationContext().getModel().getUnitSystem();
    VCUnitDefinition currentUnit = modelUnitSystem.getCurrentUnit();
    if (currentClampStimulus instanceof TotalCurrentClampStimulus) {
        TotalCurrentClampStimulus stimulus = (TotalCurrentClampStimulus) currentClampStimulus;
        LocalParameter currentParameter = stimulus.getCurrentParameter();
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], new Expression(currentParameter.getExpression()), ROLE_TransmembraneCurrent, currentUnit);
    } else if (currentClampStimulus instanceof CurrentDensityClampStimulus) {
        CurrentDensityClampStimulus stimulus = (CurrentDensityClampStimulus) currentClampStimulus;
        LocalParameter currentDensityParameter = stimulus.getCurrentDensityParameter();
        // 
        // here we have to determine the expression for current (from current density).
        // 
        Feature feature1 = currentClampStimulus.getElectrode().getFeature();
        Feature feature2 = mathMapping_4_8.getSimulationContext().getGroundElectrode().getFeature();
        Membrane membrane = null;
        StructureTopology structTopology = mathMapping_4_8.getSimulationContext().getModel().getStructureTopology();
        if (structTopology.getParentStructure(feature1) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature1)) == feature2) {
            membrane = ((Membrane) structTopology.getParentStructure(feature1));
        } else if (structTopology.getParentStructure(feature2) != null && structTopology.getOutsideFeature((Membrane) structTopology.getParentStructure(feature2)) == feature1) {
            membrane = ((Membrane) structTopology.getParentStructure(feature2));
        }
        if (membrane == null) {
            throw new RuntimeException("current clamp based on current density crosses multiple membranes, unable to " + "determine single membrane to convert current density into current in Application '" + mathMapping_4_8.getSimulationContext().getName() + "'.");
        }
        MembraneMapping membraneMapping = (MembraneMapping) mathMapping_4_8.getSimulationContext().getGeometryContext().getStructureMapping(membrane);
        StructureMappingParameter sizeParameter = membraneMapping.getSizeParameter();
        Expression area = null;
        if (mathMapping_4_8.getSimulationContext().getGeometry().getDimension() == 0 && (sizeParameter.getExpression() == null || sizeParameter.getExpression().isZero())) {
            area = membraneMapping.getNullSizeParameterValue();
        } else {
            area = new Expression(sizeParameter, mathMapping_4_8.getNameScope());
        }
        transMembraneCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TransmembraneCurrent], Expression.mult(new Expression(currentDensityParameter.getExpression()), area), ROLE_TransmembraneCurrent, currentUnit);
    } else {
        throw new RuntimeException("unexpected current clamp stimulus type : " + currentClampStimulus.getClass().getName());
    }
    ElectricalDeviceParameter totalCurrent = new ElectricalDeviceParameter(DefaultNames[ROLE_TotalCurrent], new Expression(transMembraneCurrent, getNameScope()), ROLE_TotalCurrent, currentUnit);
    ElectricalDeviceParameter voltage = new ElectricalDeviceParameter(DefaultNames[ROLE_Voltage], null, ROLE_Voltage, modelUnitSystem.getVoltageUnit());
    parameters[0] = totalCurrent;
    parameters[1] = transMembraneCurrent;
    parameters[2] = voltage;
    // 
    // add any user-defined parameters
    // 
    LocalParameter[] stimulusParameters = currentClampStimulus.getLocalParameters();
    for (int i = 0; stimulusParameters != null && i < stimulusParameters.length; i++) {
        if (stimulusParameters[i].getRole() == ElectricalStimulus.ElectricalStimulusParameterType.UserDefined) {
            ElectricalDeviceParameter newParam = new ElectricalDeviceParameter(stimulusParameters[i].getName(), new Expression(stimulusParameters[i].getExpression()), ROLE_UserDefined, stimulusParameters[i].getUnitDefinition());
            parameters = (ElectricalDeviceParameter[]) BeanUtils.addElement(parameters, newParam);
        }
    }
    setParameters(parameters);
}
Also used : MembraneMapping(cbit.vcell.mapping.MembraneMapping) StructureTopology(cbit.vcell.model.Model.StructureTopology) CurrentDensityClampStimulus(cbit.vcell.mapping.CurrentDensityClampStimulus) StructureMappingParameter(cbit.vcell.mapping.StructureMapping.StructureMappingParameter) TotalCurrentClampStimulus(cbit.vcell.mapping.TotalCurrentClampStimulus) Feature(cbit.vcell.model.Feature) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) VCUnitDefinition(cbit.vcell.units.VCUnitDefinition) Expression(cbit.vcell.parser.Expression) Membrane(cbit.vcell.model.Membrane) ModelUnitSystem(cbit.vcell.model.ModelUnitSystem)

Example 8 with TotalCurrentClampStimulus

use of cbit.vcell.mapping.TotalCurrentClampStimulus in project vcell by virtualcell.

the class SimulationContextDbDriver method assignStimuliSQL.

/**
 * This method was created in VisualAge.
 * @param simContext cbit.vcell.mapping.SimulationContext
 */
private void assignStimuliSQL(Connection con, KeyValue simContextKey, SimulationContext simContext) throws SQLException, DataAccessException {
    String sql;
    sql = " SELECT " + "*" + " FROM " + stimulusTable.getTableName() + " WHERE " + stimulusTable.simContextRef + " = " + simContextKey;
    Statement stmt = con.createStatement();
    try {
        ResultSet rset = stmt.executeQuery(sql);
        while (rset.next()) {
            try {
                // KeyValue key = new KeyValue(rset.getBigDecimal(stimulusTable.id.toString()));
                KeyValue structureRef = new KeyValue(rset.getBigDecimal(stimulusTable.structRef.toString()));
                // KeyValue simContextRef = new KeyValue(rset.getBigDecimal(stimulusTable.simContextRef.toString()));
                // 
                // lookup structure from SimulationContext by its key
                // 
                // DBCache will not always give same instance consistently (usually this is
                // fixed up later in the ReferenceResolver at the Client).
                // 
                Structure theStructure = null;
                Structure[] structureArray = simContext.getModel().getStructures();
                for (int i = 0; i < structureArray.length; i++) {
                    Structure structure = structureArray[i];
                    if (structure.getKey().compareEqual(structureRef)) {
                        theStructure = structure;
                        break;
                    }
                }
                String name = rset.getString(stimulusTable.name.toString());
                if (rset.wasNull()) {
                    name = null;
                }
                int stimulusType = rset.getInt(stimulusTable.stimulusType.toString());
                Expression exp = null;
                String expString = rset.getString(stimulusTable.expression.toString());
                if (rset.wasNull()) {
                    exp = null;
                } else {
                    exp = new Expression(TokenMangler.getSQLRestoredString(expString));
                }
                double posX = rset.getBigDecimal(stimulusTable.positionX.toString()).doubleValue();
                double posY = rset.getBigDecimal(stimulusTable.positionY.toString()).doubleValue();
                double posZ = rset.getBigDecimal(stimulusTable.positionZ.toString()).doubleValue();
                org.vcell.util.CommentStringTokenizer paramsCST = null;
                String paramsS = rset.getString(StimulusTable.table.params.toString());
                if (!rset.wasNull()) {
                    paramsCST = new org.vcell.util.CommentStringTokenizer(org.vcell.util.TokenMangler.getSQLRestoredString(paramsS));
                }
                if (stimulusType == StimulusTable.GROUND_ELECTRODE) {
                    // 
                    // an Electrode (ground)
                    // 
                    Electrode groundElectrode = new Electrode((Feature) theStructure, new org.vcell.util.Coordinate(posX, posY, posZ));
                    simContext.setGroundElectrode(groundElectrode);
                } else if (stimulusType == StimulusTable.TOTALCURRENT_CLAMP_STIMULUS) {
                    Electrode electrode = new Electrode((Feature) theStructure, new org.vcell.util.Coordinate(posX, posY, posZ));
                    TotalCurrentClampStimulus stimulus = new TotalCurrentClampStimulus(electrode, name, exp, simContext);
                    stimulus.parameterVCMLSet(paramsCST);
                    ElectricalStimulus[] newStimuli = (ElectricalStimulus[]) org.vcell.util.BeanUtils.addElement(simContext.getElectricalStimuli(), stimulus);
                    simContext.setElectricalStimuli(newStimuli);
                } else if (stimulusType == StimulusTable.CURRENTDENSITY_CLAMP_STIMULUS) {
                    Electrode electrode = new Electrode((Feature) theStructure, new org.vcell.util.Coordinate(posX, posY, posZ));
                    CurrentDensityClampStimulus stimulus = new CurrentDensityClampStimulus(electrode, name, exp, simContext);
                    stimulus.parameterVCMLSet(paramsCST);
                    ElectricalStimulus[] newStimuli = (ElectricalStimulus[]) org.vcell.util.BeanUtils.addElement(simContext.getElectricalStimuli(), stimulus);
                    simContext.setElectricalStimuli(newStimuli);
                } else if (stimulusType == StimulusTable.VOLTAGE_CLAMP_STIMULUS) {
                    Electrode electrode = new Electrode((Feature) theStructure, new org.vcell.util.Coordinate(posX, posY, posZ));
                    VoltageClampStimulus stimulus = new VoltageClampStimulus(electrode, name, exp, simContext);
                    stimulus.parameterVCMLSet(paramsCST);
                    ElectricalStimulus[] newStimuli = (ElectricalStimulus[]) org.vcell.util.BeanUtils.addElement(simContext.getElectricalStimuli(), stimulus);
                    simContext.setElectricalStimuli(newStimuli);
                } else {
                    throw new DataAccessException("unknown stimulusType <" + stimulusType + ">");
                }
            } catch (ExpressionException | PropertyVetoException e) {
                lg.error(e.getMessage(), e);
            }
        }
    } finally {
        stmt.close();
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) Feature(cbit.vcell.model.Feature) TotalCurrentClampStimulus(cbit.vcell.mapping.TotalCurrentClampStimulus) ExpressionException(cbit.vcell.parser.ExpressionException) VoltageClampStimulus(cbit.vcell.mapping.VoltageClampStimulus) ResultSet(java.sql.ResultSet) Structure(cbit.vcell.model.Structure) DataAccessException(org.vcell.util.DataAccessException) Electrode(cbit.vcell.mapping.Electrode) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CurrentDensityClampStimulus(cbit.vcell.mapping.CurrentDensityClampStimulus) PropertyVetoException(java.beans.PropertyVetoException) ElectricalStimulus(cbit.vcell.mapping.ElectricalStimulus) Expression(cbit.vcell.parser.Expression)

Example 9 with TotalCurrentClampStimulus

use of cbit.vcell.mapping.TotalCurrentClampStimulus in project vcell by virtualcell.

the class StimulusTable method getSQLValueList.

/**
 * This method was created in VisualAge.
 * @return java.lang.String
 * @param key KeyValue
 * @param modelName java.lang.String
 */
public String getSQLValueList(InsertHashtable hash, KeyValue Key, KeyValue simContextKey, ElectricalStimulus stimulus) throws DataAccessException {
    KeyValue structureKey = hash.getDatabaseKey(stimulus.getElectrode().getFeature());
    if (structureKey == null) {
        structureKey = stimulus.getElectrode().getFeature().getKey();
        if (structureKey == null) {
            throw new DataAccessException("no key for structure " + stimulus.getElectrode().getFeature());
        }
    }
    Expression exp = null;
    int stimulusType = UNKNOWN_STIMULUS;
    if (stimulus instanceof CurrentDensityClampStimulus) {
        stimulusType = CURRENTDENSITY_CLAMP_STIMULUS;
        LocalParameter currentDensityParameter = ((CurrentDensityClampStimulus) stimulus).getCurrentDensityParameter();
        exp = currentDensityParameter.getExpression();
    } else if (stimulus instanceof TotalCurrentClampStimulus) {
        stimulusType = TOTALCURRENT_CLAMP_STIMULUS;
        LocalParameter totalCurrentParameter = ((TotalCurrentClampStimulus) stimulus).getCurrentParameter();
        exp = totalCurrentParameter.getExpression();
    } else if (stimulus instanceof VoltageClampStimulus) {
        stimulusType = VOLTAGE_CLAMP_STIMULUS;
        LocalParameter voltageParameter = ((VoltageClampStimulus) stimulus).getVoltageParameter();
        exp = voltageParameter.getExpression();
    } else {
        throw new DataAccessException("storage for Stimulus type " + stimulus.getClass().getName() + " not yet supported");
    }
    java.io.StringWriter esParameterWriter = new java.io.StringWriter();
    java.io.PrintWriter pw = new java.io.PrintWriter(esParameterWriter);
    stimulus.parameterVCMLWrite(pw);
    pw.flush();
    pw.close();
    StringBuffer buffer = new StringBuffer();
    buffer.append("(");
    buffer.append(Key + ",");
    buffer.append(structureKey + ",");
    buffer.append(simContextKey + ",");
    buffer.append("'" + stimulus.getName() + "',");
    buffer.append(stimulusType + ",");
    buffer.append("'" + TokenMangler.getSQLEscapedString(exp.infix()) + "',");
    buffer.append(stimulus.getElectrode().getPosition().getX() + ",");
    buffer.append(stimulus.getElectrode().getPosition().getY() + ",");
    buffer.append(stimulus.getElectrode().getPosition().getZ() + ",");
    buffer.append("'" + TokenMangler.getSQLEscapedString(esParameterWriter.getBuffer().toString()) + "'");
    buffer.append(")");
    return buffer.toString();
}
Also used : KeyValue(org.vcell.util.document.KeyValue) CurrentDensityClampStimulus(cbit.vcell.mapping.CurrentDensityClampStimulus) TotalCurrentClampStimulus(cbit.vcell.mapping.TotalCurrentClampStimulus) LocalParameter(cbit.vcell.mapping.ParameterContext.LocalParameter) Expression(cbit.vcell.parser.Expression) VoltageClampStimulus(cbit.vcell.mapping.VoltageClampStimulus) DataAccessException(org.vcell.util.DataAccessException)

Aggregations

CurrentDensityClampStimulus (cbit.vcell.mapping.CurrentDensityClampStimulus)9 TotalCurrentClampStimulus (cbit.vcell.mapping.TotalCurrentClampStimulus)9 Expression (cbit.vcell.parser.Expression)8 VoltageClampStimulus (cbit.vcell.mapping.VoltageClampStimulus)7 LocalParameter (cbit.vcell.mapping.ParameterContext.LocalParameter)6 ElectricalStimulus (cbit.vcell.mapping.ElectricalStimulus)5 Electrode (cbit.vcell.mapping.Electrode)5 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)5 MembraneMapping (cbit.vcell.mapping.MembraneMapping)4 Feature (cbit.vcell.model.Feature)4 StructureTopology (cbit.vcell.model.Model.StructureTopology)3 StructureMappingParameter (cbit.vcell.mapping.StructureMapping.StructureMappingParameter)2 Membrane (cbit.vcell.model.Membrane)2 Model (cbit.vcell.model.Model)2 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)2 Structure (cbit.vcell.model.Structure)2 Element (org.jdom.Element)2 DataAccessException (org.vcell.util.DataAccessException)2 KeyValue (org.vcell.util.document.KeyValue)2 Edge (cbit.util.graph.Edge)1