Search in sources :

Example 16 with EquationSet

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

the class JobC method generateDeclarations.

/**
 *        Declares all classes, along with their member variables and functions.
 *
 *        For each part, generates two classes: one for the instances ("local")
 *        and one for the population as a whole ("global"). Within each class,
 *        declares buffer classes for integration and derivation, then member
 *        variables, and finally member functions as appropriate.
 */
public void generateDeclarations(EquationSet s, StringBuilder result) {
    for (EquationSet p : s.parts) generateDeclarations(p, result);
    BackendDataC bed = (BackendDataC) s.backendData;
    // Population class header
    result.append("class " + prefix(s) + "_Population : public Population\n");
    result.append("{\n");
    result.append("public:\n");
    // Population buffers
    if (bed.globalDerivative.size() > 0) {
        result.append("  class Derivative\n");
        result.append("  {\n");
        result.append("  public:\n");
        for (Variable v : bed.globalDerivative) {
            result.append("    " + type(v) + " " + mangle(v) + ";\n");
        }
        result.append("    Derivative * next;\n");
        result.append("  };\n");
        result.append("\n");
    }
    if (bed.needGlobalPreserve) {
        result.append("  class Preserve\n");
        result.append("  {\n");
        result.append("  public:\n");
        for (Variable v : bed.globalIntegrated) {
            result.append("    " + type(v) + " " + mangle(v) + ";\n");
        }
        for (Variable v : bed.globalDerivativePreserve) {
            result.append("    " + type(v) + " " + mangle(v) + ";\n");
        }
        for (Variable v : bed.globalBufferedExternalWriteDerivative) {
            result.append("    " + type(v) + " " + mangle("next_", v) + ";\n");
        }
        result.append("  };\n");
        result.append("\n");
    }
    // Population variables
    if (bed.n != null) {
        result.append("  int n;\n");
    }
    if (bed.index != null) {
        result.append("  int nextIndex;\n");
    }
    if (bed.globalDerivative.size() > 0) {
        result.append("  Derivative * stackDerivative;\n");
    }
    if (bed.needGlobalPreserve) {
        result.append("  Preserve * preserve;\n");
    }
    result.append("  " + prefix(s.container) + " * container;\n");
    for (Variable v : bed.globalMembers) {
        result.append("  " + type(v) + " " + mangle(v) + ";\n");
    }
    for (Variable v : bed.globalBufferedExternal) {
        result.append("  " + type(v) + " " + mangle("next_", v) + ";\n");
    }
    for (String columnName : bed.globalColumns) {
        result.append("  String " + columnName + ";\n");
    }
    result.append("\n");
    // Population functions
    if (bed.needGlobalCtor) {
        result.append("  " + prefix(s) + "_Population ();\n");
    }
    if (bed.needGlobalDtor) {
        result.append("  virtual ~" + prefix(s) + "_Population ();\n");
    }
    result.append("  virtual Part * create ();\n");
    if (bed.index != null || bed.trackInstances) {
        result.append("  virtual void add (Part * part);\n");
        if (bed.trackInstances) {
            result.append("  virtual void remove (Part * part);\n");
        }
    }
    result.append("  virtual void init ();\n");
    if (bed.globalIntegrated.size() > 0) {
        result.append("  virtual void integrate ();\n");
    }
    if (bed.globalUpdate.size() > 0) {
        result.append("  virtual void update ();\n");
    }
    if (bed.needGlobalFinalize) {
        result.append("  virtual bool finalize ();\n");
    }
    if (bed.n != null) {
        result.append("  virtual void resize (int n);\n");
    }
    if (bed.globalDerivativeUpdate.size() > 0) {
        result.append("  virtual void updateDerivative ();\n");
    }
    if (bed.globalBufferedExternalDerivative.size() > 0) {
        result.append("  virtual void finalizeDerivative ();\n");
    }
    if (bed.needGlobalPreserve) {
        result.append("  virtual void snapshot ();\n");
        result.append("  virtual void restore ();\n");
    }
    if (bed.globalDerivative.size() > 0) {
        result.append("  virtual void pushDerivative ();\n");
        result.append("  virtual void multiplyAddToStack (float scalar);\n");
        result.append("  virtual void multiply (float scalar);\n");
        result.append("  virtual void addToMembers ();\n");
    }
    if (s.connectionBindings != null) {
        result.append("  virtual Population * getTarget (int i);\n");
        if (bed.needK) {
            result.append("  virtual int getK (int i);\n");
        }
        if (bed.needMax) {
            result.append("  virtual int getMax (int i);\n");
        }
        if (bed.needMin) {
            result.append("  virtual int getMin (int i);\n");
        }
        if (bed.needRadius) {
            result.append("  virtual int getRadius (int i);\n");
        }
    }
    if (bed.needGlobalPath) {
        result.append("  virtual void path (String & result);\n");
    }
    // Population class trailer
    result.append("};\n");
    result.append("\n");
    // -------------------------------------------------------------------
    // Unit class
    result.append("class " + prefix(s) + " : public PartTime\n");
    result.append("{\n");
    result.append("public:\n");
    // Unit buffers
    if (bed.localDerivative.size() > 0) {
        result.append("  class Derivative\n");
        result.append("  {\n");
        result.append("  public:\n");
        for (Variable v : bed.localDerivative) {
            result.append("    " + type(v) + " " + mangle(v) + ";\n");
        }
        result.append("    Derivative * next;\n");
        result.append("  };\n");
        result.append("\n");
    }
    if (bed.needLocalPreserve) {
        result.append("  class Preserve\n");
        result.append("  {\n");
        result.append("  public:\n");
        for (Variable v : bed.localIntegrated) {
            result.append("    " + type(v) + " " + mangle(v) + ";\n");
        }
        for (Variable v : bed.localDerivativePreserve) {
            result.append("    " + type(v) + " " + mangle(v) + ";\n");
        }
        for (Variable v : bed.localBufferedExternalWriteDerivative) {
            result.append("    " + type(v) + " " + mangle("next_", v) + ";\n");
        }
        result.append("  };\n");
        result.append("\n");
    }
    // Unit variables
    if (bed.localDerivative.size() > 0) {
        result.append("  Derivative * stackDerivative;\n");
    }
    if (bed.needLocalPreserve) {
        result.append("  Preserve * preserve;\n");
    }
    if (bed.pathToContainer == null) {
        result.append("  " + prefix(s.container) + " * container;\n");
    }
    if (s.connectionBindings != null) {
        for (ConnectionBinding c : s.connectionBindings) {
            // we should be able to assume that s.container is non-null; ie: a connection should always operate in some larger container
            result.append("  " + prefix(c.endpoint) + " * " + mangle(c.alias) + ";\n");
        }
    }
    if (s.accountableConnections != null) {
        for (EquationSet.AccountableConnection ac : s.accountableConnections) {
            result.append("  int " + prefix(ac.connection) + "_" + mangle(ac.alias) + "_count;\n");
        }
    }
    if (bed.refcount) {
        result.append("  int refcount;\n");
    }
    if (bed.index != null) {
        result.append("  int __24index;\n");
    }
    if (bed.lastT) {
        // $lastT is for internal use only, so no need for __24 prefix.
        result.append("  float lastT;\n");
    }
    for (Variable v : bed.localMembers) {
        result.append("  " + type(v) + " " + mangle(v) + ";\n");
    }
    for (Variable v : bed.localBufferedExternal) {
        result.append("  " + type(v) + " " + mangle("next_", v) + ";\n");
    }
    for (EquationSet p : s.parts) {
        result.append("  " + prefix(p) + "_Population " + mangle(p.name) + ";\n");
    }
    for (String columnName : bed.localColumns) {
        result.append("  String " + columnName + ";\n");
    }
    for (EventSource es : bed.eventSources) {
        result.append("  std::vector<Part *> " + "eventMonitor_" + prefix(es.target.container) + ";\n");
    }
    for (EventTarget et : bed.eventTargets) {
        if (et.track != null && et.track.name.startsWith("eventAux")) {
            result.append("  float " + et.track.name + ";\n");
        }
        if (et.timeIndex >= 0) {
            result.append("  float eventTime" + et.timeIndex + ";\n");
        }
    }
    if (!bed.flagType.isEmpty()) {
        // This should come last, because it can affect alignment.
        result.append("  " + bed.flagType + " flags;\n");
    }
    result.append("\n");
    // Unit functions
    if (bed.needLocalCtor || s.parts.size() > 0) {
        result.append("  " + prefix(s) + " ();\n");
    }
    if (bed.needLocalDtor) {
        result.append("  virtual ~" + prefix(s) + " ();\n");
    }
    if (bed.localMembers.size() > 0) {
        result.append("  virtual void clear ();\n");
    }
    if (s.container == null) {
        result.append("  virtual void setPeriod (float dt);\n");
    }
    if (bed.needLocalDie) {
        result.append("  virtual void die ();\n");
    }
    if (bed.localReference.size() > 0) {
        result.append("  virtual void enterSimulation ();\n");
    }
    result.append("  virtual void leaveSimulation ();\n");
    if (bed.refcount) {
        result.append("  virtual bool isFree ();\n");
    }
    if (bed.needLocalInit || s.parts.size() > 0) {
        result.append("  virtual void init ();\n");
    }
    if (bed.localIntegrated.size() > 0 || s.parts.size() > 0) {
        result.append("  virtual void integrate ();\n");
    }
    if (bed.localUpdate.size() > 0 || s.parts.size() > 0) {
        result.append("  virtual void update ();\n");
    }
    if (bed.needLocalFinalize || s.parts.size() > 0) {
        result.append("  virtual bool finalize ();\n");
    }
    if (bed.localDerivativeUpdate.size() > 0 || s.parts.size() > 0) {
        result.append("  virtual void updateDerivative ();\n");
    }
    if (bed.localBufferedExternalDerivative.size() > 0 || s.parts.size() > 0) {
        result.append("  virtual void finalizeDerivative ();\n");
    }
    if (bed.needLocalPreserve || s.parts.size() > 0) {
        result.append("  virtual void snapshot ();\n");
        result.append("  virtual void restore ();\n");
    }
    if (bed.localDerivative.size() > 0 || s.parts.size() > 0) {
        result.append("  virtual void pushDerivative ();\n");
        result.append("  virtual void multiplyAddToStack (float scalar);\n");
        result.append("  virtual void multiply (float scalar);\n");
        result.append("  virtual void addToMembers ();\n");
    }
    if (bed.live != null && !bed.live.hasAttribute("constant")) {
        result.append("  virtual float getLive ();\n");
    }
    if (s.connectionBindings == null) {
        if (bed.xyz != null) {
            result.append("  virtual void getXYZ (Vector3 & xyz);\n");
        }
    } else {
        if (bed.p != null) {
            result.append("  virtual float getP ();\n");
        }
        if (bed.hasProjectFrom || bed.hasProjectTo) {
            result.append("  virtual void project (int i, int j, Vector3 & xyz);\n");
        }
        result.append("  virtual void setPart (int i, Part * part);\n");
        result.append("  virtual Part * getPart (int i);\n");
    }
    if (bed.eventTargets.size() > 0) {
        result.append("  virtual bool eventTest (int i);\n");
        if (bed.needLocalEventDelay) {
            result.append("  virtual float eventDelay (int i);\n");
        }
        result.append("  virtual void setLatch (int i);\n");
        if (bed.eventReferences.size() > 0) {
            result.append("  virtual void finalizeEvent ();\n");
        }
    }
    if (bed.accountableEndpoints.size() > 0) {
        result.append("  virtual int getCount (int i);\n");
    }
    if (bed.needLocalPath) {
        result.append("  virtual void path (String & result);\n");
    }
    // Conversions
    Set<Conversion> conversions = s.getConversions();
    for (Conversion pair : conversions) {
        EquationSet source = pair.from;
        EquationSet dest = pair.to;
        result.append("  void " + mangle(source.name) + "_2_" + mangle(dest.name) + " (" + mangle(source.name) + " * from, int " + mangle("$type") + ");\n");
    }
    // Unit class trailer
    result.append("};\n");
    result.append("\n");
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet) EventSource(gov.sandia.n2a.backend.internal.InternalBackendData.EventSource) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) Conversion(gov.sandia.n2a.eqset.EquationSet.Conversion) EventTarget(gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)

Example 17 with EquationSet

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

the class JobC method analyze.

public void analyze(EquationSet s) {
    BackendDataC bed = (BackendDataC) s.backendData;
    bed.analyze(s);
    for (EquationSet p : s.parts) analyze(p);
    bed.analyzeLastT(s);
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet)

Example 18 with EquationSet

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

the class InternalBackend method analyzeEvents.

public static void analyzeEvents(EquationSet s) throws Backend.AbortRun {
    InternalBackendData bed = (InternalBackendData) s.backendData;
    bed.analyzeEvents(s);
    for (EquationSet p : s.parts) analyzeEvents(p);
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet)

Example 19 with EquationSet

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

the class InternalBackend method createBackendData.

public static void createBackendData(EquationSet s) {
    Object o = s.backendData;
    if (!(o instanceof InternalBackendData)) {
        InternalBackendData bed = new InternalBackendData(s);
        s.backendData = bed;
        // Chain backend data, rather than simply blowing it away. This works even if o is null.
        bed.backendData = o;
    }
    for (EquationSet p : s.parts) createBackendData(p);
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet)

Example 20 with EquationSet

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

the class InternalBackendData method analyzeEvents.

/**
 *        Find event() calls and collate them (in case the same signature appears several different places
 *        in the equation set).
 *        This must be done before the variables are sorted into sets according to attributes, because we
 *        may need to add the "externalRead" attribute to some of them.
 */
public void analyzeEvents(EquationSet s) {
    analyzeEvents(s, eventTargets, eventReferences);
    // Allocate storage
    int valueIndex = -1;
    int mask = 0;
    int eventIndex = 0;
    for (EventTarget et : eventTargets) {
        if (valueIndex == -1) {
            valueIndex = countLocalFloat++;
            namesLocalFloat.add("eventLatch" + valueIndex);
            eventLatches.add(valueIndex);
            mask = 1;
        }
        et.valueIndex = valueIndex;
        et.mask = mask;
        mask <<= 1;
        // Due to limitations of float-int conversion, only 23 bits are available. Allocate another float.
        if (mask > 0x400000)
            valueIndex = -1;
        if (et.track != null && et.track.name.startsWith("eventAux")) {
            et.track.readIndex = et.track.writeIndex = countLocalFloat++;
            namesLocalFloat.add(et.track.name);
        }
        // Force multiple sources to generate only one event in a given cycle
        if (et.sources.size() > 1 && et.edge == EventTarget.NONZERO) {
            et.timeIndex = countLocalFloat++;
            namesLocalFloat.add("eventTime" + eventIndex);
        }
        // TODO: What if two different event targets in this part reference the same source part? What if the condition is different? The same?
        for (EventSource es : et.sources) {
            EquationSet sourceContainer = es.container;
            InternalBackendData sourceBed = (InternalBackendData) sourceContainer.backendData;
            es.monitorIndex = sourceBed.countLocalObject++;
            // TODO: Consolidate monitors that share the same trigger condition.
            sourceBed.namesLocalObject.add("eventMonitor_" + s.prefix());
            sourceBed.eventSources.add(es);
        }
        eventIndex++;
    }
}
Also used : EquationSet(gov.sandia.n2a.eqset.EquationSet)

Aggregations

EquationSet (gov.sandia.n2a.eqset.EquationSet)34 Variable (gov.sandia.n2a.eqset.Variable)10 AccessVariable (gov.sandia.n2a.language.AccessVariable)9 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)7 ArrayList (java.util.ArrayList)7 Operator (gov.sandia.n2a.language.Operator)6 Scalar (gov.sandia.n2a.language.type.Scalar)6 MPart (gov.sandia.n2a.eqset.MPart)5 VariableReference (gov.sandia.n2a.eqset.VariableReference)4 Constant (gov.sandia.n2a.language.Constant)4 Type (gov.sandia.n2a.language.Type)4 Visitor (gov.sandia.n2a.language.Visitor)4 TreeSet (java.util.TreeSet)4 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)3 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)3 MNode (gov.sandia.n2a.db.MNode)3 ParseException (gov.sandia.n2a.language.ParseException)3 Text (gov.sandia.n2a.language.type.Text)3 AbortRun (gov.sandia.n2a.plugins.extpoints.Backend.AbortRun)3 FileNotFoundException (java.io.FileNotFoundException)3