use of cbit.util.graph.Node in project vcell by virtualcell.
the class RegionImage method sortSurfaceCollection.
public static void sortSurfaceCollection(SurfaceCollection surfCollection) {
// Sort nodes
Object[] nodeObjArr = new Object[surfCollection.getNodes().length];
for (int i = 0; i < nodeObjArr.length; i++) {
Object[] temp = new Object[2];
temp[0] = new Integer(i);
temp[1] = surfCollection.getNodes()[i];
nodeObjArr[i] = temp;
}
Arrays.sort(nodeObjArr, new Comparator<Object>() {
public int compare(Object obj1, Object obj2) {
cbit.vcell.geometry.surface.Node o1 = (cbit.vcell.geometry.surface.Node) ((Object[]) obj1)[1];
cbit.vcell.geometry.surface.Node o2 = (cbit.vcell.geometry.surface.Node) ((Object[]) obj2)[1];
double xdiff = o1.getX() - o2.getX();
double xmin = Math.min(Math.abs(o1.getX()), Math.abs(o2.getX()));
double xlimit = (1e-12 * (xmin >= 1.0 ? (Math.pow(10, (int) Math.log10(xmin) + 1)) : 1));
double ydiff = o1.getY() - o2.getY();
double ymin = Math.min(Math.abs(o1.getY()), Math.abs(o2.getY()));
double ylimit = (1e-12 * (ymin >= 1.0 ? (Math.pow(10, (int) Math.log10(ymin) + 1)) : 1));
double zdiff = o1.getZ() - o2.getZ();
double zmin = Math.min(Math.abs(o1.getZ()), Math.abs(o2.getZ()));
double zlimit = (1e-12 * (zmin >= 1.0 ? (Math.pow(10, (int) Math.log10(zmin) + 1)) : 1));
if (Math.abs(zdiff) < zlimit) {
if (Math.abs(ydiff) < ylimit) {
return (int) Math.signum((Math.abs(xdiff) < xlimit ? 0 : xdiff));
}
return (int) Math.signum((Math.abs(ydiff) < ylimit ? 0 : ydiff));
}
return (int) Math.signum((Math.abs(zdiff) < zlimit ? 0 : zdiff));
}
});
int[] remap = new int[nodeObjArr.length];
Arrays.fill(remap, -1);
cbit.vcell.geometry.surface.Node[] sortedNodes = new cbit.vcell.geometry.surface.Node[nodeObjArr.length];
for (int i = 0; i < nodeObjArr.length; i++) {
sortedNodes[i] = (cbit.vcell.geometry.surface.Node) ((Object[]) nodeObjArr[i])[1];
if (remap[sortedNodes[i].getGlobalIndex()] == -1) {
remap[sortedNodes[i].getGlobalIndex()] = i;
} else {
throw new RuntimeException("SORT error: duplicate nodes");
}
}
// surfCollection.setNodes(sortedNodes);
System.arraycopy(sortedNodes, 0, surfCollection.getNodes(), 0, sortedNodes.length);
HashSet<cbit.vcell.geometry.surface.Node> remapHashSet = new HashSet<cbit.vcell.geometry.surface.Node>();
for (int i = 0; i < surfCollection.getSurfaceCount(); i++) {
Surface surf = surfCollection.getSurfaces(i);
Polygon[] sortedPolygonArr = new Polygon[surf.getPolygonCount()];
for (int j = 0; j < surf.getPolygonCount(); j++) {
Polygon poly = surf.getPolygons(j);
for (int k = 0; k < poly.getNodes().length; k++) {
cbit.vcell.geometry.surface.Node node = poly.getNodes(k);
if (!remapHashSet.contains(node)) {
node.setGlobalIndex(remap[node.getGlobalIndex()]);
remapHashSet.add(node);
}
}
sortedPolygonArr[j] = poly;
}
Arrays.sort(sortedPolygonArr, new Comparator<Polygon>() {
public int compare(Polygon obj1, Polygon obj2) {
Coordinate o1 = ((Quadrilateral) obj1).calculateCentroid();
Coordinate o2 = ((Quadrilateral) obj2).calculateCentroid();
double xdiff = o1.getX() - o2.getX();
double xmin = Math.min(Math.abs(o1.getX()), Math.abs(o2.getX()));
double xlimit = (1e-12 * (xmin >= 1.0 ? (Math.pow(10, (int) Math.log10(xmin) + 1)) : 1));
double ydiff = o1.getY() - o2.getY();
double ymin = Math.min(Math.abs(o1.getY()), Math.abs(o2.getY()));
double ylimit = (1e-12 * (ymin >= 1.0 ? (Math.pow(10, (int) Math.log10(ymin) + 1)) : 1));
double zdiff = o1.getZ() - o2.getZ();
double zmin = Math.min(Math.abs(o1.getZ()), Math.abs(o2.getZ()));
double zlimit = (1e-12 * (zmin >= 1.0 ? (Math.pow(10, (int) Math.log10(zmin) + 1)) : 1));
if (Math.abs(zdiff) < zlimit) {
if (Math.abs(ydiff) < ylimit) {
return (int) Math.signum((Math.abs(xdiff) < xlimit ? 0 : xdiff));
}
return (int) Math.signum((Math.abs(ydiff) < ylimit ? 0 : ydiff));
}
return (int) Math.signum((Math.abs(zdiff) < zlimit ? 0 : zdiff));
}
});
OrigSurface sortedSurface = new OrigSurface(surf.getInteriorRegionIndex(), surf.getExteriorRegionIndex());
for (int k = 0; k < sortedPolygonArr.length; k++) {
int minGlobalIndex = sortedPolygonArr[k].getNodes(0).getGlobalIndex();
// System.out.print("Surf "+i+" poly "+k+" nodeGI - ");
for (int j = 0; j < sortedPolygonArr[k].getNodeCount(); j++) {
if (sortedPolygonArr[k].getNodes(j).getGlobalIndex() < minGlobalIndex) {
minGlobalIndex = sortedPolygonArr[k].getNodes(j).getGlobalIndex();
}
// System.out.print(sortedPolygonArr[k].getNodes(j).getGlobalIndex()+" ");
}
while (sortedPolygonArr[k].getNodes(0).getGlobalIndex() != minGlobalIndex) {
cbit.vcell.geometry.surface.Node lastNode = sortedPolygonArr[k].getNodes(sortedPolygonArr[k].getNodeCount() - 1);
System.arraycopy(sortedPolygonArr[k].getNodes(), 0, sortedPolygonArr[k].getNodes(), 1, sortedPolygonArr[k].getNodeCount() - 1);
sortedPolygonArr[k].getNodes()[0] = lastNode;
}
// System.out.println();
sortedSurface.addPolygon(sortedPolygonArr[k]);
}
surfCollection.setSurfaces(i, sortedSurface);
}
}
use of cbit.util.graph.Node in project vcell by virtualcell.
the class ModelOptimizationSpec method calculateTimeDependentModelObjects.
/**
* Insert the method's description here.
* Creation date: (11/29/2005 5:10:51 PM)
* @return cbit.vcell.parser.SymbolTableEntry[]
*/
public static SymbolTableEntry[] calculateTimeDependentModelObjects(SimulationContext simulationContext) {
Graph digraph = new Graph();
//
// add time
//
Model model = simulationContext.getModel();
Node timeNode = new Node("t", model.getTIME());
digraph.addNode(timeNode);
//
// add all species concentrations (that are not fixed with a constant initial condition).
//
SpeciesContextSpec[] scs = simulationContext.getReactionContext().getSpeciesContextSpecs();
for (int i = 0; scs != null && i < scs.length; i++) {
SpeciesContextSpecParameter initParam = scs[i].getInitialConditionParameter();
Expression iniExp = initParam == null ? null : initParam.getExpression();
if (!scs[i].isConstant() || (iniExp != null && !iniExp.isNumeric())) {
String speciesContextScopedName = scs[i].getSpeciesContext().getNameScope().getAbsoluteScopePrefix() + scs[i].getSpeciesContext().getName();
Node speciesContextNode = new Node(speciesContextScopedName, scs[i].getSpeciesContext());
digraph.addNode(speciesContextNode);
digraph.addEdge(new Edge(speciesContextNode, timeNode));
}
}
//
// add all model (global) parameters that are not simple constants
//
ModelParameter[] modelParams = model.getModelParameters();
for (int i = 0; modelParams != null && i < modelParams.length; i++) {
Expression exp = modelParams[i].getExpression();
if (exp != null) {
String[] symbols = exp.getSymbols();
if (symbols != null && symbols.length > 0) {
//
// add parameter to graph as a node (if not already there).
//
String parameterScopedName = modelParams[i].getNameScope().getAbsoluteScopePrefix() + modelParams[i].getName();
Node parameterNode = digraph.getNode(parameterScopedName);
if (parameterNode == null) {
parameterNode = new Node(parameterScopedName, modelParams[i]);
digraph.addNode(parameterNode);
}
//
for (int k = 0; symbols != null && k < symbols.length; k++) {
SymbolTableEntry ste = exp.getSymbolBinding(symbols[k]);
if (ste == null) {
throw new RuntimeException("Error, symbol '" + symbols[k] + "' not bound in parameter '" + modelParams[i].getName() + "'");
}
String symbolScopedName = ste.getNameScope().getAbsoluteScopePrefix() + ste.getName();
Node symbolNode = digraph.getNode(symbolScopedName);
if (symbolNode == null) {
symbolNode = new Node(symbolScopedName, ste);
digraph.addNode(symbolNode);
}
digraph.addEdge(new Edge(parameterNode, symbolNode));
}
}
}
}
//
// add all reaction parameters that are not simple constants
//
ReactionStep[] reactionSteps = model.getReactionSteps();
for (int i = 0; reactionSteps != null && i < reactionSteps.length; i++) {
Parameter[] parameters = reactionSteps[i].getKinetics().getKineticsParameters();
for (int j = 0; parameters != null && j < parameters.length; j++) {
Expression exp = parameters[j].getExpression();
if (exp != null) {
String[] symbols = exp.getSymbols();
if (symbols != null && symbols.length > 0) {
//
// add parameter to graph as a node (if not already there).
//
String parameterScopedName = parameters[j].getNameScope().getAbsoluteScopePrefix() + parameters[j].getName();
Node parameterNode = digraph.getNode(parameterScopedName);
if (parameterNode == null) {
parameterNode = new Node(parameterScopedName, parameters[j]);
digraph.addNode(parameterNode);
}
//
for (int k = 0; symbols != null && k < symbols.length; k++) {
SymbolTableEntry ste = exp.getSymbolBinding(symbols[k]);
if (ste == null) {
throw new RuntimeException("Error, symbol '" + symbols[k] + "' not bound in parameter '" + parameters[j].getName() + "'");
}
String symbolScopedName = ste.getNameScope().getAbsoluteScopePrefix() + ste.getName();
Node symbolNode = digraph.getNode(symbolScopedName);
if (symbolNode == null) {
symbolNode = new Node(symbolScopedName, ste);
digraph.addNode(symbolNode);
}
digraph.addEdge(new Edge(parameterNode, symbolNode));
}
}
}
}
}
//
for (Structure structure : model.getStructures()) {
if (structure instanceof Membrane && ((MembraneMapping) simulationContext.getGeometryContext().getStructureMapping(structure)).getCalculateVoltage()) {
MembraneVoltage membraneVoltage = ((Membrane) structure).getMembraneVoltage();
String membraneVoltageScopedName = membraneVoltage.getNameScope().getAbsoluteScopePrefix() + membraneVoltage.getName();
Node membraneVoltageNode = digraph.getNode(membraneVoltageScopedName);
if (membraneVoltageNode == null) {
membraneVoltageNode = new Node(membraneVoltageScopedName, membraneVoltage);
digraph.addNode(membraneVoltageNode);
}
digraph.addEdge(new Edge(membraneVoltageNode, timeNode));
}
}
Node[] timeDependentNodes = digraph.getDigraphAttractorSet(timeNode);
SymbolTableEntry[] steArray = new SymbolTableEntry[timeDependentNodes.length];
for (int i = 0; i < steArray.length; i++) {
steArray[i] = (SymbolTableEntry) timeDependentNodes[i].getData();
}
return steArray;
}
use of cbit.util.graph.Node in project vcell by virtualcell.
the class StructureSorter method sortStructures.
public static Structure[] sortStructures(Model model) {
Graph graph = new Graph();
// add all structures to the graph
for (Structure structure : model.getStructures()) {
graph.addNode(new Node(structure.getName(), structure));
}
// add an edge
for (Structure structure : model.getStructures()) {
Structure parentStructure = null;
if (structure.getSbmlParentStructure() != null) {
parentStructure = structure.getSbmlParentStructure();
} else if (model.getStructureTopology().getParentStructure(structure) != null) {
parentStructure = model.getStructureTopology().getParentStructure(structure);
}
if (parentStructure != null) {
int index1 = graph.getIndex(graph.getNode(structure.getName()));
int index2 = graph.getIndex(graph.getNode(parentStructure.getName()));
StructureRelationshipEdge edge = getStructureRelationshipEdge(graph, index1, index2);
edge.bParentChild = true;
}
}
for (ReactionStep rs : model.getReactionSteps()) {
int index1 = graph.getIndex(graph.getNode(rs.getStructure().getName()));
for (ReactionParticipant rp : rs.getReactionParticipants()) {
if (rp.getStructure() != rs.getStructure()) {
int index2 = graph.getIndex(graph.getNode(rp.getStructure().getName()));
StructureRelationshipEdge edge = getStructureRelationshipEdge(graph, index2, index1);
edge.numConnections++;
}
}
}
// remove edges such that all nodes have degree <= 2
for (Node node : graph.getNodes()) {
Edge[] adjacentEdges = graph.getAdjacentEdges(node);
while (adjacentEdges.length > 2) {
StructureRelationshipEdge lowestCostEdge = getLowestCostEdge(Arrays.asList(adjacentEdges));
graph.remove(lowestCostEdge);
adjacentEdges = graph.getAdjacentEdges(node);
}
}
// for each dependency loop, remove edge with lowest cost
Path[] fundamentalCycles = graph.getFundamentalCycles();
while (fundamentalCycles.length > 0) {
StructureRelationshipEdge lowestCostEdge = getLowestCostEdge(Arrays.asList(fundamentalCycles[0].getEdges()));
graph.remove(lowestCostEdge);
fundamentalCycles = graph.getFundamentalCycles();
}
// all graphs in the forest are linear now ... find path from first to last and line up all trees in list.
Tree[] spanningForest = graph.getSpanningForest();
ArrayList<Structure> structures = new ArrayList<Structure>();
for (Tree tree : spanningForest) {
if (tree.getNodes().length > 1) {
Node start = null;
Node end = null;
for (Node node : tree.getNodes()) {
if (tree.getDegree(node) == 1) {
if (start == null) {
start = node;
} else if (end == null) {
end = node;
} else {
break;
}
}
}
Path path = tree.getTreePath(start, end);
for (Node node : path.getNodesTraversed()) {
structures.add((Structure) node.getData());
}
} else {
structures.add((Structure) tree.getNodes()[0].getData());
}
}
return structures.toArray(new Structure[structures.size()]);
}
use of cbit.util.graph.Node in project vcell by virtualcell.
the class VariableHash method reorderVariables.
/**
* Insert the method's description here.
* Creation date: (4/12/2002 3:45:17 PM)
* @return cbit.vcell.math.Variable[]
* @param variables cbit.vcell.math.Variable[]
*/
private Variable[] reorderVariables(Variable[] variables) throws VariableHash.UnresolvedException {
//
// add the nodes for each "Variable" (VolVariable, etc...) ... no dependencies
//
Graph variableGraph = new Graph();
Graph constantGraph = new Graph();
Graph functionGraph = new Graph();
for (int i = 0; i < variables.length; i++) {
Node node = new Node(variables[i].getName(), variables[i]);
if (variables[i] instanceof Constant) {
constantGraph.addNode(node);
} else if (variables[i] instanceof Function) {
functionGraph.addNode(node);
} else {
variableGraph.addNode(node);
}
}
//
// add an edge for each dependency of a Constant
//
Node[] constantNodes = constantGraph.getNodes();
for (int i = 0; i < constantNodes.length; i++) {
Constant constant = (Constant) constantNodes[i].getData();
String[] symbols = constant.getExpression().getSymbols();
if (symbols != null) {
for (int j = 0; j < symbols.length; j++) {
//
// find node that this symbol references (it better find this in the Constants).
//
Node symbolNode = constantGraph.getNode(symbols[j]);
if (symbolNode == null) {
if (functionGraph.getNode(symbols[j]) != null) {
throw new RuntimeException("Constant " + constant.getName() + " references function '" + symbols[j] + "'");
} else if (variableGraph.getNode(symbols[j]) != null) {
throw new RuntimeException("Constant " + constant.getName() + " references variable '" + symbols[j] + "'");
} else {
throw new RuntimeException("Constant " + constant.getName() + " references unknown symbol '" + symbols[j] + "'");
}
}
Edge dependency = new Edge(constantNodes[i], symbolNode, constant.getName() + "->" + ((Variable) symbolNode.getData()).getName());
constantGraph.addEdge(dependency);
}
}
}
Node[] sortedConstantNodes = constantGraph.topologicalSort();
//
// add an edge for each dependency of a Function
//
Node[] functionNodes = functionGraph.getNodes();
for (int i = 0; i < functionNodes.length; i++) {
Function function = (Function) functionNodes[i].getData();
String[] symbols = function.getExpression().getSymbols();
if (symbols != null) {
for (int j = 0; j < symbols.length; j++) {
//
// find node that this symbol references (better find it in FunctionGraph, ConstantGraph, or VariableGraph).
//
Node symbolNode = functionGraph.getNode(symbols[j]);
if (symbolNode != null) {
Edge dependency = new Edge(functionNodes[i], symbolNode, function.getName() + "->" + ((Variable) symbolNode.getData()).getName());
functionGraph.addEdge(dependency);
} else if (ReservedMathSymbolEntries.getReservedVariableEntry(symbols[j]) == null && constantGraph.getNode(symbols[j]) == null && variableGraph.getNode(symbols[j]) == null) {
throw new VariableHash.UnresolvedException(symbols[j], "Unable to sort, unknown identifier " + symbols[j]);
}
}
}
}
Node[] sortedFunctionNodes = functionGraph.topologicalSort();
//
// reassemble in the proper order
// Constants
// Variables
// Functions
//
Vector newVariableOrder = new Vector();
for (int i = 0; i < sortedConstantNodes.length; i++) {
newVariableOrder.add(sortedConstantNodes[i].getData());
}
Node[] variableNodes = variableGraph.getNodes();
for (int i = 0; i < variableNodes.length; i++) {
Object var = variableNodes[i].getData();
if (!(var instanceof InsideVariable) && !(var instanceof OutsideVariable)) {
newVariableOrder.add(var);
}
}
for (int i = 0; i < sortedFunctionNodes.length; i++) {
newVariableOrder.add(sortedFunctionNodes[i].getData());
}
Variable[] sortedVars = new Variable[newVariableOrder.size()];
newVariableOrder.copyInto(sortedVars);
return sortedVars;
}
use of cbit.util.graph.Node in project vcell by virtualcell.
the class MathSystemHash method getDependencyGraph.
/**
* Insert the method's description here.
* Creation date: (4/12/2002 3:45:17 PM)
* @return cbit.vcell.math.Variable[]
* @param variables cbit.vcell.math.Variable[]
*/
public Graph getDependencyGraph(Symbol[] symbols) throws MathSystemHash.UnresolvedException {
//
// add the nodes for each "Symbol"
//
Graph graph = new Graph();
for (int i = 0; i < symbols.length; i++) {
Node node = new Node(symbols[i].getName(), symbols[i]);
graph.addNode(node);
}
//
// add an edge for each dependency of a Symbol (due to it's associated expression implied equation ... e.g. Rate, IC).
//
Node[] nodes = graph.getNodes();
for (int i = 0; i < nodes.length; i++) {
Symbol symbol = (Symbol) nodes[i].getData();
if (symbol.getExpression() != null) {
String[] symbolNames = symbol.getExpression().getSymbols();
for (int j = 0; symbolNames != null && j < symbolNames.length; j++) {
//
// find node that this symbol references
//
Node symbolNode = graph.getNode(symbolNames[j]);
if (symbolNode == null) {
throw new RuntimeException("symbol " + symbol.getName() + " references unknown symbol '" + symbolNames[j] + "'");
}
Edge dependency = new Edge(nodes[i], symbolNode, symbol.getName() + "->" + ((Symbol) symbolNode.getData()).getName());
graph.addEdge(dependency);
}
}
if (symbol instanceof VariableReference) {
VariableReference varRef = (VariableReference) symbol;
Node referencingVarNode = graph.getNode(varRef.variable.getName());
if (referencingVarNode == null) {
throw new RuntimeException("symbol " + symbol.getName() + " references unknown variable '" + varRef.variable.getName() + "'");
}
Edge dependency = new Edge(referencingVarNode, nodes[i], varRef.variable.getName() + "->" + symbol.getName());
graph.addEdge(dependency);
}
}
return graph;
}
Aggregations