Search in sources :

Example 21 with Scalar

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

the class Pulse method eval.

public Type eval(Instance context) {
    double t = ((Scalar) operands[0].eval(context)).value;
    double width = ((Scalar) operands[1].eval(context)).value;
    double period = 0;
    double rise = 0;
    double fall = 0;
    if (operands.length >= 3)
        period = ((Scalar) operands[2].eval(context)).value;
    if (operands.length >= 4)
        rise = ((Scalar) operands[3].eval(context)).value;
    if (operands.length >= 5)
        fall = ((Scalar) operands[4].eval(context)).value;
    if (period == 0.0) {
        if (t < 0)
            return new Scalar(0);
    } else
        t %= period;
    if (t < rise)
        return new Scalar(t / rise);
    t -= rise;
    if (t < width)
        return new Scalar(1);
    t -= width;
    if (t < fall)
        return new Scalar(1.0 - t / fall);
    return new Scalar(0);
}
Also used : Scalar(gov.sandia.n2a.language.type.Scalar)

Example 22 with Scalar

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

the class XyceRenderer method render.

public boolean render(Operator op) {
    if (op instanceof AccessVariable) {
        AccessVariable av = (AccessVariable) op;
        result.append(change(av.reference));
        return true;
    }
    // IE: they are not an ongoing source of randomness during execution.
    if (op instanceof Uniform) {
        Uniform u = (Uniform) op;
        if (u.operands.length > 0) {
            int dimension = (int) Math.round(((Scalar) u.operands[0].eval(pi)).value);
            if (dimension != 1)
                System.err.println("WARNING: Xyce does not support multivariate form of uniform()");
        }
        result.append("rand()");
        return true;
    }
    if (op instanceof Gaussian) {
        Gaussian g = (Gaussian) op;
        if (g.operands.length > 0) {
            int dimension = (int) Math.round(((Scalar) g.operands[0].eval(pi)).value);
            if (dimension != 1)
                System.err.println("WARNING: Xyce does not support multivariate form of gaussian()");
        }
        result.append("agauss(0,6,6)");
        return true;
    }
    if (op instanceof Power) {
        Power p = (Power) op;
        result.append("(");
        p.operand0.render(this);
        result.append(") ** (");
        p.operand1.render(this);
        result.append(")");
        return true;
    }
    return false;
}
Also used : AccessVariable(gov.sandia.n2a.language.AccessVariable) Uniform(gov.sandia.n2a.language.function.Uniform) Gaussian(gov.sandia.n2a.language.function.Gaussian) Power(gov.sandia.n2a.language.operator.Power) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 23 with Scalar

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

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

the class EquationSet method findInitOnlyRecursive.

public boolean findInitOnlyRecursive() {
    boolean changed = false;
    for (EquationSet s : parts) {
        if (s.findInitOnlyRecursive())
            changed = true;
    }
    ReplaceInit replaceInit = new ReplaceInit();
    for (final Variable v : variables) {
        // Note: some variables get tagged "initOnly" by other means, so don't re-process
        if (v.hasAny(new String[] { "initOnly", "constant", "dummy" }))
            continue;
        // Count equations
        int firesDuringInit = 0;
        int firesDuringUpdate = 0;
        // If we have a single update equation, then we may still be initOnly if it depends only on constants or other initOnly variables. Save the update equation for analysis.
        EquationEntry update = null;
        for (EquationEntry e : v.equations) {
            if (e.condition == null) {
                firesDuringInit++;
                firesDuringUpdate++;
                update = e;
            } else {
                // init
                replaceInit.init = 1;
                replaceInit.live = 1;
                Operator test = e.condition.deepCopy().transform(replaceInit).simplify(v);
                boolean fires = true;
                if (test instanceof Constant) {
                    Constant c = (Constant) test;
                    if (c.value instanceof Scalar && ((Scalar) c.value).value == 0)
                        fires = false;
                }
                if (fires)
                    firesDuringInit++;
                // update
                replaceInit.init = 0;
                replaceInit.live = 1;
                test = e.condition.deepCopy().transform(replaceInit).simplify(v);
                fires = true;
                if (test instanceof Constant) {
                    Constant c = (Constant) test;
                    if (c.value instanceof Scalar && ((Scalar) c.value).value == 0)
                        fires = false;
                }
                if (fires) {
                    firesDuringUpdate++;
                    update = e;
                }
            }
        }
        if (firesDuringUpdate == 0) {
            if (firesDuringInit > 0 && v.derivative == null) {
                v.addAttribute("initOnly");
                changed = true;
            }
        } else if (// last chance to be "initOnly": must be exactly one equation that is not a combining operator
        firesDuringUpdate == 1 && v.assignment == Variable.REPLACE) {
            // Determine if our single update equation depends only on constants and initOnly variables
            class VisitInitOnly extends Visitor {

                // until something falsifies it
                boolean isInitOnly = true;

                public boolean visit(Operator op) {
                    if (isInitOnly) {
                        if (op instanceof AccessVariable) {
                            AccessVariable av = (AccessVariable) op;
                            // constant has already been replaced. Therefore, only the "initOnly" attribute matters here.
                            if (av.reference == null || av.reference.variable == null)
                                isInitOnly = false;
                            Variable r = av.reference.variable;
                            if (!r.hasAttribute("initOnly"))
                                isInitOnly = false;
                            // Also verify that the variables we depend on are available during the appropriate phase of init
                            if (// Note that temporaries are always available.
                            isInitOnly && !r.hasAttribute("temporary")) {
                                if (// we are a $variable, so we can only depend on $index and $init
                                v.name.startsWith("$")) {
                                    if (!"$index,$init,$live".contains(r.name))
                                        isInitOnly = false;
                                } else // we are a regular variable, so can only depend on $variables
                                {
                                    if (!r.name.startsWith("$"))
                                        isInitOnly = false;
                                }
                            }
                        } else if (op instanceof Function) {
                            Function f = (Function) op;
                            if (!f.canBeInitOnly())
                                isInitOnly = false;
                        }
                    }
                    return isInitOnly;
                }
            }
            VisitInitOnly visitor = new VisitInitOnly();
            if (update.condition != null)
                update.condition.visit(visitor);
            if (visitor.isInitOnly) {
                update.expression.visit(visitor);
                if (visitor.isInitOnly) {
                    v.addAttribute("initOnly");
                    changed = true;
                }
            }
        }
        if (firesDuringUpdate > 0 && v.derivative != null && !v.hasAttribute("initOnly"))
            v.addAttribute("updates");
        else
            v.removeAttribute("updates");
    }
    return changed;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Function(gov.sandia.n2a.language.Function) AccessVariable(gov.sandia.n2a.language.AccessVariable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 25 with Scalar

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

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