Search in sources :

Example 6 with Operator

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

the class InternalBackendData method analyzeEvents.

public static void analyzeEvents(final EquationSet s, final List<EventTarget> eventTargets, final List<Variable> eventReferences) {
    class EventVisitor extends Visitor {

        public boolean found;

        public boolean visit(Operator op) {
            if (op instanceof Event) {
                found = true;
                Event de = (Event) op;
                if (// this event has not yet been analyzed
                de.eventType == null) {
                    final EventTarget et = new EventTarget(de);
                    int targetIndex = eventTargets.indexOf(et);
                    if (// event target already exists
                    targetIndex >= 0) {
                        de.eventType = eventTargets.get(targetIndex);
                    } else // we must create a new event target, or more properly, fill in the event target we just used as a query object
                    {
                        // Create an entry and save the index
                        targetIndex = eventTargets.size();
                        eventTargets.add(et);
                        de.eventType = et;
                        et.container = s;
                        // Determine edge type
                        if (de.operands.length < 3) {
                            et.edge = EventTarget.RISE;
                        } else if (de.operands[2] instanceof Constant) {
                            Constant c = (Constant) de.operands[2];
                            if (c.value instanceof Text) {
                                Text t = (Text) c.value;
                                if (t.value.equalsIgnoreCase("nonzero"))
                                    et.edge = EventTarget.NONZERO;
                                else if (t.value.equalsIgnoreCase("change"))
                                    et.edge = EventTarget.CHANGE;
                                else if (t.value.equalsIgnoreCase("fall"))
                                    et.edge = EventTarget.FALL;
                                else
                                    et.edge = EventTarget.RISE;
                            } else {
                                Backend.err.get().println("ERROR: event() edge type must be a string.");
                                throw new Backend.AbortRun();
                            }
                        } else {
                            Backend.err.get().println("ERROR: event() edge type must be constant.");
                            throw new Backend.AbortRun();
                        }
                        // Allocate auxiliary variable
                        if (de.operands[0] instanceof AccessVariable) {
                            AccessVariable av = (AccessVariable) de.operands[0];
                            VariableReference reference = av.reference;
                            Variable v = reference.variable;
                            // then the user has broken the rule that we can't see temporaries in other parts.
                            if (v.hasAttribute("temporary") && v.container != s) {
                                Backend.err.get().println("WARNING: Cannot be temporary due to event monitor: " + v.container.name + "." + v.nameString() + " from " + s.name);
                                v.removeAttribute("temporary");
                            }
                            // so fall through to the !trackOne case below.
                            if (!v.hasAttribute("temporary")) {
                                // ensure it's buffered, so we can detect change
                                v.addAttribute("externalRead");
                                et.trackOne = true;
                                // just a holder for the reference
                                et.track = new Variable();
                                et.track.reference = reference;
                            }
                        }
                        if (// Expression, so create auxiliary variable. Aux not needed for NONZERO, because no change detection.
                        !et.trackOne && et.edge != EventTarget.NONZERO) {
                            et.track = new Variable("eventAux" + targetIndex, 0);
                            et.track.type = new Scalar(0);
                            et.track.reference = new VariableReference();
                            et.track.reference.variable = et.track;
                        }
                        // Locate any temporaries for evaluation. TODO: for more efficiency, we could have separate lists of temporaries for the condition and delay operands
                        // Tie into the dependency graph using a phantom variable (which can go away afterward without damaging the graph).
                        final Variable phantom = new Variable("event");
                        phantom.uses = new IdentityHashMap<Variable, Integer>();
                        for (int i = 0; i < et.event.operands.length; i++) et.event.operands[i].visit(new Visitor() {

                            public boolean visit(Operator op) {
                                if (op instanceof AccessVariable) {
                                    AccessVariable av = (AccessVariable) op;
                                    Variable v = av.reference.variable;
                                    if (!phantom.uses.containsKey(v))
                                        phantom.uses.put(v, 1);
                                    return false;
                                }
                                return true;
                            }
                        });
                        // Scan all variables in equation set to see if we need them
                        for (Variable t : s.variables) {
                            if (t.hasAttribute("temporary") && phantom.dependsOn(t) != null)
                                et.dependencies.add(t);
                        }
                        // Note the default is already set to -1 (no care)
                        class DelayVisitor extends Visitor {

                            TreeSet<EquationSet> containers = new TreeSet<EquationSet>();

                            public boolean visit(Operator op) {
                                if (op instanceof AccessVariable) {
                                    AccessVariable av = (AccessVariable) op;
                                    // could include the target part itself, if in fact we use local variables
                                    containers.add(av.reference.variable.container);
                                    return false;
                                }
                                return true;
                            }
                        }
                        DelayVisitor dv = new DelayVisitor();
                        if (de.operands.length >= 2) {
                            if (de.operands[1] instanceof Constant) {
                                Constant c = (Constant) de.operands[1];
                                et.delay = (float) ((Scalar) c.value).value;
                                if (et.delay < 0)
                                    et.delay = -1;
                            } else {
                                // indicates that we need to evaluate delay at run time
                                et.delay = -2;
                                de.operands[1].visit(dv);
                            }
                        }
                        // Set up monitors in source parts
                        class ConditionVisitor extends Visitor {

                            TreeSet<EquationSet> containers = new TreeSet<EquationSet>();

                            public boolean visit(Operator op) {
                                if (op instanceof AccessVariable) {
                                    AccessVariable av = (AccessVariable) op;
                                    Variable v = av.reference.variable;
                                    EquationSet sourceContainer = v.container;
                                    containers.add(sourceContainer);
                                    // Set up monitors for values that can vary during update.
                                    if (!v.hasAttribute("constant") && !v.hasAttribute("initOnly") && !et.monitors(sourceContainer)) {
                                        EventSource es = new EventSource(sourceContainer, et);
                                        // null means self-reference, a special case handled in Part
                                        if (sourceContainer != s)
                                            es.reference = av.reference;
                                        et.sources.add(es);
                                    }
                                    return false;
                                }
                                return true;
                            }
                        }
                        ConditionVisitor cv = new ConditionVisitor();
                        de.operands[0].visit(cv);
                        // Special case for event with no references that vary
                        if (et.sources.isEmpty()) {
                            // We can avoid creating a self monitor if we know for certain that the event will never fire
                            boolean neverFires = false;
                            if (de.operands[0] instanceof Constant) {
                                if (et.edge == EventTarget.NONZERO) {
                                    Type op0 = ((Constant) de.operands[0]).value;
                                    if (op0 instanceof Scalar) {
                                        neverFires = ((Scalar) op0).value == 0;
                                    } else {
                                        Backend.err.get().println("ERROR: Condition for event() must resolve to a number.");
                                        throw new Backend.AbortRun();
                                    }
                                } else {
                                    neverFires = true;
                                }
                            }
                            if (!neverFires) {
                                EventSource es = new EventSource(s, et);
                                // This is a self-reference, so es.reference should be null.
                                et.sources.add(es);
                            }
                        }
                        // Determine if monitor needs to test every target, or if one representative target is sufficient
                        for (EventSource source : et.sources) {
                            // associated with any given source instance, so every target must be evaluated separately.
                            if (cv.containers.size() > 1)
                                source.testEach = true;
                            if (dv.containers.size() > 1 || (dv.containers.size() == 1 && dv.containers.first() != source.container))
                                source.delayEach = true;
                        }
                    }
                }
            }
            return true;
        }
    }
    EventVisitor eventVisitor = new EventVisitor();
    for (Variable v : s.variables) {
        eventVisitor.found = false;
        v.visit(eventVisitor);
        if ((eventVisitor.found || v.dependsOnEvent()) && v.reference.variable != v)
            eventReferences.add(v);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar) TreeSet(java.util.TreeSet) EquationSet(gov.sandia.n2a.eqset.EquationSet) AccessVariable(gov.sandia.n2a.language.AccessVariable) VariableReference(gov.sandia.n2a.eqset.VariableReference) Text(gov.sandia.n2a.language.type.Text) Backend(gov.sandia.n2a.plugins.extpoints.Backend) Type(gov.sandia.n2a.language.Type) Event(gov.sandia.n2a.language.function.Event)

Example 7 with Operator

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

the class Max method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    // Check if Max appears as an operand. If so, merge its operands into ours
    ArrayList<Operator> newOperands = new ArrayList<Operator>(operands.length);
    boolean changed = false;
    for (int i = 0; i < operands.length; i++) {
        Operator o = operands[i];
        if (o instanceof Max) {
            Max m = (Max) o;
            newOperands.addAll(Arrays.asList(m.operands));
            changed = true;
        } else {
            newOperands.add(o);
        }
    }
    if (changed) {
        from.changed = true;
        Max newMax = new Max();
        newMax.operands = newOperands.toArray(new Operator[0]);
        return result;
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) ArrayList(java.util.ArrayList)

Example 8 with Operator

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

the class Output method determineVariableName.

// This method should be called by analysis, with v set to the variable that holds this equation.
public void determineVariableName(Variable v) {
    // Ensure that the first operand is a file name.
    // If no file name is specified, stdout is used. We get the same effect by specifying the empty string, so insert it here.
    // This makes parameter processing much simpler elsewhere.
    int length = operands.length;
    if (length > 0 && !isStringExpression(v, operands[0])) {
        variableName = variableName0;
        Operator[] newOperands = new Operator[length + 1];
        for (int i = 0; i < length; i++) newOperands[i + 1] = operands[i];
        newOperands[0] = new Constant(new Text());
        operands = newOperands;
    } else {
        variableName = variableName1;
    }
    if (// Column name not specified
    length < 3) {
        if (variableName == null)
            variableName = v.nameString();
        EquationSet container = v.container;
        if (// regular part
        container.connectionBindings == null) {
            dependOnIndex(v, container);
        } else // connection
        {
            // depend on all endpoints
            for (ConnectionBinding c : container.connectionBindings) {
                dependOnIndex(v, c.endpoint);
            }
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) Constant(gov.sandia.n2a.language.Constant) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) Text(gov.sandia.n2a.language.type.Text)

Example 9 with Operator

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

the class Output method isStringExpression.

/**
 *        Determines if the given operator is a string expression.
 *        This is the case if any operator it depends on is a string.
 *        If an operand is a variable, then its equations must contain a string.
 */
public static boolean isStringExpression(Variable v, Operator op) {
    class StringVisitor extends Visitor {

        boolean foundString;

        Variable from;

        public StringVisitor(Variable from) {
            this.from = from;
            from.visited = null;
        }

        public boolean visit(Operator op) {
            if (op instanceof Constant) {
                Constant c = (Constant) op;
                if (c.value instanceof Text)
                    foundString = true;
                return false;
            }
            if (op instanceof AccessVariable) {
                AccessVariable av = (AccessVariable) op;
                Variable v = av.reference.variable;
                // Prevent infinite recursion
                Variable p = from;
                while (p != null) {
                    if (p == v)
                        return false;
                    p = p.visited;
                }
                v.visited = from;
                from = v;
                v.visit(this);
                from = v.visited;
                return false;
            }
            // no reason to dig any further
            if (foundString)
                return false;
            // Add is the only operator that can propagate string values. All other operators and functions return scalars or matrices.
            return op instanceof Add;
        }
    }
    StringVisitor visitor = new StringVisitor(v);
    op.visit(visitor);
    return visitor.foundString;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text)

Example 10 with Operator

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

the class AND 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) {
                operand1.releaseDependencies(from);
                return new Constant(new Scalar(0));
            }
            return operand1;
        }
    } 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) {
                operand0.releaseDependencies(from);
                return new Constant(new Scalar(0));
            }
            return operand0;
        }
    }
    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)

Aggregations

Operator (gov.sandia.n2a.language.Operator)27 AccessVariable (gov.sandia.n2a.language.AccessVariable)17 Constant (gov.sandia.n2a.language.Constant)14 Visitor (gov.sandia.n2a.language.Visitor)11 Type (gov.sandia.n2a.language.Type)10 Scalar (gov.sandia.n2a.language.type.Scalar)10 Variable (gov.sandia.n2a.eqset.Variable)9 ArrayList (java.util.ArrayList)7 EquationSet (gov.sandia.n2a.eqset.EquationSet)6 Output (gov.sandia.n2a.language.function.Output)6 Text (gov.sandia.n2a.language.type.Text)6 TreeSet (java.util.TreeSet)4 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)3 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 Input (gov.sandia.n2a.language.function.Input)3 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)2 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)2 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)2 Function (gov.sandia.n2a.language.Function)2 ParseException (gov.sandia.n2a.language.ParseException)2