Search in sources :

Example 6 with Instance

use of gov.sandia.n2a.language.type.Instance in project n2a by frothga.

the class EquationSet method determineTypesEval.

public boolean determineTypesEval() {
    boolean changed = false;
    for (final Variable v : variables) {
        if (v.hasAttribute("constant"))
            continue;
        if ((v.name.startsWith("$") || v.name.contains(".$")) && !v.name.startsWith("$up."))
            continue;
        Type value;
        if (// and v is not "constant", but that is covered above
        v.derivative != null) {
            // this should exist, so no need to verify result
            value = find(new Variable(v.name, v.order + 1)).type;
        } else {
            Instance instance = new Instance() {

                // all AccessVariable objects will reach here first, and get back the Variable.type field
                public Type get(VariableReference r) throws EvaluationException {
                    return r.variable.type;
                }
            };
            // can return null if no equation's condition is true
            value = v.eval(instance);
        }
        if (value != null && value.betterThan(v.reference.variable.type)) {
            v.reference.variable.type = value;
            // convenience, so that a reference knows its type, not merely its target
            v.type = value;
            changed = true;
        }
    }
    for (EquationSet s : parts) {
        if (s.determineTypesEval())
            changed = true;
    }
    return changed;
}
Also used : Type(gov.sandia.n2a.language.Type) AccessVariable(gov.sandia.n2a.language.AccessVariable) Instance(gov.sandia.n2a.language.type.Instance)

Example 7 with Instance

use of gov.sandia.n2a.language.type.Instance in project n2a by frothga.

the class EquationSet method isSingleton.

/**
 *        Determines if this equation set has a fixed size of 1.
 */
public boolean isSingleton() {
    Variable n = find(new Variable("$n", 0));
    // We only do more work if $n exists. Non-existent $n is the same as $n==1
    if (n == null)
        return true;
    // make sure no other orders of $n exist
    Variable n2 = variables.higher(n);
    // higher orders means $n is dynamic
    if (n2.name.equals("$n"))
        return false;
    // check contents of $n
    if (n.assignment != Variable.REPLACE)
        return false;
    if (n.equations.size() != 1)
        return false;
    EquationEntry ne = n.equations.first();
    // If we can't evaluate $n, then we treat it as 1
    if (ne.expression == null)
        return true;
    Instance bypass = new Instance() {

        public Type get(VariableReference r) throws EvaluationException {
            // we evaluate $n in init cycle
            if (r.variable.name.equals("$init"))
                return new Scalar(1);
            // During init all other vars are 0, even if they have an initialization conditioned on $init. IE: those values won't be seen until after the init cycle.
            return new Scalar(0);
        }
    };
    Type value = n.eval(bypass);
    if (value instanceof Scalar && ((Scalar) value).value != 1)
        return false;
    // Notice that we could fall through if $n is not a Scalar. That is an error, but since $n can't be evaluated properly, we treat is as 1.
    return true;
}
Also used : Type(gov.sandia.n2a.language.Type) AccessVariable(gov.sandia.n2a.language.AccessVariable) Instance(gov.sandia.n2a.language.type.Instance) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 8 with Instance

use of gov.sandia.n2a.language.type.Instance 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 9 with Instance

use of gov.sandia.n2a.language.type.Instance in project n2a by frothga.

the class EventSpikeMulti method run.

public void run(Simulator simulator) {
    setFlag();
    for (Instance i : targets) simulator.integrate(i);
    for (Instance i : targets) i.update(simulator);
    for (Instance i : targets) {
        boolean live = i.finish(simulator);
        InternalBackendData bed = (InternalBackendData) i.equations.backendData;
        for (Variable v : bed.eventReferences) ((Instance) i.valuesObject[v.reference.index]).finishEvent(v.reference.variable);
        if (!live)
            i.dequeue();
    }
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) Instance(gov.sandia.n2a.language.type.Instance)

Aggregations

Instance (gov.sandia.n2a.language.type.Instance)9 Variable (gov.sandia.n2a.eqset.Variable)5 Type (gov.sandia.n2a.language.Type)5 Scalar (gov.sandia.n2a.language.type.Scalar)5 AccessVariable (gov.sandia.n2a.language.AccessVariable)4 ArrayList (java.util.ArrayList)4 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)3 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)3 Operator (gov.sandia.n2a.language.Operator)2 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)1 Conversion (gov.sandia.n2a.backend.internal.InternalBackendData.Conversion)1 Population (gov.sandia.n2a.backend.internal.Population)1 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)1 XyceRenderer (gov.sandia.n2a.backend.xyce.netlist.XyceRenderer)1 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)1 EquationSet (gov.sandia.n2a.eqset.EquationSet)1 VariableReference (gov.sandia.n2a.eqset.VariableReference)1 OperatorBinary (gov.sandia.n2a.language.OperatorBinary)1 Visitor (gov.sandia.n2a.language.Visitor)1 Output (gov.sandia.n2a.language.function.Output)1