Search in sources :

Example 21 with Variable

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

the class NodeVariable method enforceOneLine.

/**
 *        Ensures that a variable either has multiple equations or a one-line expression,
 *        but not both.
 *        This method is independent of any given NodeVariable so it can be used at
 *        various points in processing.
 */
public static void enforceOneLine(MNode source) {
    // Collect info
    int equationCount = 0;
    MNode equation = null;
    for (MNode n : source) {
        String key = n.key();
        if (key.startsWith("@")) {
            equation = n;
            equationCount++;
        }
    }
    String value = source.get();
    if (value.startsWith("$kill"))
        value = "";
    Variable.ParsedValue pieces = new Variable.ParsedValue(value);
    boolean empty = pieces.expression.isEmpty() && pieces.condition.isEmpty();
    // Collapse or expand
    if (equationCount > 0) {
        if (!empty) {
            // Expand
            source.set(pieces.combiner);
            // may override an existing equation
            source.set("@" + pieces.condition, pieces.expression);
        } else if (equationCount == 1) {
            // Collapse
            String key = equation.key();
            source.clear(key);
            if (key.equals("@"))
                source.set(pieces.combiner + equation.get());
            else
                source.set(pieces.combiner + equation.get() + key);
        }
    }
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) ChangeVariable(gov.sandia.n2a.ui.eq.undo.ChangeVariable) DeleteVariable(gov.sandia.n2a.ui.eq.undo.DeleteVariable) MNode(gov.sandia.n2a.db.MNode)

Example 22 with Variable

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

the class Output method dependOnIndex.

public void dependOnIndex(Variable v, EquationSet container) {
    while (container != null) {
        Variable index = container.find(new Variable("$index"));
        if (index != null && !container.isSingleton()) {
            v.addDependencyOn(index);
        }
        container = container.container;
    }
}
Also used : AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable)

Example 23 with Variable

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

the class EventSpikeSingle method run.

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

Example 24 with Variable

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

the class InternalBackendData method dumpVariableList.

public void dumpVariableList(String name, List<Variable> list) {
    System.out.print("  " + name + ":");
    for (Variable v : list) System.out.print(" " + v.nameString());
    System.out.println();
}
Also used : AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable)

Example 25 with Variable

use of gov.sandia.n2a.eqset.Variable 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)

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