Search in sources :

Example 11 with Variable

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

the class Part method integrate.

public void integrate(Simulator simulator) {
    InternalBackendData bed = (InternalBackendData) equations.backendData;
    int populations = equations.parts.size();
    // nothing to do
    if (bed.localIntegrated.isEmpty() && populations == 0)
        return;
    double dt;
    if (bed.lastT == null)
        dt = ((EventStep) simulator.currentEvent).dt;
    else
        dt = simulator.currentEvent.t - ((Scalar) get(bed.lastT)).value;
    // nothing to do
    if (dt <= 0)
        return;
    // Integrate variables
    for (Variable v : bed.localIntegrated) {
        double a = ((Scalar) get(v)).value;
        double aa = ((Scalar) get(v.derivative)).value;
        setFinal(v, new Scalar(a + aa * dt));
    }
    for (int i = 0; i < populations; i++) ((Population) valuesObject[i]).integrate(simulator, dt);
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 12 with Variable

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

the class Part method getP.

public double getP(Simulator simulator) {
    InstancePreLive temp = new InstancePreLive(this, simulator);
    // N2A language defines default to be 1 (always create)
    if (temp.bed.p == null)
        return 1;
    for (Variable v : temp.bed.Pdependencies) {
        Type result = v.eval(temp);
        if (result != null && v.writeIndex >= 0)
            temp.set(v, result);
    }
    Type result = temp.bed.p.eval(temp);
    if (result == null)
        return 1;
    return ((Scalar) result).value;
}
Also used : Type(gov.sandia.n2a.language.Type) Variable(gov.sandia.n2a.eqset.Variable) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 13 with Variable

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

the class Output method isStringExpression.

/**
 *        Determines if the given operator is a string expression.
 *        This is the case if any operator it depends on is a string.
 *        If an operand is a variable, then its equations must contain a string.
 */
public static boolean isStringExpression(Variable v, Operator op) {
    class StringVisitor extends Visitor {

        boolean foundString;

        Variable from;

        public StringVisitor(Variable from) {
            this.from = from;
            from.visited = null;
        }

        public boolean visit(Operator op) {
            if (op instanceof Constant) {
                Constant c = (Constant) op;
                if (c.value instanceof Text)
                    foundString = true;
                return false;
            }
            if (op instanceof AccessVariable) {
                AccessVariable av = (AccessVariable) op;
                Variable v = av.reference.variable;
                // Prevent infinite recursion
                Variable p = from;
                while (p != null) {
                    if (p == v)
                        return false;
                    p = p.visited;
                }
                v.visited = from;
                from = v;
                v.visit(this);
                from = v.visited;
                return false;
            }
            // no reason to dig any further
            if (foundString)
                return false;
            // Add is the only operator that can propagate string values. All other operators and functions return scalars or matrices.
            return op instanceof Add;
        }
    }
    StringVisitor visitor = new StringVisitor(v);
    op.visit(visitor);
    return visitor.foundString;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text)

Example 14 with Variable

use of gov.sandia.n2a.eqset.Variable 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 15 with Variable

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

the class JobC method findLiveReferences.

@SuppressWarnings("unchecked")
public void findLiveReferences(EquationSet s, ArrayList<Object> resolution, NavigableSet<EquationSet> touched, List<VariableReference> localReference, boolean terminate) {
    if (terminate) {
        Variable live = s.find(new Variable("$live"));
        if (live == null || live.hasAttribute("constant"))
            return;
        if (live.hasAttribute("initOnly")) {
            if (touched.add(s)) {
                VariableReference result = new VariableReference();
                result.variable = live;
                result.resolution = (ArrayList<Object>) resolution.clone();
                localReference.add(result);
                s.referenced = true;
            }
            return;
        }
    // The third possibility is "accessor", in which case we fall through ...
    }
    // Recurse up to container
    if (s.lethalContainer) {
        resolution.add(s.container);
        findLiveReferences(s.container, resolution, touched, localReference, true);
        resolution.remove(resolution.size() - 1);
    }
    // Recurse into connections
    if (s.lethalConnection) {
        for (ConnectionBinding c : s.connectionBindings) {
            resolution.add(c);
            findLiveReferences(c.endpoint, resolution, touched, localReference, true);
            resolution.remove(resolution.size() - 1);
        }
    }
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) VariableReference(gov.sandia.n2a.eqset.VariableReference) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)

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