Search in sources :

Example 36 with Scalar

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

the class OR method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    if (operand0 instanceof Constant) {
        Type c0 = ((Constant) operand0).value;
        if (c0 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c0).value;
            if (value == 0)
                return operand1;
            else
                return new Constant(new Scalar(1));
        }
    } else if (operand1 instanceof Constant) {
        Type c1 = ((Constant) operand1).value;
        if (c1 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c1).value;
            if (value == 0)
                return operand0;
            else
                return new Constant(new Scalar(1));
        }
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 37 with Scalar

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

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

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

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

the class Population method insert.

@SuppressWarnings("unchecked")
public void insert(Part p) {
    n++;
    InternalBackendData bed = (InternalBackendData) equations.backendData;
    if (bed.index != null) {
        int index;
        if (valuesObject[bed.indexAvailable] == null) {
            index = (int) valuesFloat[bed.indexNext]++;
        } else {
            ArrayList<Integer> availableIndex = (ArrayList<Integer>) valuesObject[bed.indexAvailable];
            index = availableIndex.remove(availableIndex.size() - 1);
            if (availableIndex.size() < 1)
                valuesObject[bed.indexAvailable] = null;
        }
        p.set(bed.index, new Scalar(index));
        if (bed.instances >= 0) {
            ArrayList<Part> instances = (ArrayList<Part>) valuesObject[bed.instances];
            if (instances == null) {
                instances = new ArrayList<Part>(index + 1);
                valuesObject[bed.instances] = instances;
            }
            for (int size = instances.size(); size <= index; size++) instances.add(null);
            instances.set(index, p);
            if (equations.connected) {
                p.valuesFloat[bed.newborn] = 1;
                valuesFloat[bed.firstborn] = Math.min(valuesFloat[bed.firstborn], index);
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Scalar (gov.sandia.n2a.language.type.Scalar)42 Type (gov.sandia.n2a.language.Type)17 AccessVariable (gov.sandia.n2a.language.AccessVariable)14 Variable (gov.sandia.n2a.eqset.Variable)13 Matrix (gov.sandia.n2a.language.type.Matrix)11 Constant (gov.sandia.n2a.language.Constant)10 Operator (gov.sandia.n2a.language.Operator)10 Text (gov.sandia.n2a.language.type.Text)8 EquationSet (gov.sandia.n2a.eqset.EquationSet)6 ArrayList (java.util.ArrayList)6 Simulator (gov.sandia.n2a.backend.internal.Simulator)5 Instance (gov.sandia.n2a.language.type.Instance)5 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)5 TreeSet (java.util.TreeSet)5 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)4 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)4 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)3 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)2 EvaluationException (gov.sandia.n2a.language.EvaluationException)2