Search in sources :

Example 1 with Population

use of gov.sandia.n2a.backend.internal.Population in project n2a by frothga.

the class Instance method path.

/**
 *        Return a unique name for this instance within the simulation.
 *        Name consists of the equation set name combined with the index of this part,
 *        prefixed by the path to the parent part. In the case of a connection, the name
 *        consists of the path to each of the connected parts.
 */
public String path() {
    // In general, container is never null, as that would only happen if this is the instance of Wrapper.
    boolean top = container instanceof Wrapper || container == null;
    String result;
    if (top)
        result = "";
    else
        result = equations.name;
    InternalBackendData bed = (InternalBackendData) equations.backendData;
    if (bed.index != null && !(this instanceof Population))
        result += get(bed.index);
    if (top)
        return result;
    String prefix = container.path();
    if (prefix.isEmpty())
        return result;
    return prefix + "." + result;
}
Also used : Wrapper(gov.sandia.n2a.backend.internal.Wrapper) Population(gov.sandia.n2a.backend.internal.Population) InternalBackendData(gov.sandia.n2a.backend.internal.InternalBackendData)

Example 2 with Population

use of gov.sandia.n2a.backend.internal.Population in project n2a by frothga.

the class XyceBackend method generateNetlist.

public void generateNetlist(MNode job, Simulator simulator, BufferedWriter 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("seed") + "\n");
    writer.append(".tran 0 " + job.get("duration") + "\n");
    MNode integrator = job.child("integrator");
    if (integrator != null) {
        String method = integrator.get();
        if (!method.isEmpty()) {
            writer.append(".options timeint method=" + method + "\n");
        }
    // TODO: add other integrator options
    }
    // 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, 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 implements Visitor {

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

                public boolean visit(Operator op) {
                    if (op instanceof Output) {
                        traces.add(((Output) op).operands[1]);
                        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[1] == 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) MNode(gov.sandia.n2a.db.MNode) 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 3 with Population

use of gov.sandia.n2a.backend.internal.Population 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 4 with Population

use of gov.sandia.n2a.backend.internal.Population in project n2a by frothga.

the class Instance method resolve.

public void resolve(TreeSet<VariableReference> references) {
    for (VariableReference r : references) {
        Instance result = this;
        Object last = r.resolution.get(r.resolution.size() - 1);
        for (Object o : r.resolution) {
            result = ((Resolver) o).resolve(result);
            if (// Resolution is done and this is a global variable.
            o == last && r.variable.global) {
                if (result instanceof Part) {
                    // Need to locate our population.
                    InternalBackendData bed = (InternalBackendData) result.equations.backendData;
                    result = (Instance) result.container.valuesObject[bed.populationIndex];
                }
            } else {
                if (result instanceof Population) {
                    // Need to drill down to an instance of the population.
                    // This is only possible with a singleton.
                    InternalBackendData bed = (InternalBackendData) result.equations.backendData;
                    if (!bed.singleton) {
                        Backend.err.get().println("ERROR: Ambiguous reference to " + result.equations.prefix());
                        throw new Backend.AbortRun();
                    }
                    result = (Instance) result.valuesObject[bed.instances];
                }
            }
        }
        valuesObject[r.index] = result;
    }
}
Also used : VariableReference(gov.sandia.n2a.eqset.VariableReference) Part(gov.sandia.n2a.backend.internal.Part) Population(gov.sandia.n2a.backend.internal.Population) InternalBackendData(gov.sandia.n2a.backend.internal.InternalBackendData)

Aggregations

Population (gov.sandia.n2a.backend.internal.Population)4 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)2 InternalBackendData (gov.sandia.n2a.backend.internal.InternalBackendData)2 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)2 XyceRenderer (gov.sandia.n2a.backend.xyce.netlist.XyceRenderer)2 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)2 Variable (gov.sandia.n2a.eqset.Variable)2 AccessVariable (gov.sandia.n2a.language.AccessVariable)2 Operator (gov.sandia.n2a.language.Operator)2 Visitor (gov.sandia.n2a.language.Visitor)2 Output (gov.sandia.n2a.language.function.Output)2 Instance (gov.sandia.n2a.language.type.Instance)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Part (gov.sandia.n2a.backend.internal.Part)1 Wrapper (gov.sandia.n2a.backend.internal.Wrapper)1 MNode (gov.sandia.n2a.db.MNode)1 VariableReference (gov.sandia.n2a.eqset.VariableReference)1