Search in sources :

Example 16 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() separately 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 InstanceInit(this, simulator);
    InternalBackendData bed = temp.bed;
    // update $n and assign $index
    ((Population) container.valuesObject[bed.populationIndex]).insert(this);
    // Note: these do not require resolve(). Instead, they access their target directly through the endpoints array.
    if (bed.count != null) {
        int length = bed.count.length;
        for (int i = 0; i < length; i++) {
            if (bed.count[i] >= 0) {
                Part p = (Part) valuesObject[bed.endpoints + i];
                p.valuesFloat[bed.count[i]]++;
            }
        }
    }
    // Initialize variables
    // Note that some valuesObject entries could be left null. This is OK, because Instance.get() will return
    // a zero-equivalent value if it finds null.
    // So our intial values can be applied correctly.
    clearExternalWriteInit(bed.localBufferedExternalWrite);
    for (Variable v : bed.localInit) {
        Type result = v.eval(temp);
        if (result == null || v.reference.variable.writeIndex < 0)
            continue;
        // with its finish() step. It would be as if the part had an extra cycle inserted.
        if (v.reference.variable == v)
            temp.applyResultInit(v, result);
        else
            ((Instance) valuesObject[v.reference.index]).applyResultInit(v.reference.variable, result);
    }
    if (bed.liveStorage == InternalBackendData.LIVE_STORED)
        set(bed.live, new Scalar(1));
    if (bed.lastT != null)
        temp.setFinal(bed.lastT, new Scalar(simulator.currentEvent.t));
    if (bed.type != null)
        temp.setFinal(bed.type, new Scalar(0));
    if (bed.setDt)
        simulator.move(this, ((Scalar) bed.dt.type).value);
    // Prepare variables that have a combiner, in case they get written before the first finish().
    // skips REPLACE it because it is unnecessary when called from update(). Handle REPLACE separately ...
    clearExternalWriteBuffers(bed.localBufferedExternalWrite);
    // This must come after the variables are initialized. Otherwise, there is no point.
    for (Variable v : bed.localBufferedExternalWrite) if (v.assignment == Variable.REPLACE)
        temp.set(v, temp.get(v));
    // Request event monitors
    for (EventTarget et : 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);
        }
    }
    if (equations.parts.size() > 0) {
        // If there are parts at all, then orderedParts must be filled in correctly. Otherwise it may be null.
        for (EquationSet s : equations.orderedParts) ((Population) valuesObject[s.priority]).init(simulator);
    }
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet) 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 17 with Instance

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

the class SumSquares method determineExponent.

public void determineExponent(ExponentContext context) {
    // This function is related to both Norm and Power. See those classes for similar processing.
    // A
    Operator op0 = operands[0];
    op0.determineExponent(context);
    // Determine number of elements in A
    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;
        }
    };
    Matrix A = (Matrix) op0.eval(instance);
    int Asize = A.rows() * A.columns();
    if (op0.exponent != UNKNOWN) {
        // log2(Asize)
        int shift = (int) Math.floor(Math.log(Asize) / Math.log(2));
        int centerNew = MSB / 2;
        int exponentNew = op0.centerPower() * 2 + shift + MSB - centerNew;
        updateExponent(context, exponentNew, centerNew);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) Matrix(gov.sandia.n2a.language.type.Matrix) VariableReference(gov.sandia.n2a.eqset.VariableReference) Instance(gov.sandia.n2a.language.type.Instance)

Aggregations

Instance (gov.sandia.n2a.language.type.Instance)17 Scalar (gov.sandia.n2a.language.type.Scalar)10 AccessVariable (gov.sandia.n2a.language.AccessVariable)8 Type (gov.sandia.n2a.language.Type)8 Variable (gov.sandia.n2a.eqset.Variable)7 Operator (gov.sandia.n2a.language.Operator)7 ArrayList (java.util.ArrayList)5 Visitor (gov.sandia.n2a.language.Visitor)4 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)3 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)3 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)3 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 Constant (gov.sandia.n2a.language.Constant)3 Population (gov.sandia.n2a.backend.internal.Population)2 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)2 XyceRenderer (gov.sandia.n2a.backend.xyce.netlist.XyceRenderer)2 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)2 EquationSet (gov.sandia.n2a.eqset.EquationSet)2 EvaluationException (gov.sandia.n2a.language.EvaluationException)2 Output (gov.sandia.n2a.language.function.Output)2