Search in sources :

Example 16 with Variable

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

the class XyceBackend method generateNetlist.

public void generateNetlist(MNode job, Simulator simulator, FileWriter writer) throws Exception {
    Population toplevel = (Population) simulator.wrapper.valuesObject[0];
    XyceRenderer renderer = new XyceRenderer(simulator);
    // Header
    writer.append(toplevel.equations.name + "\n");
    writer.append("\n");
    writer.append("* seed: " + job.get("$metadata", "seed") + "\n");
    writer.append(".tran 0 " + job.get("$metadata", "duration") + "\n");
    // Equations
    for (Instance i : simulator) {
        if (i == simulator.wrapper)
            continue;
        writer.append("\n");
        writer.append("* " + i + "\n");
        renderer.pi = i;
        renderer.exceptions = null;
        XyceBackendData bed = (XyceBackendData) i.equations.backendData;
        if (bed.deviceSymbol != null) {
            writer.append(bed.deviceSymbol.getDefinition(renderer));
        }
        InstanceTemporaries temp = new InstanceTemporaries(i, simulator, false, bed.internal);
        for (final Variable v : i.equations.variables) {
            // Compute variable v
            // TODO: how to switch between multiple conditions that can be true during normal operation? IE: how to make Xyce code conditional?
            // Perhaps gate each condition (through a transistor?) and sum them at a single node.
            // e can be null
            EquationEntry e = v.select(temp);
            Symbol def = bed.equationSymbols.get(e);
            if (def == null)
                continue;
            writer.append(def.getDefinition(renderer));
            // Trace
            class TraceFinder extends Visitor {

                List<Operator> traces = new ArrayList<Operator>();

                public boolean visit(Operator op) {
                    if (op instanceof Output) {
                        traces.add(((Output) op).operands[0]);
                        return false;
                    }
                    return true;
                }
            }
            TraceFinder traceFinder = new TraceFinder();
            e.expression.visit(traceFinder);
            for (Operator trace : traceFinder.traces) {
                // We don't know if contents is .func, expression or a node, so always wrap in braces.
                writer.append(".print tran {");
                if (trace instanceof AccessVariable) {
                    AccessVariable av = (AccessVariable) trace;
                    writer.append(renderer.change(av.reference));
                } else // trace is an expression
                {
                    if (// this trace wraps the entire equation
                    e.expression instanceof Output && ((Output) e.expression).operands[0] == trace) {
                        // simply print the LHS variable, similar to the AccessVariable case above
                        writer.append(renderer.change(v.reference));
                    } else {
                        // arbitrary expression
                        writer.append(renderer.change(trace));
                    }
                }
                // one .print line per variable
                writer.append("}\n");
            }
        }
    }
    // Trailer
    writer.append(".end\n");
}
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) AccessVariable(gov.sandia.n2a.language.AccessVariable) Instance(gov.sandia.n2a.language.type.Instance) Symbol(gov.sandia.n2a.backend.xyce.netlist.Symbol) InstanceTemporaries(gov.sandia.n2a.backend.internal.InstanceTemporaries) XyceRenderer(gov.sandia.n2a.backend.xyce.netlist.XyceRenderer) Output(gov.sandia.n2a.language.function.Output) Population(gov.sandia.n2a.backend.internal.Population) ArrayList(java.util.ArrayList) List(java.util.List) EquationEntry(gov.sandia.n2a.eqset.EquationEntry)

Example 17 with Variable

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

the class XyceBackendData method analyze.

public void analyze(EquationSet s) {
    if (Device.isXyceDevice(s)) {
        deviceSymbol = new Device(s);
    }
    class ContainsOperator extends Visitor {

        @SuppressWarnings("rawtypes")
        public Class targetClass;

        boolean found;

        public boolean visit(Operator op) {
            if (found)
                return false;
            if (op.getClass().equals(targetClass)) {
                found = true;
                return false;
            }
            return true;
        }

        public boolean check(EquationEntry e) {
            found = false;
            e.expression.visit(this);
            return found;
        }
    }
    ContainsOperator containsPulse = new ContainsOperator();
    containsPulse.targetClass = Pulse.class;
    ContainsOperator containsSinewave = new ContainsOperator();
    containsSinewave.targetClass = Sinewave.class;
    ContainsVariable containsT = new ContainsVariable(new Variable("$t", 0));
    for (Variable v : s.variables) {
        // in a static (no structural dynamics) simulation, no $variable needs to be computed at runtime
        if (v.name.startsWith("$"))
            continue;
        // Constants are already subbed in. "initOnly" values are defined during init cycle, and can now be subbed during code generation.
        if (v.hasAttribute("constant") || v.hasAttribute("initOnly"))
            continue;
        for (EquationEntry eq : v.equations) {
            // don't need to write out equations defining dynamics already defined by a device
            if (Device.isXyceDevice(s) && Device.ignoreEquation(eq))
                continue;
            Symbol handler = null;
            if (eq.variable.order > 1) {
                Backend.err.get().println("Support for higher order differential equations not implemented yet (" + eq + ")");
                throw new Backend.AbortRun();
            } else if (eq.variable.order == 1) {
                handler = new SymbolStateVar1(eq);
            } else // The following are all order 0
            if (containsPulse.check(eq)) {
                handler = new SymbolPulse(eq);
            } else if (containsSinewave.check(eq)) {
                handler = new SymbolSinewave(eq);
            } else // TODO: this doesn't seem like an adequate test. Why would having a $t be the only reason to generate a zero-order symbol?
            if (containsT.check(eq.expression)) {
                handler = new SymbolStateVar0(eq);
            } else if (isExplicitInit(eq)) {
                handler = new SymbolConstantIC(eq);
            } else {
                // The RHS expression depends on state variables, so we create a netlist .func for it.
                handler = new SymbolFunc(eq);
            }
            equationSymbols.put(eq, handler);
            // May set the handler for v several times, but only the last one is kept. Multiple handlers should agree on symbol for reference. Better yet is to handle multiple equations together.
            variableSymbols.put(v.name, handler);
        }
    }
}
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) SymbolStateVar0(gov.sandia.n2a.backend.xyce.netlist.SymbolStateVar0) SymbolStateVar1(gov.sandia.n2a.backend.xyce.netlist.SymbolStateVar1) Device(gov.sandia.n2a.backend.xyce.netlist.Device) Symbol(gov.sandia.n2a.backend.xyce.netlist.Symbol) SymbolPulse(gov.sandia.n2a.backend.xyce.netlist.SymbolPulse) SymbolFunc(gov.sandia.n2a.backend.xyce.netlist.SymbolFunc) SymbolSinewave(gov.sandia.n2a.backend.xyce.netlist.SymbolSinewave) EquationEntry(gov.sandia.n2a.eqset.EquationEntry) SymbolConstantIC(gov.sandia.n2a.backend.xyce.netlist.SymbolConstantIC)

Example 18 with Variable

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

the class SymbolStateVar1 method getDefinition.

@Override
public String getDefinition(XyceRenderer renderer) {
    String translatedEq = renderer.change(eq.expression);
    Variable v = eq.variable;
    VariableReference r = v.reference;
    if (// symbol is defined here; no += allowed within same part
    r.index < 0) {
        return Xyceisms.defineDiffEq(v.name, renderer.pi.hashCode(), translatedEq);
    }
    // This symbol refers to a symbol in another part. We don't re-define the
    // variable, rather we create another diff eq that updates the existing one.
    Instance target = (Instance) renderer.pi.valuesObject[r.index];
    String thisVarname = r.variable.name + "_" + target.hashCode();
    String eqName = v.name + "_" + renderer.pi.hashCode();
    return Xyceisms.updateDiffEq(eqName, thisVarname, translatedEq);
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) VariableReference(gov.sandia.n2a.eqset.VariableReference) Instance(gov.sandia.n2a.language.type.Instance)

Example 19 with Variable

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

the class AccessElement method simplify.

public Operator simplify(Variable from) {
    for (int i = 0; i < operands.length; i++) operands[i] = operands[i].simplify(from);
    if (operands.length == 1) {
        from.changed = true;
        return operands[0];
    }
    // All operand positions beyond 0 are subscripts, presumably into a matrix at operands[0].
    // Attempt to replace the element access with a constant.
    int row = -1;
    int col = 0;
    if (operands[1] instanceof Constant) {
        Constant c = (Constant) operands[1];
        if (c.value instanceof Scalar)
            row = (int) ((Scalar) c.value).value;
    }
    if (operands.length > 2) {
        col = -1;
        if (operands[2] instanceof Constant) {
            Constant c = (Constant) operands[2];
            if (c.value instanceof Scalar)
                col = (int) ((Scalar) c.value).value;
        }
    }
    if (row < 0 || col < 0)
        return this;
    if (operands[0] instanceof Constant) {
        Constant c = (Constant) operands[0];
        if (c.value instanceof Matrix) {
            from.changed = true;
            return new Constant(new Scalar(((Matrix) c.value).get(row, col)));
        }
    } else {
        // Try to unpack the target variable and see if the specific element we want is constant
        AccessVariable av = (AccessVariable) operands[0];
        if (av.reference != null && av.reference.variable != null) {
            Variable v = av.reference.variable;
            if (v.equations != null && v.equations.size() == 1) {
                EquationEntry e = v.equations.first();
                if (// Ideally, we would also ensure e.condition is satisfied. However, only weird code would have a condition at all.
                e.expression instanceof BuildMatrix) {
                    BuildMatrix b = (BuildMatrix) e.expression;
                    Operator element = b.getElement(row, col);
                    if (element != null && element instanceof Constant) {
                        from.changed = true;
                        e.expression.releaseDependencies(from);
                        if (e.condition != null)
                            e.condition.releaseDependencies(from);
                        return element;
                    }
                }
            }
        }
    }
    return this;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) Variable(gov.sandia.n2a.eqset.Variable) EquationEntry(gov.sandia.n2a.eqset.EquationEntry) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 20 with Variable

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

the class NodeEquation method applyEdit.

@Override
public void applyEdit(JTree tree) {
    String input = (String) getUserObject();
    if (input.isEmpty()) {
        delete(tree, true);
        return;
    }
    // There are three possible outcomes of the edit:
    // 1) Nothing changed
    // 2) The name was not allowed to change
    // 3) Arbitrary change
    Variable.ParsedValue piecesBefore = new Variable.ParsedValue(source.get() + source.key());
    Variable.ParsedValue piecesAfter = new Variable.ParsedValue(input);
    NodeVariable parent = (NodeVariable) getParent();
    if (!piecesBefore.condition.equals(piecesAfter.condition)) {
        MPart partAfter = (MPart) parent.source.child("@" + piecesAfter.condition);
        if (// Can't overwrite another top-document node
        partAfter != null && partAfter.isFromTopDocument()) {
            piecesAfter.condition = piecesBefore.condition;
        }
    }
    if (piecesBefore.equals(piecesAfter)) {
        FilteredTreeModel model = (FilteredTreeModel) tree.getModel();
        FontMetrics fm = getFontMetrics(tree);
        parent.updateTabStops(fm);
        parent.allNodesChanged(model);
        return;
    }
    // The fact that we are modifying an existing equation node indicates that the variable (parent) should only contain a combiner.
    piecesBefore.combiner = parent.source.get();
    if (piecesAfter.combiner.isEmpty())
        piecesAfter.combiner = piecesBefore.combiner;
    PanelModel.instance.undoManager.add(new ChangeEquation(parent, piecesBefore.condition, piecesBefore.combiner, piecesBefore.expression, piecesAfter.condition, piecesAfter.combiner, piecesAfter.expression));
}
Also used : ChangeEquation(gov.sandia.n2a.ui.eq.undo.ChangeEquation) MPart(gov.sandia.n2a.eqset.MPart) Variable(gov.sandia.n2a.eqset.Variable) FontMetrics(java.awt.FontMetrics) FilteredTreeModel(gov.sandia.n2a.ui.eq.FilteredTreeModel)

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