Search in sources :

Example 21 with Type

use of gov.sandia.n2a.language.Type in project n2a by frothga.

the class Part method update.

public void update(Simulator simulator) {
    InstanceTemporaries temp = new InstanceTemporaries(this, simulator, false);
    for (Variable v : temp.bed.localUpdate) {
        Type result = v.eval(temp);
        // this is a "dummy" variable, so calling eval() was all we needed to do
        if (v.reference.variable.writeIndex < 0)
            continue;
        if (// no condition matched
        result == null) {
            if (v.reference.variable != v || v.equations.size() == 0)
                continue;
            if (// not buffered
            v.readIndex == v.writeIndex) {
                // This is a pure temporary for which no equation fired, so set value to default for use by later equations. Note that readTemp==writeTemp==true.
                if (v.readTemp)
                    temp.set(v, v.type);
                continue;
            }
            // Variable is buffered
            if (// not an accumulator
            v.assignment == Variable.REPLACE) {
                // so copy its value
                temp.set(v, temp.get(v));
            }
            continue;
        }
        if (v.assignment == Variable.REPLACE) {
            temp.set(v, result);
        } else {
            // the rest of these require knowing the current value of the working result, which is most likely external buffered
            Type current = temp.getFinal(v.reference);
            switch(v.assignment) {
                case Variable.ADD:
                    temp.set(v, current.add(result));
                    break;
                case Variable.MULTIPLY:
                    temp.set(v, current.multiply(result));
                    break;
                case Variable.DIVIDE:
                    temp.set(v, current.divide(result));
                    break;
                case Variable.MIN:
                    temp.set(v, current.min(result));
                    break;
                case Variable.MAX:
                    temp.set(v, current.max(result));
                    break;
            }
        }
    }
    for (Variable v : temp.bed.localBufferedInternalUpdate) {
        temp.setFinal(v, temp.getFinal(v));
    }
    int populations = equations.parts.size();
    for (int i = 0; i < populations; i++) ((Population) valuesObject[i]).update(simulator);
}
Also used : Type(gov.sandia.n2a.language.Type) Variable(gov.sandia.n2a.eqset.Variable)

Example 22 with Type

use of gov.sandia.n2a.language.Type 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 23 with Type

use of gov.sandia.n2a.language.Type 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 24 with Type

use of gov.sandia.n2a.language.Type in project n2a by frothga.

the class ExportJob method genericPart.

public void genericPart(MPart part, Element result, String... skip) {
    EquationSet partEquations = getEquations(part);
    List<Element> resultElements = new ArrayList<Element>();
    List<String> skipList = Arrays.asList(skip);
    for (MNode c : part) {
        MPart p = (MPart) c;
        String key = p.key();
        // Skip connection bindings. They are unpacked elsewhere.
        if (partEquations.findConnection(key) != null)
            continue;
        if (key.startsWith("$"))
            continue;
        if (skipList.contains(key))
            continue;
        if (p.isPart()) {
            genericPart(p, resultElements);
        } else {
            // We need to check two things:
            // * Has the variable been overridden after it was declared in the base part?
            // * Is it an expression or a constant?
            // An override that is an expression should trigger a LEMS extension part.
            // A constant that is either overridden or required should be emitted here.
            boolean expression = true;
            String value = p.get();
            Variable v = partEquations.find(new Variable(key));
            try {
                // This could convert an expression to a constant.
                Type evalue = v.eval(context);
                // TODO: if it is a simple AccessVariable, then it shouldn't be viewed as an expression.
                Operator op = Operator.parse(value);
                if (op instanceof Constant) {
                    // "direct" value
                    Type dvalue = ((Constant) op).value;
                    expression = !evalue.equals(dvalue);
                }
            } catch (Exception e) {
            }
            boolean overridden = p.isFromTopDocument() || isOverride(part.get("$inherit").replace("\"", ""), key);
            String name = sequencer.bestFieldName(result, p.get("$metadata", "backend.lems.param"));
            if (name.isEmpty())
                name = key;
            boolean required = sequencer.isRequired(result, name);
            if (required || (overridden && !expression)) {
                // biophysicalUnits() should return strings (non-numbers) unmodified
                result.setAttribute(name, biophysicalUnits(p.get()));
            }
        }
    }
    sequencer.append(result, resultElements);
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) MPart(gov.sandia.n2a.eqset.MPart) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Constant(gov.sandia.n2a.language.Constant) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MNode(gov.sandia.n2a.db.MNode) IncommensurableException(javax.measure.IncommensurableException) UnconvertibleException(javax.measure.UnconvertibleException) ParseException(gov.sandia.n2a.language.ParseException) Type(gov.sandia.n2a.language.Type)

Aggregations

Type (gov.sandia.n2a.language.Type)24 Scalar (gov.sandia.n2a.language.type.Scalar)17 Operator (gov.sandia.n2a.language.Operator)10 Constant (gov.sandia.n2a.language.Constant)9 Variable (gov.sandia.n2a.eqset.Variable)8 AccessVariable (gov.sandia.n2a.language.AccessVariable)7 Matrix (gov.sandia.n2a.language.type.Matrix)6 Text (gov.sandia.n2a.language.type.Text)6 Instance (gov.sandia.n2a.language.type.Instance)5 Simulator (gov.sandia.n2a.backend.internal.Simulator)4 EquationSet (gov.sandia.n2a.eqset.EquationSet)4 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)3 ArrayList (java.util.ArrayList)3 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)2 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)2 ParseException (gov.sandia.n2a.language.ParseException)2 Visitor (gov.sandia.n2a.language.Visitor)2 Random (java.util.Random)2 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)1 Conversion (gov.sandia.n2a.backend.internal.InternalBackendData.Conversion)1