use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class NFsimXMLWriter method getListOfFunctions.
private static Element getListOfFunctions(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
Element listOfParametersElement = new Element("ListOfFunctions");
for (Variable var : simulationSymbolTable.getVariables()) {
Double value = null;
if (var instanceof Constant || var instanceof Function) {
Expression valExpression = var.getExpression();
Expression substitutedValExpr = null;
try {
substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
}
try {
value = substitutedValExpr.evaluateConstant();
} catch (ExpressionException e) {
System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
}
Element functionElement = new Element("Function");
functionElement.setAttribute("id", var.getName());
if (value != null) {
// parameter, see getListOfParameters() above
continue;
} else {
Element listOfReferencesElement = new Element("ListOfReferences");
String[] references = valExpression.getSymbols();
for (int i = 0; i < references.length; i++) {
String reference = references[i];
Element referenceElement = new Element("Reference");
referenceElement.setAttribute("name", reference);
Variable referenceVariable = simulationSymbolTable.getVariable(reference);
Double referenceValue = null;
Expression referenceExpression = referenceVariable.getExpression();
Expression substitutedReferenceExpression = null;
if (referenceExpression != null) {
try {
substitutedReferenceExpression = simulationSymbolTable.substituteFunctions(referenceExpression);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
}
try {
referenceValue = substitutedReferenceExpression.evaluateConstant();
} catch (ExpressionException e) {
System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
}
}
if (referenceVariable instanceof ParticleObservable) {
referenceElement.setAttribute("type", "Observable");
} else if (referenceVariable instanceof Function) {
if (referenceValue != null) {
referenceElement.setAttribute("type", "ConstantExpression");
} else {
referenceElement.setAttribute("type", "Function");
}
} else {
// constant
referenceElement.setAttribute("type", "ConstantExpression");
}
listOfReferencesElement.addContent(referenceElement);
}
functionElement.addContent(listOfReferencesElement);
Element expressionElement = new Element("Expression");
String functionExpression = valExpression.infix();
expressionElement.setText(functionExpression);
functionElement.addContent(expressionElement);
}
listOfParametersElement.addContent(functionElement);
}
}
return listOfParametersElement;
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class NFsimXMLWriter method getListOfSpecies.
private static Element getListOfSpecies(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
CompartmentSubDomain compartmentSubDomain = (CompartmentSubDomain) mathDesc.getSubDomains().nextElement();
//
// NFsim expects a list of seed species. These are concrete species patterns that have a "ParticleProperties" element defined (for initial conditions).
//
Element listOfSpeciesElement = new Element("ListOfSpecies");
for (int speciesIndex = 0; speciesIndex < compartmentSubDomain.getParticleProperties().size(); speciesIndex++) {
// seedSpecies.getSpeciesPattern().resolveBonds();
ParticleProperties particleProperties = compartmentSubDomain.getParticleProperties().get(speciesIndex);
Element speciesElement = new Element("Species");
String speciesID = "S" + (speciesIndex + 1);
ParticleSpeciesPattern seedSpecies = (ParticleSpeciesPattern) particleProperties.getVariable();
speciesElement.setAttribute("id", speciesID);
speciesElement.setAttribute("name", seedSpecies.getName());
List<ParticleInitialCondition> particleInitialConditions = particleProperties.getParticleInitialConditions();
if (particleInitialConditions.size() != 1) {
throw new SolverException("multiple particle initial conditions not expected for " + ParticleSpeciesPattern.class.getSimpleName() + " " + seedSpecies.getName());
}
// the initial conditions must be a count in math (ParticleInitialConditionCount)
if (!(particleInitialConditions.get(0) instanceof ParticleInitialConditionCount)) {
throw new SolverException("expecting initial count for " + ParticleSpeciesPattern.class.getSimpleName() + " " + seedSpecies.getName());
}
ParticleInitialConditionCount initialCount = (ParticleInitialConditionCount) particleInitialConditions.get(0);
try {
double value = evaluateConstant(initialCount.getCount(), simulationSymbolTable);
Integer maxMoleculesPerType = simulationSymbolTable.getSimulation().getSolverTaskDescription().getNFSimSimulationOptions().getMaxMoleculesPerType();
if (maxMoleculesPerType == null) {
maxMoleculesPerType = NFsimSimulationOptions.DefaultMaxMoleculesPerSpecies;
}
if (maxMoleculesPerType.doubleValue() < value) {
String eMessage = "The Initial count for Species '" + seedSpecies.getName() + "' is " + BigDecimal.valueOf(value).toBigInteger();
eMessage += ", which is higher than the limit of " + maxMoleculesPerType + ".\n";
eMessage += "Please do one of the following: \n- reduce the Initial Condition value for this Species or reduce the compartment size\n";
eMessage += "- increase the maximal number of Molecules per Molecular Type in the Advanced Solver Options panel.";
throw new RuntimeException(eMessage);
}
speciesElement.setAttribute("concentration", Double.toString(value));
} catch (ExpressionException | MathException e) {
e.printStackTrace();
throw new SolverException("error processing initial count of " + ParticleSpeciesPattern.class.getSimpleName() + " " + seedSpecies.getName() + ": " + e.getMessage());
}
HashMap<Bond, BondSites> bondSiteMapping = new HashMap<Bond, BondSites>();
Element listOfMoleculesElement = getListOfMolecules(speciesID, seedSpecies, bondSiteMapping);
speciesElement.addContent(listOfMoleculesElement);
if (bondSiteMapping.size() > 0) {
Element listOfBondsElement = getListOfBonds(bondSiteMapping);
speciesElement.addContent(listOfBondsElement);
}
listOfSpeciesElement.addContent(speciesElement);
}
return listOfSpeciesElement;
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class NFsimXMLWriter method getListOfParameters.
private static Element getListOfParameters(MathDescription mathDesc, SimulationSymbolTable simulationSymbolTable) throws SolverException {
Element listOfParametersElement = new Element("ListOfParameters");
for (Variable var : simulationSymbolTable.getVariables()) {
Double value = null;
if (var instanceof Constant || var instanceof Function) {
Expression valExpression = var.getExpression();
Expression substitutedValExpr = null;
try {
substitutedValExpr = simulationSymbolTable.substituteFunctions(valExpression);
} catch (Exception e) {
e.printStackTrace(System.out);
throw new SolverException("Constant or Function " + var.getName() + " substitution failed : exp = \"" + var.getExpression().infix() + "\": " + e.getMessage());
}
try {
value = substitutedValExpr.evaluateConstant();
} catch (ExpressionException e) {
System.out.println("constant or function " + var.getName() + " = " + substitutedValExpr.infix() + " does not have a constant value");
}
Element parameterElement = new Element("Parameter");
parameterElement.setAttribute("id", var.getName());
if (value != null) {
parameterElement.setAttribute("type", "Constant");
parameterElement.setAttribute("value", value.toString());
} else {
// function, see getListOfFunctions() below
continue;
}
listOfParametersElement.addContent(parameterElement);
}
}
return listOfParametersElement;
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class SmoldynFileWriter method writeReactions.
private void writeReactions() throws ExpressionException, MathException {
printWriter.println("# reactions");
Enumeration<SubDomain> subdomains = mathDesc.getSubDomains();
while (subdomains.hasMoreElements()) {
SubDomain subdomain = subdomains.nextElement();
for (ParticleJumpProcess pjp : subdomain.getParticleJumpProcesses()) {
ArrayList<Variable> reactants = new ArrayList<Variable>();
ArrayList<Variable> products = new ArrayList<Variable>();
for (Action a : pjp.getActions().toArray(new Action[pjp.getActions().size()])) {
if (a.getOperation().equals(Action.ACTION_CREATE)) {
products.add(a.getVar());
} else if (a.getOperation().equals(Action.ACTION_DESTROY)) {
reactants.add(a.getVar());
}
}
Expression rateDefinition = null;
JumpProcessRateDefinition jprd = pjp.getParticleRateDefinition();
if (jprd instanceof MacroscopicRateConstant) {
rateDefinition = subsituteFlatten(((MacroscopicRateConstant) jprd).getExpression());
} else if (jprd instanceof InteractionRadius) {
rateDefinition = subsituteFlatten(((InteractionRadius) jprd).getExpression());
} else {
new RuntimeException("The jump process rate definition is not supported");
}
if (rateDefinition.isZero()) {
continue;
}
if (mathDesc.isSpatialHybrid()) {
String[] symbols = rateDefinition.getSymbols();
if (symbols != null) {
if (subdomain instanceof MembraneSubDomain) {
rateDefinition = new Expression(FiniteVolumeFileWriter.replaceVolumeVariable(getSimulationTask(), (MembraneSubDomain) subdomain, rateDefinition));
}
}
} else {
try {
rateDefinition.evaluateConstant();
} catch (ExpressionException ex) {
throw new ExpressionException("reaction rate for jump process " + pjp.getName() + " is not a constant. Constants are required for all reaction rates.");
}
}
// Smoldyn takes maximum 2nd order reaction.
if (reactants.size() > 2) {
throw new MathException("VCell spatial stochastic models support up to 2nd order reactions. \n" + "The reaction:" + pjp.getName() + " has more than 2 reactants.");
}
if (products.size() > 2) {
throw new MathException("VCell spatial stochastic models support up to 2nd order reactions. \n" + "The reaction:" + pjp.getName() + " has more than 2 products.");
}
String rateDefinitionStr = simulation.getMathDescription().isSpatialHybrid() ? rateDefinition.infix() + ";" : rateDefinition.evaluateConstant() + "";
if (subdomain instanceof CompartmentSubDomain) {
// 0th order reaction, product limited to one and we'll let the reaction know where it happens
if (reactants.size() == 0 && products.size() == 1) {
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_cmpt + " " + subdomain.getName() + " " + pjp.getName() + " ");
} else {
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction + " " + /* + subdomain.getName() + " "*/
pjp.getName() + " ");
}
writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
} else if (subdomain instanceof MembraneSubDomain) {
// 0th order reaction, product limited to one and it can be on mem or in vol
if (reactants.size() == 0 && products.size() == 1) {
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
} else // consuming of a species to nothing, limited to one reactant
if (reactants.size() == 1 && products.size() == 0) {
if (// consuming a mem species in mem reaction
getMembraneVariableCount(reactants) == 1) {
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
} else // it equals to adsorption, species A from vol adsorbed to mem as again species A, and then we kill the speceis A on mem.
if (getVolumeVariableCount(reactants) == 1) {
writeRateTransitionCommand(reactants, products, subdomain, rateDefinitionStr);
String speciesName = reactants.get(0).getName();
String killMolCmd = "cmd " + SmoldynVCellMapper.SmoldynKeyword.E + " " + SmoldynVCellMapper.SmoldynKeyword.killmol + " " + speciesName + "(" + SmoldynKeyword.up + ")";
killMolCommands.add(killMolCmd);
}
} else // Use rate command for any membrane reactions with 1 reactant and 1 product
if ((reactants.size() == 1) && (products.size() == 1)) {
// Membrane reaction (1 react to 1 product).
if (getMembraneVariableCount(products) == 1 && getMembraneVariableCount(reactants) == 1) {
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
} else // Other single molecular reactions
{
writeRateTransitionCommand(reactants, products, subdomain, rateDefinitionStr);
}
} else // membrane reactions which are not one to one, or 0th order, or consuming species
{
if (// membrane reaction has one membrane bound reactant
(getMembraneVariableCount(reactants) == 1)) {
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
} else if (// bimolecular membrane reaction
getMembraneVariableCount(reactants) == 2) {
if (jprd instanceof InteractionRadius) {
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
writeReactionByInteractionRadius(reactants, products, subdomain, rateDefinitionStr, pjp.getName());
} else {
// throw new MathException("Error with reaction: " + pjp.getName() + ".\nVCell Spatial stochastic modeling requires macroscopic or microscopic kinetics for bimolecular membrane reactions.");
printWriter.print(SmoldynVCellMapper.SmoldynKeyword.reaction_surface + " " + subdomain.getName() + " " + pjp.getName() + " ");
writeReactionCommand(reactants, products, subdomain, rateDefinitionStr);
}
} else if (getMembraneVariableCount(reactants) == 0) {
throw new MathException("Error with reaction: " + pjp.getName() + ".\nIn VCell spatial stochastic modeling, the membrane reaction requires at least one membrane bound reactant.");
}
}
}
}
}
printWriter.println();
}
use of cbit.vcell.parser.ExpressionException in project vcell by virtualcell.
the class SmoldynFileWriter method writeDrifts.
private void writeDrifts() throws ExpressionBindingException, ExpressionException, MathException {
// writer diffusion properties
printWriter.println("# drift properties");
Enumeration<SubDomain> subDomainEnumeration = mathDesc.getSubDomains();
while (subDomainEnumeration.hasMoreElements()) {
SubDomain subDomain = subDomainEnumeration.nextElement();
List<ParticleProperties> particlePropertiesList = subDomain.getParticleProperties();
for (ParticleProperties pp : particlePropertiesList) {
String variableName = null;
if (subDomain instanceof MembraneSubDomain) {
variableName = getVariableName(pp.getVariable(), subDomain);
} else {
variableName = getVariableName(pp.getVariable(), null);
}
try {
double driftX = 0.0;
if (pp.getDriftX() != null) {
driftX = subsituteFlattenToConstant(pp.getDriftX());
}
double driftY = 0.0;
if (pp.getDriftY() != null) {
driftY = subsituteFlattenToConstant(pp.getDriftY());
}
double driftZ = 0.0;
if (pp.getDriftZ() != null) {
driftZ = subsituteFlattenToConstant(pp.getDriftZ());
}
printWriter.println(SmoldynVCellMapper.SmoldynKeyword.drift + " " + variableName + " " + driftX + " " + driftY + " " + driftZ);
} catch (NotAConstantException ex) {
throw new ExpressionException("diffusion coefficient for variable " + variableName + " is not a constant. Constants are required for all diffusion coefficients");
}
}
}
printWriter.println();
}
Aggregations