Search in sources :

Example 41 with AccessVariable

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

the class InternalBackendData method analyzeEvents.

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

        public boolean found;

        public boolean visit(Operator op) {
            if (op instanceof Delay) {
                delays.add((Delay) op);
            } else 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.fullName() + " 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);
                            et.track.container = s;
                            et.track.type = new Scalar(0);
                            et.track.reference = new VariableReference();
                            et.track.reference.variable = et.track;
                            // Make executable so it can be directly evaluated during the init cycle.
                            et.track.equations = new TreeSet<EquationEntry>();
                            EquationEntry ee = new EquationEntry(et.track, "");
                            et.track.equations.add(ee);
                            ee.expression = et.event.operands[0].deepCopy();
                            ee.expression.addDependencies(et.track);
                        }
                        // Locate any temporaries for evaluation.
                        // Tie into the dependency graph using a phantom variable (which can go away afterward without damaging the graph).
                        // TODO: for more efficiency, we could have separate lists of temporaries for the condition and delay operands
                        // TODO: for more efficiency, cut off search for temporaries along a given branch of the tree at the first non-temporary.
                        final Variable phantom = new Variable("event");
                        phantom.uses = new IdentityHashMap<Variable, Integer>();
                        phantom.container = s;
                        et.event.visit(new Visitor() {

                            public boolean visit(Operator op) {
                                if (op instanceof AccessVariable) {
                                    AccessVariable av = (AccessVariable) op;
                                    Variable v = av.reference.variable;
                                    if (v.hasAttribute("temporary") && !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.ordered) {
                            if (t.hasAttribute("temporary") && phantom.dependsOn(t) != null)
                                et.dependencies.add(t);
                        }
                        // Note the default is already set to -1 (no care)
                        class DelayVisitor implements 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 implements 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) EquationSet(gov.sandia.n2a.eqset.EquationSet) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) VariableReference(gov.sandia.n2a.eqset.VariableReference) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) IdentityHashMap(java.util.IdentityHashMap) Text(gov.sandia.n2a.language.type.Text) Delay(gov.sandia.n2a.language.function.Delay) Scalar(gov.sandia.n2a.language.type.Scalar) Type(gov.sandia.n2a.language.Type) Backend(gov.sandia.n2a.plugins.extpoints.Backend) TreeSet(java.util.TreeSet) Event(gov.sandia.n2a.language.function.Event) EquationEntry(gov.sandia.n2a.eqset.EquationEntry)

Example 42 with AccessVariable

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

the class UnitMap method determineExponentNext.

public void determineExponentNext() {
    // See AccessElement for similar code ...
    Operator v = operands[0];
    if (// The preferred use of this function is to access a non-calculated matrix.
    v instanceof Constant || v instanceof AccessVariable) {
        // Let matrix output in its natural exponent.
        v.exponentNext = v.exponent;
        // Force our output to be shifted after the fact.
        exponent = v.exponent;
    } else // A matrix expression of some sort, so pass the required exponent on to it. This case should be rare.
    {
        // Pass the required exponent on to the expression
        v.exponentNext = exponentNext;
        // and expect that we require not further adjustment.
        exponent = exponentNext;
    }
    v.determineExponentNext();
    // Other operands are either float coordinates or a mode string. In the latter case, it doesn't matter what exponent we set.
    for (int i = 1; i < operands.length; i++) {
        Operator op = operands[i];
        // a number in [0,1] with some provision for going slightly out of bounds
        op.exponentNext = 0;
        op.determineExponentNext();
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) AccessVariable(gov.sandia.n2a.language.AccessVariable) Constant(gov.sandia.n2a.language.Constant)

Aggregations

AccessVariable (gov.sandia.n2a.language.AccessVariable)42 Operator (gov.sandia.n2a.language.Operator)31 Visitor (gov.sandia.n2a.language.Visitor)16 Variable (gov.sandia.n2a.eqset.Variable)15 Constant (gov.sandia.n2a.language.Constant)15 Scalar (gov.sandia.n2a.language.type.Scalar)13 ArrayList (java.util.ArrayList)12 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)11 TreeSet (java.util.TreeSet)11 EquationSet (gov.sandia.n2a.eqset.EquationSet)10 Type (gov.sandia.n2a.language.Type)9 Instance (gov.sandia.n2a.language.type.Instance)7 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)6 VariableReference (gov.sandia.n2a.eqset.VariableReference)6 Output (gov.sandia.n2a.language.function.Output)6 Transformer (gov.sandia.n2a.language.Transformer)5 Event (gov.sandia.n2a.language.function.Event)5 Text (gov.sandia.n2a.language.type.Text)5 MNode (gov.sandia.n2a.db.MNode)4 AccessElement (gov.sandia.n2a.language.AccessElement)4