Search in sources :

Example 21 with ConnectionBinding

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.
    }
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) EvaluationException(gov.sandia.n2a.language.EvaluationException)

Example 22 with ConnectionBinding

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);
}
Also used : ConnectionMatrix(gov.sandia.n2a.eqset.EquationSet.ConnectionMatrix) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) ArrayList(java.util.ArrayList)

Aggregations

ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)22 EquationSet (gov.sandia.n2a.eqset.EquationSet)16 Variable (gov.sandia.n2a.eqset.Variable)11 AccessVariable (gov.sandia.n2a.language.AccessVariable)11 ArrayList (java.util.ArrayList)9 VariableReference (gov.sandia.n2a.eqset.VariableReference)6 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)5 Operator (gov.sandia.n2a.language.Operator)5 Scalar (gov.sandia.n2a.language.type.Scalar)5 ExtensionPoint (gov.sandia.n2a.plugins.ExtensionPoint)5 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)4 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)3 ConnectionMatrix (gov.sandia.n2a.eqset.EquationSet.ConnectionMatrix)3 Conversion (gov.sandia.n2a.eqset.EquationSet.Conversion)3 Constant (gov.sandia.n2a.language.Constant)3 AbortRun (gov.sandia.n2a.plugins.extpoints.Backend.AbortRun)3 TreeSet (java.util.TreeSet)3 Output (gov.sandia.n2a.language.function.Output)2 List (java.util.List)2 MNode (gov.sandia.n2a.db.MNode)1