Search in sources :

Example 11 with Type

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

the class MultiplyElementwise method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    // This will be reversed below if we don't actually make a change.
    from.changed = true;
    if (operand0 instanceof Constant) {
        Type c0 = ((Constant) operand0).value;
        if (c0 instanceof Scalar) {
            double value = ((Scalar) c0).value;
            if (value == 1)
                return operand1;
        }
    } else if (operand1 instanceof Constant) {
        Type c1 = ((Constant) operand1).value;
        if (c1 instanceof Scalar) {
            double value = ((Scalar) c1).value;
            if (value == 1)
                return operand0;
        }
    }
    from.changed = false;
    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 12 with Type

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

the class EquationSet method determineDuration.

/**
 *        Infers length of simulation based on contents of equation set, particularly
 *        an expression for top-level $p that involves $t.
 *        Depends on results of: determineTypes() -- to set up Variable.type member with usable values
 */
public void determineDuration() {
    Variable p = find(new Variable("$p", 0));
    if (p != null && p.equations.size() == 1) {
        Operator expression = p.equations.first().expression;
        Operator variable = null;
        Operator value = null;
        if (// only true if expression is not null
        expression instanceof LT || expression instanceof LE) {
            OperatorBinary comparison = (OperatorBinary) expression;
            variable = comparison.operand0;
            value = comparison.operand1;
        } else if (expression instanceof GT || expression instanceof GE) {
            OperatorBinary comparison = (OperatorBinary) expression;
            variable = comparison.operand1;
            value = comparison.operand0;
        }
        if (variable instanceof AccessVariable && ((AccessVariable) variable).name.equals("$t")) {
            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;
                }
            };
            Type result = value.eval(instance);
            if (result instanceof Scalar)
                setNamedValue("duration", new Double(((Scalar) result).value).toString());
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) AccessVariable(gov.sandia.n2a.language.AccessVariable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Instance(gov.sandia.n2a.language.type.Instance) LT(gov.sandia.n2a.language.operator.LT) GT(gov.sandia.n2a.language.operator.GT) OperatorBinary(gov.sandia.n2a.language.OperatorBinary) Scalar(gov.sandia.n2a.language.type.Scalar) Type(gov.sandia.n2a.language.Type) LE(gov.sandia.n2a.language.operator.LE) GE(gov.sandia.n2a.language.operator.GE)

Example 13 with Type

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

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

the class EquationSet method findLethalVariables.

public void findLethalVariables() {
    for (EquationSet s : parts) {
        s.findLethalVariables();
    }
    // Determine if $n can decrease
    Variable n = find(new Variable("$n"));
    if (n != null) {
        for (EquationEntry e : n.equations) {
            // Even if each expression is constant, $n could still change during operation if it is a multi-conditional.
            if (e.condition != null && !(e.condition instanceof Constant)) {
                lethalN = true;
                break;
            }
            if (!(e.expression instanceof Constant)) {
                lethalN = true;
                break;
            }
        }
    }
    // Determine if $p has an assignment less than 1
    Variable p = find(new Variable("$p"));
    if (p != null) {
        // Determine if any equation is capable of setting $p to something besides 1
        ReplaceInit replaceInit = new ReplaceInit();
        for (EquationEntry e : p.equations) {
            if (e.expression instanceof Constant) {
                Type value = ((Constant) e.expression).value;
                if (value instanceof Scalar) {
                    if (((Scalar) value).value == 1.0)
                        continue;
                }
            }
            // Check if condition fires during init phase
            if (e.condition != null) {
                replaceInit.init = 1;
                replaceInit.live = 1;
                Operator test = e.condition.deepCopy().transform(replaceInit).simplify(p);
                if (test instanceof Constant) {
                    Constant c = (Constant) test;
                    if (// Does not fire during init phase
                    c.value instanceof Scalar && ((Scalar) c.value).value == 0) {
                        // Check if condition fires during update phase
                        replaceInit.init = 0;
                        test = e.condition.deepCopy().transform(replaceInit).simplify(p);
                        if (test instanceof Constant) {
                            c = (Constant) test;
                            // Does not fire during update phase
                            if (c.value instanceof Scalar && ((Scalar) c.value).value == 0)
                                continue;
                        }
                    }
                }
            }
            lethalP = true;
            break;
        }
    }
    // Determine if any splits kill this part
    for (// my splits are the parts I can split into
    ArrayList<EquationSet> split : // my splits are the parts I can split into
    splits) {
        if (!split.contains(this)) {
            lethalType = true;
            break;
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) AccessVariable(gov.sandia.n2a.language.AccessVariable) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 15 with Type

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

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