Search in sources :

Example 26 with Variable

use of gov.sandia.n2a.eqset.Variable in project n2a by frothga.

the class Part method init.

/**
 *        Note: specifically for Parts, call resolve() before calling init(). This is to
 *        accommodate the connection process, which must probe values in a part (which
 *        may include references) before calling init().
 */
public void init(Simulator simulator) {
    InstanceTemporaries temp = new InstanceTemporaries(this, simulator, true);
    // update $n and assign $index
    ((Population) container.valuesObject[temp.bed.populationIndex]).insert(this);
    // Note: these do not require resolve(). Instead, they access their target directly through the endpoints array.
    if (temp.bed.count != null) {
        int length = temp.bed.count.length;
        for (int i = 0; i < length; i++) {
            if (temp.bed.count[i] >= 0) {
                Part p = (Part) valuesObject[temp.bed.endpoints + i];
                p.valuesFloat[temp.bed.count[i]]++;
            }
        }
    }
    // force $live to be set before anything else
    if (temp.bed.liveStorage == InternalBackendData.LIVE_STORED)
        set(temp.bed.live, new Scalar(1));
    for (Variable v : temp.bed.localInitSpecial) {
        Type result = v.eval(temp);
        if (result != null && v.writeIndex >= 0)
            temp.set(v, result);
    // Note that some valuesObject entries may be left null. This is OK, because Instance.get() will return
    // a zero-equivalent value if it finds null. Ditto for non-$variables below.
    }
    for (Variable v : temp.bed.localBufferedSpecial) {
        temp.setFinal(v, temp.getFinal(v));
    }
    if (temp.bed.lastT != null)
        temp.setFinal(temp.bed.lastT, new Scalar(simulator.currentEvent.t));
    // non-$variables
    for (Variable v : temp.bed.localInitRegular) {
        Type result = v.eval(temp);
        if (result != null && v.writeIndex >= 0)
            temp.set(v, result);
    }
    for (Variable v : temp.bed.localBufferedRegular) {
        temp.setFinal(v, temp.getFinal(v));
    }
    // v.type should be pre-loaded with zero-equivalent values
    for (Variable v : temp.bed.localBufferedExternalWrite) set(v, v.type);
    // Request event monitors
    for (EventTarget et : temp.bed.eventTargets) {
        for (EventSource es : et.sources) {
            Part source;
            if (es.reference == null)
                source = this;
            else
                source = (Part) valuesObject[es.reference.index];
            @SuppressWarnings("unchecked") ArrayList<Instance> monitors = (ArrayList<Instance>) source.valuesObject[es.monitorIndex];
            monitors.add(this);
        }
    }
    int populations = equations.parts.size();
    for (int i = 0; i < populations; i++) ((Population) valuesObject[i]).init(simulator);
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) Instance(gov.sandia.n2a.language.type.Instance) ArrayList(java.util.ArrayList) Scalar(gov.sandia.n2a.language.type.Scalar) Type(gov.sandia.n2a.language.Type) EventSource(gov.sandia.n2a.backend.internal.InternalBackendData.EventSource) EventTarget(gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)

Example 27 with Variable

use of gov.sandia.n2a.eqset.Variable in project n2a by frothga.

the class Population method finish.

public boolean finish(Simulator simulator) {
    InternalBackendData bed = (InternalBackendData) equations.backendData;
    // This work is split between here and after the finalize code below.
    if (bed.populationCanResize && bed.populationCanGrowOrDie && bed.n.derivative == null) {
        double oldN = ((Scalar) get(bed.n)).value;
        double newN = ((Scalar) getFinal(bed.n)).value;
        if (// $n was explicitly changed, so its value takes precedence
        newN != oldN)
            // $n was explicitly changed, so its value takes precedence
            simulator.resize(this, (int) newN);
        else
            // -1 means to update $n from this.n. This can only be done after other parts are finalized, as they may impose structural dynamics via $p or $type.
            simulator.resize(this, -1);
    }
    for (Variable v : bed.globalBufferedExternal) setFinal(v, getFinal(v));
    for (Variable v : bed.globalBufferedExternalWrite) {
        switch(v.assignment) {
            case Variable.ADD:
                // initial value is zero-equivalent (additive identity)
                set(v, v.type);
                break;
            case Variable.MULTIPLY:
            case Variable.DIVIDE:
                // multiplicative identity
                if (v.type instanceof Matrix)
                    set(v, ((Matrix) v.type).identity());
                else
                    set(v, new Scalar(1));
                break;
            case Variable.MIN:
                if (v.type instanceof Matrix)
                    set(v, ((Matrix) v.type).clear(Double.POSITIVE_INFINITY));
                else
                    set(v, new Scalar(Double.POSITIVE_INFINITY));
                break;
            case Variable.MAX:
                if (v.type instanceof Matrix)
                    set(v, ((Matrix) v.type).clear(Double.NEGATIVE_INFINITY));
                else
                    set(v, new Scalar(Double.NEGATIVE_INFINITY));
                break;
        }
    }
    if (bed.populationCanResize) {
        // This is the finalized value of $n.
        int requestedN = (int) ((Scalar) get(bed.n)).value;
        if (bed.populationCanGrowOrDie) {
            if (// $n' exists
            bed.n.derivative != null) {
                // the rate of change in $n is pre-determined, so it relentlessly overrides any other structural dynamics
                simulator.resize(this, requestedN);
            }
        } else // $n is the only kind of structural dynamics, so only do a resize() when needed
        {
            if (requestedN != n)
                simulator.resize(this, requestedN);
        }
    }
    return true;
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) Matrix(gov.sandia.n2a.language.type.Matrix) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 28 with Variable

use of gov.sandia.n2a.eqset.Variable in project n2a by frothga.

the class Population method integrate.

public void integrate(Simulator simulator, double dt) {
    InternalBackendData bed = (InternalBackendData) equations.backendData;
    for (Variable v : bed.globalIntegrated) {
        double a = ((Scalar) get(v)).value;
        double aa = ((Scalar) get(v.derivative)).value;
        setFinal(v, new Scalar(a + aa * dt));
    }
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 29 with Variable

use of gov.sandia.n2a.eqset.Variable in project n2a by frothga.

the class Population method init.

public void init(Simulator simulator) {
    InstanceTemporaries temp = new InstanceTemporaries(this, simulator, true);
    resolve(temp.bed.globalReference);
    for (Variable v : temp.bed.globalInit) {
        Type result = v.eval(temp);
        if (result != null && v.writeIndex >= 0)
            temp.set(v, result);
    }
    for (Variable v : temp.bed.globalBuffered) {
        temp.setFinal(v, temp.getFinal(v));
    }
    // v.type should be pre-loaded with zero-equivalent values
    for (Variable v : temp.bed.globalBufferedExternalWrite) set(v, v.type);
    if (temp.bed.index != null) {
        // Using floats directly as index counter limits us to 24 bits, or about 16 million. Internal is not intended for large simulations, so this limitation is acceptable.
        valuesFloat[temp.bed.indexNext] = 0;
    // indexAvailable is initially null
    }
    // it still cannot set its own population size.
    if (equations.connectionBindings == null) {
        InternalBackendData bed = (InternalBackendData) equations.backendData;
        int requestedN = 1;
        if (bed.n.hasAttribute("constant"))
            requestedN = (int) ((Scalar) bed.n.eval(this)).value;
        else
            requestedN = (int) ((Scalar) get(bed.n)).value;
        resize(simulator, requestedN);
    } else {
        // queue to evaluate our connections
        simulator.connect(this);
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Variable(gov.sandia.n2a.eqset.Variable)

Example 30 with Variable

use of gov.sandia.n2a.eqset.Variable in project n2a by frothga.

the class ExportJob method analyze.

/**
 *        Find references to $index in connection endpoints, and set up info for ConnectionContext.
 */
public void analyze(EquationSet s) {
    for (EquationSet p : s.parts) analyze(p);
    for (final Variable v : s.variables) {
        Visitor visitor = new Visitor() {

            public boolean visit(Operator op) {
                if (op instanceof AccessVariable) {
                    VariableReference r = ((AccessVariable) op).reference;
                    Variable rv = r.variable;
                    if (rv.container != v.container && !r.resolution.isEmpty()) {
                        Object o = r.resolution.get(r.resolution.size() - 1);
                        if (o instanceof ConnectionBinding) {
                            ConnectionBinding c = (ConnectionBinding) o;
                            // This is somewhat of a hack, but ConnectionContext assumes the mappings A->0 and B->1.
                            if (c.alias.equals("A"))
                                r.index = 0;
                            if (c.alias.equals("B"))
                                r.index = 1;
                        }
                    }
                }
                return true;
            }
        };
        v.visit(visitor);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Visitor(gov.sandia.n2a.language.Visitor) AccessVariable(gov.sandia.n2a.language.AccessVariable) VariableReference(gov.sandia.n2a.eqset.VariableReference) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)

Aggregations

Variable (gov.sandia.n2a.eqset.Variable)35 AccessVariable (gov.sandia.n2a.language.AccessVariable)17 Scalar (gov.sandia.n2a.language.type.Scalar)13 EquationSet (gov.sandia.n2a.eqset.EquationSet)10 Operator (gov.sandia.n2a.language.Operator)9 Type (gov.sandia.n2a.language.Type)8 Visitor (gov.sandia.n2a.language.Visitor)7 ArrayList (java.util.ArrayList)7 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)6 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)6 VariableReference (gov.sandia.n2a.eqset.VariableReference)6 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)5 Instance (gov.sandia.n2a.language.type.Instance)5 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)4 Constant (gov.sandia.n2a.language.Constant)4 Matrix (gov.sandia.n2a.language.type.Matrix)4 MNode (gov.sandia.n2a.db.MNode)3 MPart (gov.sandia.n2a.eqset.MPart)3 Output (gov.sandia.n2a.language.function.Output)3 Text (gov.sandia.n2a.language.type.Text)3