use of gov.sandia.n2a.eqset.EquationSet.ConnectionBinding in project n2a by frothga.
the class InternalBackendData method analyzeConversions.
public void analyzeConversions(EquationSet s) {
// Type conversions
// forbid $index and all phase variables. TODO: create attributes just for this?
List<String> forbiddenVariables = Arrays.asList("$type", "$index", "$live", "$init", "$connect");
for (EquationSet target : s.getConversionTargets()) {
if (s.connectionBindings == null) {
if (target.connectionBindings != null)
throw new EvaluationException("Can't change $type from compartment to connection.");
} else {
if (target.connectionBindings == null)
throw new EvaluationException("Can't change $type from connection to compartment.");
}
Conversion conversion = new Conversion();
conversions.put(target, conversion);
// Match variables
InternalBackendData targetBed = (InternalBackendData) target.backendData;
for (Variable v : targetBed.localMembers) {
if (forbiddenVariables.contains(v.name))
continue;
// find() does not consider container
Variable v2 = s.find(v);
if (v2 != null) {
conversion.to.add(v);
conversion.from.add(v2);
}
}
// Match connection bindings
if (// Since we checked above, we know that target is also a connection.
s.connectionBindings != null) {
conversion.bindings = new int[s.connectionBindings.size()];
int i = 0;
for (ConnectionBinding c : s.connectionBindings) {
conversion.bindings[i] = -1;
int j = 0;
for (ConnectionBinding d : target.connectionBindings) {
if (c.alias.equals(d.alias)) {
conversion.bindings[i] = j;
break;
}
j++;
}
// Note: ALL bindings must match. There is no other mechanism for initializing the endpoints.
if (conversion.bindings[i] < 0)
throw new EvaluationException("Unfulfilled connection binding during $type change.");
i++;
}
}
// TODO: Match populations?
// Currently, any contained populations do not carry over to new instance. Instead, it must create them from scratch.
}
}
use of gov.sandia.n2a.eqset.EquationSet.ConnectionBinding in project n2a by frothga.
the class Population method getIterators.
public ConnectIterator getIterators(Simulator simulator, boolean poll) {
InternalBackendData bed = (InternalBackendData) equations.backendData;
int count = equations.connectionBindings.size();
ArrayList<ConnectPopulation> iterators = new ArrayList<ConnectPopulation>(count);
boolean nothingNew = true;
boolean spatialFiltering = false;
for (int i = 0; i < count; i++) {
ConnectionBinding target = equations.connectionBindings.get(i);
ConnectPopulation it = new ConnectPopulation(i, target, bed, simulator, poll);
// Nothing to connect. This should never happen.
if (it.instances == null || it.instances.size() == 0)
return null;
iterators.add(it);
if (it.firstborn < it.size)
nothingNew = false;
if (it.k > 0 || it.radius > 0)
spatialFiltering = true;
}
if (nothingNew && !poll)
return null;
ConnectionMatrix cm = equations.connectionMatrix;
if (// Use sparse-matrix optimization
cm != null) {
// cm takes precedence over any other iteration method.
return new ConnectMatrix(cm, iterators.get(cm.rows.index), iterators.get(cm.cols.index), simulator);
}
// That allows the most number of old entries to be skipped.
if (!poll) {
// This is a simple insertion sort ...
for (int i = 1; i < count; i++) {
for (int j = i; j > 0; j--) {
ConnectPopulation A = iterators.get(j - 1);
ConnectPopulation B = iterators.get(j);
if (A.firstborn >= B.firstborn)
break;
iterators.set(j - 1, B);
iterators.set(j, A);
}
}
}
// For spatial filtering, make the innermost iterator be the one that best defines C.$xyz
if (spatialFiltering) {
double[] xyz = new double[3];
for (int i = 0; i < count; i++) iterators.get(i).xyz = xyz;
if (// connection's own $xyz is not defined, so must get it from some $project
bed.xyz == null || bed.xyz.equations.size() == 0) {
int last = count - 1;
ConnectPopulation A = iterators.get(last);
int bestIndex = last;
double bestRank = A.rank;
for (int i = 0; i < last; i++) {
A = iterators.get(i);
if (A.rank > bestRank) {
bestIndex = i;
bestRank = A.rank;
}
}
if (bestIndex != last) {
A = iterators.remove(bestIndex);
iterators.add(A);
}
}
}
for (int i = 1; i < count; i++) {
ConnectPopulation A = iterators.get(i - 1);
ConnectPopulation B = iterators.get(i);
A.permute = B;
B.contained = true;
if (A.k > 0 || A.radius > 0)
A.prepareNN();
}
return iterators.get(0);
}
Aggregations