Search in sources :

Example 51 with Variable

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

the class JobC method addImplicitDependencies.

/**
 *        Depends on the results of: addAttributes(), findDeath()
 */
public void addImplicitDependencies(EquationSet s) {
    if (T.equals("int")) {
        // Force top-level model to keep $t', so it can retrieve time exponent.
        Variable dt = s.find(new Variable("$t", 1));
        dt.addUser(s);
    }
    addImplicitDependenciesRecursive(s);
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable)

Example 52 with Variable

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

the class JobC method generateCode.

public void generateCode(Path source) throws Exception {
    StringBuilder result = new StringBuilder();
    RendererC context;
    if (T.equals("int"))
        context = new RendererCfp(this, result);
    else
        context = new RendererC(this, result);
    result.append("#include \"runtime.h\"\n");
    if (kokkos) {
        result.append("#include \"profiling.h\"\n");
    }
    result.append("#include \"Matrix.tcc\"\n");
    result.append("#include \"MatrixFixed.tcc\"\n");
    if (T.equals("int")) {
        result.append("#include \"fixedpoint.tcc\"\n");
    }
    for (ProvideOperator po : extensions) {
        Path include = po.include(this);
        if (include == null)
            continue;
        result.append("#include <" + include.getFileName() + ">\n");
    }
    result.append("\n");
    result.append("#include <iostream>\n");
    result.append("#include <vector>\n");
    result.append("#include <cmath>\n");
    result.append("#include <csignal>\n");
    result.append("\n");
    result.append("using namespace std;\n");
    result.append("using namespace fl;\n");
    result.append("\n");
    if (cli) {
        result.append("Parameters<" + T + "> params;\n");
    }
    generateStatic(context, digestedModel);
    result.append("\n");
    generateClassList(digestedModel, result);
    result.append("class Wrapper;\n");
    result.append("\n");
    generateDeclarations(digestedModel, result);
    result.append("class Wrapper : public WrapperBase<" + T + ">\n");
    result.append("{\n");
    result.append("public:\n");
    result.append("  " + prefix(digestedModel) + "_Population " + mangle(digestedModel.name) + ";\n");
    result.append("\n");
    result.append("  Wrapper ()\n");
    result.append("  {\n");
    result.append("    population = &" + mangle(digestedModel.name) + ";\n");
    result.append("    " + mangle(digestedModel.name) + ".container = this;\n");
    result.append("  }\n");
    result.append("};\n");
    result.append("\n");
    generateDefinitions(context, digestedModel);
    // Main
    result.append("int main (int argc, char * argv[])\n");
    result.append("{\n");
    result.append("  signal (SIGFPE,  signalHandler);\n");
    result.append("  signal (SIGINT,  signalHandler);\n");
    result.append("  signal (SIGTERM, signalHandler);\n");
    result.append("\n");
    result.append("  try\n");
    result.append("  {\n");
    if (kokkos) {
        result.append("    get_callbacks ();\n");
    }
    if (cli) {
        result.append("    params.parse (argc, argv);\n");
    }
    generateMainInitializers(context);
    result.append("\n");
    if (seed >= 0) {
        result.append("    srand (" + seed + ");\n");
    }
    if (T.equals("int")) {
        Variable dt = digestedModel.find(new Variable("$t", 1));
        result.append("    Event<int>::exponent = " + dt.exponent + ";\n");
    }
    String integrator = digestedModel.metadata.getOrDefault("Euler", "backend", "all", "integrator");
    if (integrator.equalsIgnoreCase("RungeKutta"))
        integrator = "RungeKutta";
    else
        integrator = "Euler";
    result.append("    Simulator<" + T + ">::instance.integrator = new " + integrator + "<" + T + ">;\n");
    result.append("    Simulator<" + T + ">::instance.after = " + after + ";\n");
    result.append("    Wrapper wrapper;\n");
    result.append("    Simulator<" + T + ">::instance.run (wrapper);\n");
    result.append("\n");
    result.append("    outputClose ();\n");
    if (kokkos) {
        result.append("    finalize_profiling ();\n");
    }
    result.append("  }\n");
    result.append("  catch (const char * message)\n");
    result.append("  {\n");
    result.append("    cerr << \"Exception: \" << message << endl;\n");
    result.append("    return 1;\n");
    result.append("  }\n");
    result.append("  return 0;\n");
    result.append("}\n");
    Files.copy(new ByteArrayInputStream(result.toString().getBytes("UTF-8")), source);
}
Also used : Path(java.nio.file.Path) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 53 with Variable

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

the class RendererC method moduloParam.

public void moduloParam(Operator a) {
    if (a.isScalar()) {
        double d = a.getDouble();
        result.append(String.valueOf(d));
        if (job.T.equals("float"))
            result.append("f");
        return;
    }
    if (a instanceof AccessVariable) {
        // Trap variables that are declared int
        Variable v = ((AccessVariable) a).reference.variable;
        if (v.name.equals("$index") || v.name.equals("$n") && v.hasAttribute("initOnly")) {
            result.append("(" + job.T + ") ");
        }
        a.render(this);
        return;
    }
    // The general case
    result.append("(" + job.T + ") (");
    a.render(this);
    result.append(")");
}
Also used : AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable)

Example 54 with Variable

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

the class EventSpikeMulti method run.

public void run(Simulator simulator) {
    setFlag();
    for (Instance i : targets) simulator.integrate(i);
    for (Instance i : targets) i.update(simulator);
    for (Instance i : targets) {
        boolean live = i.finish(simulator);
        InternalBackendData bed = (InternalBackendData) i.equations.backendData;
        for (Variable v : bed.eventReferences) ((Instance) i.valuesObject[v.reference.index]).finishEvent(v.reference.variable);
        if (!live)
            i.dequeue();
    }
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) Instance(gov.sandia.n2a.language.type.Instance)

Example 55 with Variable

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

the class EventSpikeSingle method run.

public void run(Simulator simulator) {
    setFlag();
    simulator.integrate(target);
    target.update(simulator);
    boolean live = target.finish(simulator);
    InternalBackendData bed = (InternalBackendData) target.equations.backendData;
    for (Variable v : bed.eventReferences) ((Instance) target.valuesObject[v.reference.index]).finishEvent(v.reference.variable);
    if (!live)
        target.dequeue();
}
Also used : Variable(gov.sandia.n2a.eqset.Variable)

Aggregations

Variable (gov.sandia.n2a.eqset.Variable)63 AccessVariable (gov.sandia.n2a.language.AccessVariable)38 EquationSet (gov.sandia.n2a.eqset.EquationSet)25 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)20 Scalar (gov.sandia.n2a.language.type.Scalar)18 Operator (gov.sandia.n2a.language.Operator)17 ArrayList (java.util.ArrayList)17 Type (gov.sandia.n2a.language.Type)12 Visitor (gov.sandia.n2a.language.Visitor)12 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)11 MNode (gov.sandia.n2a.db.MNode)9 VariableReference (gov.sandia.n2a.eqset.VariableReference)9 Constant (gov.sandia.n2a.language.Constant)9 MPart (gov.sandia.n2a.eqset.MPart)7 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)6 Output (gov.sandia.n2a.language.function.Output)6 FilteredTreeModel (gov.sandia.n2a.ui.eq.FilteredTreeModel)6 List (java.util.List)6 IncommensurableException (javax.measure.IncommensurableException)6 UnconvertibleException (javax.measure.UnconvertibleException)6