Search in sources :

Example 16 with SpeciesContextMapping

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

the class StructureAnalyzer method refreshTotalDependancies.

/**
 * This method was created by a SmartGuide.
 * @param b cbit.vcell.math.Matrix
 * @param vars java.lang.String[]
 */
static StructureAnalyzer.Dependency[] refreshTotalDependancies(RationalMatrix nullSpaceMatrix, SpeciesContextMapping[] speciesContextMappings, MathMapping_4_8 argMathMapping, boolean bFast) throws Exception {
    // System.out.println("StructureAnalyzer.refreshTotalDependancies()");
    SimulationContext argSimContext = argMathMapping.getSimulationContext();
    if (nullSpaceMatrix == null) {
        // System.out.println("the matrix has full rank, there are no dependencies");
        return new StructureAnalyzer.Dependency[0];
    }
    if (speciesContextMappings.length != nullSpaceMatrix.getNumCols()) {
        // throw new Exception("varName array not same dimension as b matrix");
        System.out.println("varName array not same dimension as b matrix");
        nullSpaceMatrix.show();
        for (int i = 0; i < speciesContextMappings.length; i++) {
            System.out.println("scm[" + i + "] is " + speciesContextMappings[i].getSpeciesContext().getName());
        }
    }
    // System.out.println("there are "+nullSpaceMatrix.rows+" dependencies");
    Vector<Dependency> dependencyList = new Vector<Dependency>();
    for (int i = 0; i < nullSpaceMatrix.getNumRows(); i++) {
        // 
        // find first variable
        // 
        Expression exp = null;
        Expression constantExp = null;
        String constantName = null;
        SpeciesContextMapping firstSCM = null;
        boolean bFirst = true;
        for (int j = 0; j < nullSpaceMatrix.getNumCols(); j++) {
            RationalNumber coeff = nullSpaceMatrix.get_elem(i, j);
            if (coeff.doubleValue() != 0.0) {
                if (bFirst) {
                    if (coeff.doubleValue() != 1.0) {
                        System.out.println("i=" + i + " j=" + j);
                        nullSpaceMatrix.show();
                        throw new Exception("expecting a coefficient of 1.0, instead coeff = " + coeff.infix());
                    }
                    firstSCM = speciesContextMappings[j];
                    // 
                    // first term of dependancy expression   ("K_CalciumCyt")
                    // 
                    SpeciesContext firstSC = firstSCM.getSpeciesContext();
                    constantName = "K_" + firstSC.getName() + "_total";
                    exp = new Expression(constantName);
                    // 
                    // first term of K expression
                    // 
                    StructureMapping firstSM = argSimContext.getGeometryContext().getStructureMapping(firstSC.getStructure());
                    SpeciesContextSpec firstSCS = argSimContext.getReactionContext().getSpeciesContextSpec(firstSC);
                    SymbolTableEntry scSTE = null;
                    if (bFast) {
                        scSTE = firstSCS.getSpeciesContext();
                    } else {
                        scSTE = firstSCS.getParameterFromRole(SpeciesContextSpec.ROLE_InitialConcentration);
                    }
                    constantExp = Expression.mult(new Expression(coeff), firstSM.getNormalizedConcentrationCorrection(argSimContext, argMathMapping), new Expression(scSTE, argMathMapping.getNameScope()));
                    bFirst = false;
                } else {
                    // 
                    // add term to dependancy expression     (" - 2*IP3Cyt ")
                    // 
                    SpeciesContextMapping scm = speciesContextMappings[j];
                    SpeciesContext sc = scm.getSpeciesContext();
                    StructureMapping sm = argSimContext.getGeometryContext().getStructureMapping(sc.getStructure());
                    SpeciesContextSpec scs = argSimContext.getReactionContext().getSpeciesContextSpec(sc);
                    exp = Expression.add(exp, Expression.negate(Expression.mult(new Expression(coeff), sm.getNormalizedConcentrationCorrection(argSimContext, argMathMapping), new Expression(sc, argMathMapping.getNameScope()))));
                    // 
                    // add term to K expression
                    // 
                    SymbolTableEntry scSTE = null;
                    if (bFast) {
                        scSTE = sc;
                    } else {
                        scSTE = scs.getParameterFromRole(SpeciesContextSpec.ROLE_InitialConcentration);
                    }
                    constantExp = Expression.add(constantExp, Expression.mult(new Expression(coeff), sm.getNormalizedConcentrationCorrection(argSimContext, argMathMapping), new Expression(scSTE, argMathMapping.getNameScope())));
                }
            }
        }
        if (firstSCM != null) {
            // 
            // store totalMass parameter (e.g. K_xyz_total = xyz_init + wzy_init)
            // 
            // MathMapping.MathMappingParameter totalMassParameter = mathMapping.addMathMappingParameter(constantName,constantExp.flatten(),MathMapping.PARAMETER_ROLE_TOTALMASS,cbit.vcell.units.VCUnitDefinition.UNIT_uM);
            // 
            // store dependency parameter (e.g. xyz = K_xyz_total - wzy)
            // 
            StructureMapping sm = argSimContext.getGeometryContext().getStructureMapping(firstSCM.getSpeciesContext().getStructure());
            exp = Expression.mult(exp, Expression.invert(sm.getNormalizedConcentrationCorrection(argSimContext, argMathMapping)));
            exp = exp.flatten();
            // exp.bindExpression(mathMapping_temp);
            // firstSCM.setDependencyExpression(exp);
            Dependency dependency = new Dependency();
            dependency.dependencyExpression = exp;
            dependency.speciesContextMapping = firstSCM;
            dependency.invariantSymbolName = constantName;
            dependency.conservedMoietyExpression = constantExp.flatten();
            dependencyList.add(dependency);
        }
    }
    return (StructureAnalyzer.Dependency[]) BeanUtils.getArray(dependencyList, StructureAnalyzer.Dependency.class);
}
Also used : SpeciesContextMapping(cbit.vcell.mapping.SpeciesContextMapping) SpeciesContext(cbit.vcell.model.SpeciesContext) SimulationContext(cbit.vcell.mapping.SimulationContext) SpeciesContextSpec(cbit.vcell.mapping.SpeciesContextSpec) StructureMapping(cbit.vcell.mapping.StructureMapping) SymbolTableEntry(cbit.vcell.parser.SymbolTableEntry) Expression(cbit.vcell.parser.Expression) RationalNumber(cbit.vcell.matrix.RationalNumber) Vector(java.util.Vector)

Aggregations

SpeciesContextMapping (cbit.vcell.mapping.SpeciesContextMapping)16 Expression (cbit.vcell.parser.Expression)13 SpeciesContextSpec (cbit.vcell.mapping.SpeciesContextSpec)10 ReactionSpec (cbit.vcell.mapping.ReactionSpec)8 SpeciesContext (cbit.vcell.model.SpeciesContext)8 StructureMapping (cbit.vcell.mapping.StructureMapping)7 Structure (cbit.vcell.model.Structure)7 ReactionStep (cbit.vcell.model.ReactionStep)6 Vector (java.util.Vector)6 MappingException (cbit.vcell.mapping.MappingException)5 MembraneMapping (cbit.vcell.mapping.MembraneMapping)5 Feature (cbit.vcell.model.Feature)5 Membrane (cbit.vcell.model.Membrane)5 Model (cbit.vcell.model.Model)4 ModelUnitSystem (cbit.vcell.model.ModelUnitSystem)4 VCUnitDefinition (cbit.vcell.units.VCUnitDefinition)4 DiffusionDummyReactionStep (cbit.vcell.mapping.DiffusionDummyReactionStep)3 DummyReactionStep (cbit.vcell.mapping.DummyReactionStep)3 EventDummyReactionStep (cbit.vcell.mapping.EventDummyReactionStep)3 FeatureMapping (cbit.vcell.mapping.FeatureMapping)3