use of gov.sandia.n2a.language.Visitor 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");
}
use of gov.sandia.n2a.language.Visitor 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);
}
}
}
use of gov.sandia.n2a.language.Visitor in project n2a by frothga.
the class EquationSet method determineTraceVariableName.
public void determineTraceVariableName() {
for (EquationSet s : parts) {
s.determineTraceVariableName();
}
class TraceVisitor extends Visitor {
public Variable v;
public boolean visit(Operator op) {
if (op instanceof Output) {
((Output) op).determineVariableName(v);
return false;
}
return true;
}
}
TraceVisitor visitor = new TraceVisitor();
for (Variable v : variables) {
visitor.v = v;
v.visit(visitor);
}
}
use of gov.sandia.n2a.language.Visitor in project n2a by frothga.
the class Variable method dependsOnEvent.
protected boolean dependsOnEvent(Variable from) {
// Prevent infinite recursion
Variable p = from;
while (p != null) {
if (p == this)
return false;
p = p.visited;
}
visited = from;
// Scan temporary variables we depend on
if (uses != null) {
for (Variable u : uses.keySet()) {
if (!u.hasAttribute("temporary"))
continue;
if (u.dependsOnEvent(this))
return true;
}
}
// Scan equations
class EventVisitor extends Visitor {
boolean found;
public boolean visit(Operator op) {
if (op instanceof Event)
found = true;
return !found;
}
}
EventVisitor visitor = new EventVisitor();
visit(visitor);
return visitor.found;
}
use of gov.sandia.n2a.language.Visitor in project n2a by frothga.
the class ExportJob method analyze.
/**
* Find references to $index in connection endpoints, and set up info for ConnectionContext.
*/
public void analyze(EquationSet s) {
for (EquationSet p : s.parts) analyze(p);
for (final Variable v : s.variables) {
Visitor visitor = new Visitor() {
public boolean visit(Operator op) {
if (op instanceof AccessVariable) {
VariableReference r = ((AccessVariable) op).reference;
Variable rv = r.variable;
if (rv.container != v.container && !r.resolution.isEmpty()) {
Object o = r.resolution.get(r.resolution.size() - 1);
if (o instanceof ConnectionBinding) {
ConnectionBinding c = (ConnectionBinding) o;
// This is somewhat of a hack, but ConnectionContext assumes the mappings A->0 and B->1.
if (c.alias.equals("A"))
r.index = 0;
if (c.alias.equals("B"))
r.index = 1;
}
}
}
return true;
}
};
v.visit(visitor);
}
}
Aggregations