use of gov.sandia.n2a.language.AccessElement in project n2a by frothga.
the class JobC method generateDefinitionsGlobal.
public void generateDefinitionsGlobal(RendererC context) throws Exception {
EquationSet s = context.part;
BackendDataC bed = context.bed;
StringBuilder result = context.result;
context.global = true;
String ps = prefix(s);
// namespace for all functions associated with part s
String ns = ps + "_Population::";
// Population ctor
if (bed.needGlobalCtor) {
result.append(ns + ps + "_Population ()\n");
result.append("{\n");
if (!bed.singleton) {
if (// and not singleton, so trackN is true
bed.n != null) {
result.append(" n = 0;\n");
}
if (!bed.trackInstances && bed.index != null) {
result.append(" nextIndex = 0;\n");
}
if (bed.newborn >= 0) {
result.append(" firstborn = 0;\n");
}
}
if (bed.needGlobalDerivative) {
result.append(" stackDerivative = 0;\n");
}
if (bed.needGlobalPreserve) {
result.append(" preserve = 0;\n");
}
result.append("}\n");
result.append("\n");
}
// Population dtor
if (bed.needGlobalDtor) {
result.append(ns + "~" + ps + "_Population ()\n");
result.append("{\n");
if (bed.needGlobalDerivative) {
result.append(" while (stackDerivative)\n");
result.append(" {\n");
result.append(" Derivative * temp = stackDerivative;\n");
result.append(" stackDerivative = stackDerivative->next;\n");
result.append(" delete temp;\n");
result.append(" }\n");
}
if (bed.needGlobalPreserve) {
result.append(" if (preserve) delete preserve;\n");
}
result.append("}\n");
result.append("\n");
}
// Population create
if (// In the case of a singleton, this will remain a pure virtual function, and throw an exception if called.
!bed.singleton) {
result.append("Part<" + T + "> * " + ns + "create ()\n");
result.append("{\n");
result.append(" " + ps + " * p = new " + ps + ";\n");
if (bed.pathToContainer == null)
result.append(" p->container = (" + prefix(s.container) + " *) container;\n");
result.append(" return p;\n");
result.append("}\n");
result.append("\n");
}
// Population add / remove
if (bed.index != null && !bed.singleton) {
result.append("void " + ns + "add (Part<" + T + "> * part)\n");
result.append("{\n");
result.append(" " + ps + " * p = (" + ps + " *) part;\n");
if (bed.trackInstances) {
result.append(" if (p->__24index < 0)\n");
result.append(" {\n");
result.append(" p->__24index = instances.size ();\n");
result.append(" instances.push_back (p);\n");
result.append(" }\n");
result.append(" else\n");
result.append(" {\n");
result.append(" instances[p->__24index] = p;\n");
result.append(" }\n");
if (bed.newborn >= 0) {
result.append(" p->flags = (" + bed.localFlagType + ") 0x1 << " + bed.newborn + ";\n");
result.append(" firstborn = min (firstborn, p->__24index);\n");
}
} else {
result.append(" if (p->__24index < 0) p->__24index = nextIndex++;\n");
}
result.append("}\n");
result.append("\n");
if (bed.trackInstances) {
result.append("void " + ns + "remove (Part<" + T + "> * part)\n");
result.append("{\n");
result.append(" " + ps + " * p = (" + ps + " *) part;\n");
result.append(" instances[p->__24index] = 0;\n");
result.append(" Population<" + T + ">::remove (part);\n");
result.append("}\n");
result.append("\n");
}
}
// Population init
if (bed.needGlobalInit) {
result.append("void " + ns + "init ()\n");
result.append("{\n");
s.setInit(1);
// Zero out members
for (Variable v : bed.globalMembers) {
result.append(" " + zero(mangle(v), v) + ";\n");
}
for (Variable v : bed.globalBufferedExternal) {
result.append(" " + clearAccumulator(mangle("next_", v), v, context) + ";\n");
result.append(" " + clearAccumulator(mangle(v), v, context) + ";\n");
}
if (!bed.globalFlagType.isEmpty()) {
result.append(" flags = 0;\n");
}
// Compute variables
if (// $n is not stored, so we need to declare a local variable to receive its value.
bed.nInitOnly) {
result.append(" " + type(bed.n) + " " + mangle(bed.n) + ";\n");
}
s.simplify("$init", bed.globalInit);
if (T.equals("int"))
EquationSet.determineExponentsSimplified(bed.globalInit);
EquationSet.determineOrderInit(bed.globalInit);
for (Variable v : bed.globalInit) {
multiconditional(v, context, " ");
}
// create instances
if (bed.singleton) {
if (bed.newborn >= 0) {
result.append(" instance.flags = (" + bed.localFlagType + ") 0x1 << " + bed.newborn + ";\n");
}
result.append(" instance.enterSimulation ();\n");
result.append(" container->getEvent ()->enqueue (&instance);\n");
result.append(" instance.init ();\n");
} else {
if (// and not singleton, so trackN is true
bed.n != null) {
result.append(" resize (" + resolve(bed.n.reference, context, bed.nInitOnly));
if (context.useExponent)
result.append(context.printShift(bed.n.exponent - Operator.MSB));
result.append(");\n");
}
}
// make connections
if (s.connectionBindings != null) {
// queue to evaluate our connections
result.append(" Simulator<" + T + ">::instance.connect (this);\n");
}
s.setInit(0);
result.append("}\n");
result.append("\n");
}
// Population integrate
if (bed.needGlobalIntegrate) {
result.append("void " + ns + "integrate ()\n");
result.append("{\n");
push_region(result, ns + "integrate()");
result.append(" EventStep<" + T + "> * event = getEvent ();\n");
context.hasEvent = true;
result.append(" " + T + " dt = event->dt;\n");
result.append(" if (preserve)\n");
result.append(" {\n");
for (Variable v : bed.globalIntegrated) {
result.append(" " + resolve(v.reference, context, false) + " = preserve->" + mangle(v) + " + ");
// For fixed-point:
// raw result = exponentDerivative+exponentTime-MSB
// shift = raw-exponentVariable = exponentDerivative+exponentTime-MSB-exponentVariable
int shift = v.derivative.exponent + bed.dt.exponent - Operator.MSB - v.exponent;
if (shift != 0 && T.equals("int")) {
result.append("(int) ((int64_t) " + resolve(v.derivative.reference, context, false) + " * dt" + context.printShift(shift) + ");\n");
} else {
result.append(resolve(v.derivative.reference, context, false) + " * dt;\n");
}
}
result.append(" }\n");
result.append(" else\n");
result.append(" {\n");
for (Variable v : bed.globalIntegrated) {
result.append(" " + resolve(v.reference, context, false) + " += ");
int shift = v.derivative.exponent + bed.dt.exponent - Operator.MSB - v.exponent;
if (shift != 0 && T.equals("int")) {
result.append("(int) ((int64_t) " + resolve(v.derivative.reference, context, false) + " * dt" + context.printShift(shift) + ");\n");
} else {
result.append(resolve(v.derivative.reference, context, false) + " * dt;\n");
}
}
result.append(" }\n");
context.hasEvent = false;
pop_region(result);
result.append("}\n");
result.append("\n");
}
// Population update
if (bed.needGlobalUpdate) {
result.append("void " + ns + "update ()\n");
result.append("{\n");
push_region(result, ns + "update()");
for (Variable v : bed.globalBufferedInternalUpdate) {
result.append(" " + type(v) + " " + mangle("next_", v) + ";\n");
}
s.simplify("$live", bed.globalUpdate);
if (T.equals("int"))
EquationSet.determineExponentsSimplified(bed.globalUpdate);
for (Variable v : bed.globalUpdate) {
multiconditional(v, context, " ");
}
for (Variable v : bed.globalBufferedInternalUpdate) {
result.append(" " + mangle(v) + " = " + mangle("next_", v) + ";\n");
}
pop_region(result);
result.append("}\n");
result.append("\n");
}
// Population finalize
if (bed.needGlobalFinalize) {
result.append("bool " + ns + "finalize ()\n");
result.append("{\n");
if (// $n shares control with other specials, so must coordinate with them
bed.canResize && bed.n.derivative == null && bed.canGrowOrDie) {
// $n may be assigned during the regular update cycle, so we need to monitor it.
result.append(" if (" + mangle("$n") + " != " + mangle("next_", "$n") + ") Simulator<" + T + ">::instance.resize (this, max (0, " + mangle("next_", "$n"));
if (context.useExponent)
result.append(context.printShift(bed.n.exponent - Operator.MSB));
result.append("));\n");
result.append(" else Simulator<" + T + ">::instance.resize (this, -1);\n");
}
for (Variable v : bed.globalBufferedExternal) {
result.append(" " + mangle(v) + " = " + mangle("next_", v) + ";\n");
}
for (Variable v : bed.globalBufferedExternalWrite) {
result.append(" " + clearAccumulator(mangle("next_", v), v, context) + ";\n");
}
// Return value is generally ignored, except for top-level population.
boolean returnN = bed.needGlobalFinalizeN;
if (bed.canResize) {
if (bed.canGrowOrDie) {
if (// $n' exists
bed.n.derivative != null) {
// the rate of change in $n is pre-determined, so it relentlessly overrides any other structural dynamics
if (returnN) {
result.append(" if (n == 0) return false;\n");
returnN = false;
}
result.append(" Simulator<" + T + ">::instance.resize (this, max (0, " + mangle("$n"));
if (context.useExponent)
result.append(context.printShift(bed.n.exponent - Operator.MSB));
result.append("));\n");
}
} else // $n is the only kind of structural dynamics, so simply do a resize() when needed
{
if (returnN) {
result.append(" if (n == 0) return false;\n");
returnN = false;
}
result.append(" int floorN = max (0, ");
if (context.useExponent)
result.append(mangle("$n") + context.printShift(bed.n.exponent - Operator.MSB));
else
result.append("(int) " + mangle("$n"));
result.append(");\n");
result.append(" if (n != floorN) Simulator<" + T + ">::instance.resize (this, floorN);\n");
}
}
if (returnN) {
result.append(" return n;\n");
} else {
result.append(" return true;\n");
}
result.append("}\n");
result.append("\n");
}
// Population resize()
if (bed.canResize) {
result.append("void " + ns + "resize (int n)\n");
result.append("{\n");
if (bed.canGrowOrDie && bed.n.derivative == null) {
result.append(" if (n < 0)\n");
result.append(" {\n");
result.append(" " + mangle("$n") + " = this->n;\n");
result.append(" return;\n");
result.append(" }\n");
result.append("\n");
}
result.append(" Population<" + T + ">::resize (n);\n");
result.append("\n");
result.append(" for (int i = instances.size () - 1; this->n > n && i >= 0; i--)\n");
result.append(" {\n");
result.append(" Part * p = instances[i];\n");
result.append(" if (p && p->getLive ()) p->die ();\n");
result.append(" }\n");
result.append("}\n");
result.append("\n");
}
// Population getN
if (bed.trackN) {
result.append("int " + ns + "getN ()\n");
result.append("{\n");
result.append(" return n;\n");
result.append("}\n");
result.append("\n");
}
// Population updateDerivative
if (bed.needGlobalUpdateDerivative) {
result.append("void " + ns + "updateDerivative ()\n");
result.append("{\n");
push_region(result, ns + "updateDerivative()");
for (Variable v : bed.globalBufferedInternalDerivative) {
result.append(" " + type(v) + " " + mangle("next_", v) + ";\n");
}
// This is unlikely to make any difference. Just being thorough before call to multiconditional().
s.simplify("$live", bed.globalDerivativeUpdate);
if (T.equals("int"))
EquationSet.determineExponentsSimplified(bed.globalDerivativeUpdate);
for (Variable v : bed.globalDerivativeUpdate) {
multiconditional(v, context, " ");
}
for (Variable v : bed.globalBufferedInternalDerivative) {
result.append(" " + mangle(v) + " = " + mangle("next_", v) + ";\n");
}
pop_region(result);
result.append("}\n");
result.append("\n");
}
// Population finalizeDerivative
if (bed.needGlobalFinalizeDerivative) {
result.append("void " + ns + "finalizeDerivative ()\n");
result.append("{\n");
for (Variable v : bed.globalBufferedExternalDerivative) {
result.append(" " + mangle(v) + " = " + mangle("next_", v) + ";\n");
}
for (Variable v : bed.globalBufferedExternalWriteDerivative) {
result.append(" " + clearAccumulator(mangle("next_", v), v, context) + ";\n");
}
result.append("}\n");
result.append("\n");
}
if (bed.needGlobalPreserve) {
// Population snapshot
result.append("void " + ns + "snapshot ()\n");
result.append("{\n");
result.append(" preserve = new Preserve;\n");
for (Variable v : bed.globalIntegrated) {
result.append(" preserve->" + mangle(v) + " = " + mangle(v) + ";\n");
}
for (Variable v : bed.globalDerivativePreserve) {
result.append(" preserve->" + mangle(v) + " = " + mangle(v) + ";\n");
}
for (Variable v : bed.globalBufferedExternalWriteDerivative) {
result.append(" preserve->" + mangle("next_", v) + " = " + mangle("next_", v) + ";\n");
result.append(" " + clearAccumulator(mangle("next_", v), v, context) + ";\n");
}
result.append("}\n");
result.append("\n");
// Population restore
result.append("void " + ns + "restore ()\n");
result.append("{\n");
for (Variable v : bed.globalDerivativePreserve) {
result.append(" " + mangle(v) + " = preserve->" + mangle(v) + ";\n");
}
for (Variable v : bed.globalBufferedExternalWriteDerivative) {
result.append(" " + mangle("next_", v) + " = preserve->" + mangle("next_", v) + ";\n");
}
result.append(" delete preserve;\n");
result.append(" preserve = 0;\n");
result.append("}\n");
result.append("\n");
}
if (bed.needGlobalDerivative) {
// Population pushDerivative
result.append("void " + ns + "pushDerivative ()\n");
result.append("{\n");
result.append(" Derivative * temp = new Derivative;\n");
result.append(" temp->_next = stackDerivative;\n");
result.append(" stackDerivative = temp;\n");
for (Variable v : bed.globalDerivative) {
result.append(" temp->" + mangle(v) + " = " + mangle(v) + ";\n");
}
result.append("}\n");
result.append("\n");
// Population multiplyAddToStack
result.append("void " + ns + "multiplyAddToStack (" + T + " scalar)\n");
result.append("{\n");
for (Variable v : bed.globalDerivative) {
result.append(" stackDerivative->" + mangle(v) + " += ");
if (T.equals("int")) {
result.append("(int) ((int64_t) " + mangle(v) + " * scalar >> " + (Operator.MSB - 1) + ");\n");
} else {
result.append(mangle(v) + " * scalar;\n");
}
}
result.append("}\n");
result.append("\n");
// Population multiply
result.append("void " + ns + "multiply (" + T + " scalar)\n");
result.append("{\n");
for (Variable v : bed.globalDerivative) {
if (T.equals("int")) {
result.append(" " + mangle(v) + " = (int64_t) " + mangle(v) + " * scalar >> " + (Operator.MSB - 1) + ";\n");
} else {
result.append(" " + mangle(v) + " *= scalar;\n");
}
}
result.append("}\n");
result.append("\n");
// Population addToMembers
result.append("void " + ns + "addToMembers ()\n");
result.append("{\n");
for (Variable v : bed.globalDerivative) {
result.append(" " + mangle(v) + " += stackDerivative->" + mangle(v) + ";\n");
}
result.append(" Derivative * temp = stackDerivative;\n");
result.append(" stackDerivative = stackDerivative->next;\n");
result.append(" delete temp;\n");
result.append("}\n");
result.append("\n");
}
// Population clearNew
if (bed.newborn >= 0) {
result.append("void " + ns + "clearNew ()\n");
result.append("{\n");
// Reset our clearNew flag
result.append(" flags &= ~((" + bed.globalFlagType + ") 0x1 << " + bed.clearNew + ");\n");
if (bed.singleton) {
result.append(" instance.flags &= ~((" + bed.localFlagType + ") 0x1 << " + bed.newborn + ");\n");
} else {
result.append(" int count = instances.size ();\n");
result.append(" for (int i = firstborn; i < count; i++)\n");
result.append(" {\n");
result.append(" " + ps + " * p = instances[i];\n");
result.append(" if (p) p->flags &= ~((" + bed.localFlagType + ") 0x1 << " + bed.newborn + ");\n");
result.append(" }\n");
result.append(" firstborn = count;\n");
}
result.append("}\n");
result.append("\n");
}
// Population getIterators
if (s.connectionBindings != null) {
class ConnectionHolder {
public Operator k;
public Operator min;
public Operator max;
public Operator radius;
public boolean hasProject;
public EquationSet endpoint;
public List<Integer> indices = new ArrayList<Integer>();
public List<Object> resolution;
public boolean equivalent(Operator a, Operator b) {
if (a == b)
return true;
if (a == null || b == null)
return false;
return a.equals(b);
}
public boolean equals(Object o) {
// This is a safe assumption, since this is a local class.
ConnectionHolder that = (ConnectionHolder) o;
return equivalent(k, that.k) && equivalent(min, that.min) && equivalent(max, that.max) && equivalent(radius, that.radius) && hasProject == that.hasProject && endpoint == that.endpoint;
}
public void emit() {
for (Integer index : indices) {
result.append(" case " + index + ":\n");
}
result.append(" {\n");
if (k == null && radius == null) {
result.append(" result = new ConnectPopulation<" + T + "> (i);\n");
} else {
// Pulls in KDTree dependencies, for full NN support.
result.append(" result = new ConnectPopulationNN<" + T + "> (i);\n");
}
boolean testK = false;
boolean testRadius = false;
boolean constantKR = false;
if (k != null) {
result.append(" result->k = ");
k.render(context);
result.append(";\n");
testK = true;
if (k instanceof Constant) {
Constant c = (Constant) k;
if (c.value instanceof Scalar && ((Scalar) c.value).value != 0)
constantKR = true;
}
}
if (max != null) {
result.append(" result->Max = ");
max.render(context);
result.append(";\n");
}
if (min != null) {
result.append(" result->Min = ");
min.render(context);
result.append(";\n");
}
if (radius != null) {
result.append(" result->radius = ");
radius.render(context);
result.append(";\n");
testRadius = true;
if (radius instanceof Constant) {
Constant c = (Constant) radius;
if (c.value instanceof Scalar && ((Scalar) c.value).value != 0)
constantKR = true;
}
}
if (hasProject) {
result.append(" result->rank += 1;");
}
if (constantKR) {
result.append(" result->rank -= 2;\n");
} else {
if (testK && testRadius) {
result.append(" if (result->k > 0 || result->radius > 0) result->rank -= 2;\n");
} else if (testK) {
result.append(" if (result->k > 0) result->rank -= 2;\n");
} else if (testRadius) {
result.append(" if (result->radius > 0) result->rank -= 2;\n");
}
}
assembleInstances(s, "", resolution, 0, " ", result);
result.append(" result->size = result->instances->size ();\n");
result.append(" break;\n");
result.append(" }\n");
}
}
List<ConnectionHolder> connections = new ArrayList<ConnectionHolder>();
// TODO: Should determine this across the entire simulation, so that only one of getIteratorsSimple() or getIteratorsNN() is linked.
boolean needNN = false;
for (ConnectionBinding c : s.connectionBindings) {
ConnectionHolder h = new ConnectionHolder();
Variable v = s.find(new Variable(c.alias + ".$k"));
EquationEntry e = null;
if (v != null)
e = v.equations.first();
if (e != null)
h.k = e.expression;
v = s.find(new Variable(c.alias + ".$max"));
e = null;
if (v != null)
e = v.equations.first();
if (e != null)
h.max = e.expression;
v = s.find(new Variable(c.alias + ".$min"));
e = null;
if (v != null)
e = v.equations.first();
if (e != null)
h.min = e.expression;
v = s.find(new Variable(c.alias + ".$radius"));
e = null;
if (v != null)
e = v.equations.first();
if (e != null)
h.radius = e.expression;
h.hasProject = s.find(new Variable(c.alias + ".$project")) != null;
h.endpoint = c.endpoint;
int i = connections.indexOf(h);
if (i < 0) {
connections.add(h);
h.resolution = c.resolution;
} else {
h = connections.get(i);
}
h.indices.add(c.index);
if (h.k != null || h.radius != null)
needNN = true;
}
result.append("ConnectIterator<" + T + "> * " + ns + "getIterators ()\n");
result.append("{\n");
if (s.connectionMatrix == null) {
if (needNN) {
result.append(" return getIteratorsNN ();\n");
} else {
result.append(" return getIteratorsSimple ();\n");
}
} else {
ConnectionMatrix cm = s.connectionMatrix;
result.append(" ConnectPopulation<" + T + "> * rows = getIterator (" + cm.rows.index + ");\n");
result.append(" ConnectPopulation<" + T + "> * cols = getIterator (" + cm.cols.index + ");\n");
// Will be deleted when ConnectMatrix is deleted.
result.append(" " + ps + " * dummy = (" + ps + " *) create ();\n");
result.append(" dummy->setPart (" + cm.rows.index + ", (*rows->instances)[0]);\n");
result.append(" dummy->setPart (" + cm.cols.index + ", (*cols->instances)[0]);\n");
// We don't actually want $p. This just forces "dummy" to initialize any local matrix variables.
result.append(" dummy->getP ();\n");
// Create iterator
result.append(" IteratorNonzero<" + T + "> * it = ");
boolean found = false;
for (ProvideOperator po : extensions) {
if (po.getIterator(cm.A, context)) {
found = true;
break;
}
}
if (!found && cm.A instanceof AccessElement) {
AccessElement ae = (AccessElement) cm.A;
Operator op0 = ae.operands[0];
result.append("::getIterator (");
if (op0 instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op0;
Variable v = av.reference.variable;
if (v.hasAttribute("temporary")) {
// Just assume that v is an alias for ReadMatrix.
// Also, matrix must be a static object. Enforced by AccessElement.hasCorrectForm().
ReadMatrix r = (ReadMatrix) v.equations.first().expression;
result.append(r.name + "->A");
} else {
result.append("& ");
context.global = false;
result.append(resolve(av.reference, context, false, "dummy->", false));
context.global = true;
}
} else // Must be a constant. Enforced by AccessElement.hasCorrectForm().
{
Constant c = (Constant) op0;
result.append(c.name);
}
result.append(");\n");
}
result.append(" return new ConnectMatrix<" + T + "> (rows, cols, " + cm.rows.index + ", " + cm.cols.index + ", it, dummy);\n");
}
result.append("}\n");
result.append("\n");
result.append("ConnectPopulation<" + T + "> * " + ns + "getIterator (int i)\n");
result.append("{\n");
result.append(" ConnectPopulation<" + T + "> * result = 0;\n");
result.append(" switch (i)\n");
result.append(" {\n");
for (ConnectionHolder h : connections) h.emit();
result.append(" }\n");
result.append(" return result;\n");
result.append("}\n");
result.append("\n");
}
// Population path
if (bed.needGlobalPath) {
result.append("void " + ns + "path (String & result)\n");
result.append("{\n");
if (// Will our container provide a non-empty path?
((BackendDataC) s.container.backendData).needLocalPath) {
result.append(" container->path (result);\n");
result.append(" result += \"." + s.name + "\";\n");
} else {
result.append(" result = \"" + s.name + "\";\n");
}
result.append("}\n");
result.append("\n");
}
}
use of gov.sandia.n2a.language.AccessElement in project n2a by frothga.
the class RendererPython method render.
public boolean render(Operator op) {
if (op instanceof Add) {
Add a = (Add) op;
// Check if this is a string expression
if (a.name != null) {
result.append(a.name);
return true;
}
return false;
}
if (op instanceof AccessElement) {
AccessElement ae = (AccessElement) op;
ae.operands[0].render(this);
if (ae.operands.length >= 2) {
result.append("[");
ae.operands[1].render(this);
if (ae.operands.length >= 3) {
result.append(",");
ae.operands[2].render(this);
}
result.append("]");
}
return true;
}
if (op instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op;
result.append(job.resolve(av.reference, this, false));
return true;
}
if (op instanceof AND) {
AND and = (AND) op;
and.render(this, " and ");
return true;
}
if (op instanceof BuildMatrix) {
BuildMatrix b = (BuildMatrix) op;
result.append(b.name);
return true;
}
if (op instanceof Constant) {
Constant c = (Constant) op;
Type o = c.value;
if (o instanceof Scalar) {
result.append(print(((Scalar) o).value));
return true;
}
if (o instanceof Text) {
result.append("\"" + o.toString() + "\"");
return true;
}
if (o instanceof Matrix) {
result.append(c.name);
return true;
}
return false;
}
if (op instanceof Event) {
Event e = (Event) op;
// The cast to bool gets rid of the specific numeric value from flags.
// If used in a numeric expression, it will convert to either 1 or 0.
result.append("bool (flags & 0x1 << " + e.eventType.valueIndex + ")");
return true;
}
if (op instanceof Exp) {
Exp e = (Exp) op;
Operator a = e.operands[0];
result.append("exp(");
a.render(this);
result.append(")");
return true;
}
if (op instanceof Gaussian) {
Gaussian g = (Gaussian) op;
result.append("gaussian(");
if (g.operands.length > 0) {
g.operands[0].render(this);
}
result.append(")");
return true;
}
if (op instanceof Grid) {
// TODO: needs library implementation
Grid g = (Grid) op;
boolean raw = g.operands.length >= 5 && g.operands[4].getString().contains("raw");
result.append("grid");
if (raw)
result.append("Raw");
result.append("(");
int count = Math.min(4, g.operands.length);
if (count > 0)
g.operands[0].render(this);
for (int i = 1; i < count; i++) {
result.append(", ");
g.operands[i].render(this);
}
result.append(")");
return true;
}
if (op instanceof Input) {
// TODO: needs library implementation
Input i = (Input) op;
String mode = "";
if (i.operands.length == 2)
mode = i.operands[1].getString();
else if (i.operands.length >= 4)
mode = i.operands[3].getString();
if (mode.contains("columns")) {
result.append(i.name + "->getColumns ()");
} else {
Operator op1 = i.operands[1];
Operator op2 = i.operands[2];
result.append(i.name + ".get");
if (mode.contains("raw"))
result.append("Raw");
result.append("(");
op1.render(this);
result.append(", ");
op2.render(this);
result.append(")");
}
return true;
}
if (op instanceof Log) {
Log l = (Log) op;
Operator a = l.operands[0];
result.append("log(");
a.render(this);
return true;
}
if (op instanceof Modulo) {
Modulo m = (Modulo) op;
Operator a = m.operand0;
Operator b = m.operand1;
a.render(this);
result.append(" % ");
b.render(this);
return true;
}
if (op instanceof Norm) {
Norm n = (Norm) op;
Operator A = n.operands[0];
result.append("numpy.linalg.norm(");
A.render(this);
result.append(", ");
n.operands[1].render(this);
result.append(")");
return true;
}
if (op instanceof OR) {
OR or = (OR) op;
or.render(this, " or ");
return true;
}
if (op instanceof Output) {
Output o = (Output) op;
result.append(o.name + "->trace (Simulator::instance.currentEvent->t, ");
if (// column name is explicit
o.hasColumnName) {
o.operands[2].render(this);
} else // column name is generated, so use prepared string value
{
result.append(o.columnName);
}
result.append(", ");
o.operands[1].render(this);
result.append(")");
return true;
}
if (op instanceof Power) {
Power p = (Power) op;
Operator a = p.operand0;
Operator b = p.operand1;
result.append("pow(");
a.render(this);
result.append(", ");
b.render(this);
result.append(")");
return true;
}
if (op instanceof ReadMatrix) {
ReadMatrix r = (ReadMatrix) op;
String mode = "";
int lastParm = r.operands.length - 1;
if (lastParm > 0) {
if (r.operands[lastParm] instanceof Constant) {
Constant c = (Constant) r.operands[lastParm];
if (c.value instanceof Text) {
mode = ((Text) c.value).value;
}
}
}
if (mode.equals("rows")) {
result.append(r.name + "->rows ()");
} else if (mode.equals("columns")) {
result.append(r.name + "->columns ()");
} else {
result.append(r.name + "->get");
if (mode.equals("raw"))
result.append("Raw");
result.append("(");
r.operands[1].render(this);
result.append(", ");
r.operands[2].render(this);
result.append(")");
}
return true;
}
if (op instanceof SquareRoot) {
SquareRoot s = (SquareRoot) op;
Operator a = s.operands[0];
result.append("sqrt(");
a.render(this);
return true;
}
if (op instanceof Tangent) {
Tangent t = (Tangent) op;
Operator a = t.operands[0];
result.append("tan(");
a.render(this);
result.append(")");
return true;
}
if (op instanceof Uniform) {
Uniform u = (Uniform) op;
result.append("uniform(");
if (u.operands.length > 0) {
u.operands[0].render(this);
}
result.append(")");
return true;
}
return super.render(op);
}
use of gov.sandia.n2a.language.AccessElement in project n2a by frothga.
the class ExportJob method fakeConnectionTarget.
/**
* Replace any entries of the form A=connect() with A=N2A_fakePart.
*/
public static boolean fakeConnectionTarget(EquationSet s) {
boolean result = false;
for (Variable v : s.variables) {
if (// may be due to unreadable part names in connect()
v.equations.size() == 0) {
String value = s.source.get(v.nameString());
if (!Operator.containsConnect(value))
continue;
try {
v.add(new EquationEntry("N2A_fakePart"));
result = true;
} catch (Exception e) {
}
} else if (// could be a properly-parsed connect() line
v.equations.size() == 1) {
EquationEntry e = v.equations.first();
// connect() appears like AccessElement during initial parse
if (!(e.expression instanceof AccessElement))
continue;
AccessElement ae = (AccessElement) e.expression;
AccessVariable av = (AccessVariable) ae.operands[0];
if (!av.name.equals("connect"))
continue;
av.name = "N2A_fakePart";
e.expression = av;
result = true;
}
}
for (EquationSet p : s.parts) if (fakeConnectionTarget(p))
result = true;
return result;
}
use of gov.sandia.n2a.language.AccessElement in project n2a by frothga.
the class RendererC method render.
public boolean render(Operator op) {
for (ProvideOperator po : job.extensions) {
Boolean result = po.render(this, op);
if (result != null)
return result;
}
if (op instanceof Add) {
Add a = (Add) op;
// Check if this is a string expression
if (a.name != null) {
result.append(a.name);
return true;
}
return false;
}
if (op instanceof AccessElement) {
AccessElement ae = (AccessElement) op;
ae.operands[0].render(this);
if (ae.operands.length == 2) {
result.append("[");
ae.operands[1].render(this);
result.append("]");
} else if (ae.operands.length == 3) {
result.append("(");
ae.operands[1].render(this);
result.append(",");
ae.operands[2].render(this);
result.append(")");
}
return true;
}
if (op instanceof AccessVariable) {
AccessVariable av = (AccessVariable) op;
result.append(job.resolve(av.reference, this, false));
return true;
}
if (op instanceof Atan) {
Atan atan = (Atan) op;
int shift = atan.exponent - atan.exponentNext;
if (useExponent && shift != 0)
result.append("(");
if (atan.operands.length > 1 || useExponent)
result.append("atan2 (");
else
result.append("atan (");
Operator y = atan.operands[0];
if (y.getType() instanceof Matrix) {
y.render(this);
result.append("[1], ");
y.render(this);
result.append("[0]");
} else {
y.render(this);
if (atan.operands.length > 1) {
result.append(", ");
// x
atan.operands[1].render(this);
} else if (useExponent) {
int shiftX = Operator.MSB - y.exponent;
int x = shiftX >= 0 ? 0x1 << shiftX : 0;
result.append(", " + x);
}
}
result.append(")");
if (useExponent && shift != 0)
result.append(printShift(shift) + ")");
return true;
}
if (op instanceof BuildMatrix) {
BuildMatrix b = (BuildMatrix) op;
result.append(b.name);
return true;
}
if (op instanceof Columns) {
Columns c = (Columns) op;
c.operands[0].render(this);
result.append(".columns ()");
return true;
}
if (op instanceof Constant) {
Constant c = (Constant) op;
Type o = c.value;
if (o instanceof Scalar) {
result.append(print(((Scalar) o).value, c.exponentNext));
return true;
}
if (o instanceof Text) {
result.append("\"" + o.toString() + "\"");
return true;
}
if (o instanceof Matrix) {
result.append(c.name);
return true;
}
return false;
}
if (op instanceof Delay) {
Delay d = (Delay) op;
if (d.operands.length == 1) {
result.append("(");
d.operands[0].render(this);
result.append(")");
return true;
}
result.append("delay" + d.index + ".step (Simulator<" + job.T + ">::instance.currentEvent->t, ");
d.operands[1].render(this);
result.append(", ");
d.operands[0].render(this);
result.append(", ");
if (d.operands.length > 2)
d.operands[2].render(this);
else
result.append("0");
result.append(")");
return true;
}
if (op instanceof Event) {
Event e = (Event) op;
// The cast to bool gets rid of the specific numeric value from flags.
// If used in a numeric expression, it should convert to either 1 or 0.
result.append("((bool) (flags & (" + bed.localFlagType + ") 0x1 << " + e.eventType.valueIndex + "))");
return true;
}
if (op instanceof Exp) {
Exp e = (Exp) op;
Operator a = e.operands[0];
result.append("exp (");
a.render(this);
if (useExponent)
result.append(", " + e.exponentNext);
result.append(")");
return true;
}
if (op instanceof Gaussian) {
Gaussian g = (Gaussian) op;
result.append("gaussian<" + job.T + "> (");
if (g.operands.length > 0) {
g.operands[0].render(this);
}
result.append(")");
return true;
}
if (op instanceof Grid) {
Grid g = (Grid) op;
boolean raw = g.operands.length >= 5 && g.operands[4].getString().contains("raw");
int shift = g.exponent - g.exponentNext;
if (useExponent && shift != 0)
result.append("(");
result.append("grid");
if (raw)
result.append("Raw");
result.append("<" + job.T + "> (");
int count = Math.min(4, g.operands.length);
if (count > 0)
g.operands[0].render(this);
for (int i = 1; i < count; i++) {
result.append(", ");
g.operands[i].render(this);
}
result.append(")");
if (useExponent && shift != 0)
result.append(printShift(shift) + ")");
return true;
}
if (op instanceof HyperbolicTangent) {
HyperbolicTangent t = (HyperbolicTangent) op;
Operator a = t.operands[0];
result.append("tanh (");
a.render(this);
result.append(")");
return true;
}
if (op instanceof Input) {
Input i = (Input) op;
result.append(i.name + "->get (");
if (// return matrix
i.operands.length < 3 || i.operands[2].getDouble() < 0) {
boolean time = i.getMode().contains("time");
String defaultLine = time ? "-INFINITY" : "0";
if (i.operands.length > 1) {
Operator op1 = i.operands[1];
if (// expression for line
op1.getType() instanceof Scalar)
// expression for line
op1.render(this);
else
// op1 is probably the mode flag
result.append(defaultLine);
} else // line is not specified. We're probably just retrieving a dummy matrix to get column count.
{
result.append(defaultLine);
}
} else // return scalar
{
i.operands[1].render(this);
result.append(", ");
i.operands[2].render(this);
}
result.append(")");
return true;
}
if (op instanceof Log) {
Log l = (Log) op;
Operator a = l.operands[0];
result.append("log (");
a.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + l.exponentNext);
result.append(")");
return true;
}
if (op instanceof Max) {
Max m = (Max) op;
for (int i = 0; i < m.operands.length - 1; i++) {
Operator a = m.operands[i];
result.append("max (");
renderType(a);
result.append(", ");
}
Operator b = m.operands[m.operands.length - 1];
renderType(b);
for (int i = 0; i < m.operands.length - 1; i++) result.append(")");
return true;
}
if (op instanceof Min) {
Min m = (Min) op;
for (int i = 0; i < m.operands.length - 1; i++) {
Operator a = m.operands[i];
result.append("min (");
renderType(a);
result.append(", ");
}
Operator b = m.operands[m.operands.length - 1];
renderType(b);
for (int i = 0; i < m.operands.length - 1; i++) result.append(")");
return true;
}
if (op instanceof Modulo) {
Modulo m = (Modulo) op;
Operator a = m.operand0;
Operator b = m.operand1;
result.append("modFloor (");
moduloParam(a);
result.append(", ");
moduloParam(b);
result.append(")");
return true;
}
if (op instanceof Norm) {
Norm n = (Norm) op;
Operator A = n.operands[0];
result.append("norm (");
A.render(this);
result.append(", ");
n.operands[1].render(this);
if (useExponent)
result.append(", " + A.exponentNext + ", " + n.exponentNext);
result.append(")");
return true;
}
if (op instanceof Output) {
Output o = (Output) op;
result.append(o.name + "->trace (Simulator<" + job.T + ">::instance.currentEvent->t, ");
if (// column name is explicit
o.hasColumnName) {
o.operands[2].render(this);
} else // column name is generated, so use prepared string value
{
result.append(o.columnName);
}
result.append(", ");
o.operands[1].render(this);
if (useExponent)
result.append(", " + o.operands[1].exponentNext);
result.append(", ");
if (// No mode string
o.operands.length < 4) {
// null
result.append("0");
} else if (// Mode string is constant
o.operands[3] instanceof Constant) {
result.append("\"" + o.operands[3] + "\"");
} else if (// Mode string is calculated
o.operands[3] instanceof Add) {
Add a = (Add) o.operands[3];
// No need for cast or call c_str()
result.append(a.name);
}
// else badness
result.append(")");
return true;
}
if (op instanceof Power) {
Power p = (Power) op;
Operator a = p.operand0;
Operator b = p.operand1;
result.append("pow (");
a.render(this);
result.append(", ");
b.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + p.exponentNext);
result.append(")");
return true;
}
if (op instanceof Pulse) {
Pulse p = (Pulse) op;
Operator t = p.operands[0];
result.append("pulse (");
renderType(t);
for (int i = 1; i < p.operands.length; i++) {
result.append(", ");
renderType(p.operands[i]);
}
result.append(")");
return true;
}
if (op instanceof ReadMatrix) {
ReadMatrix r = (ReadMatrix) op;
// Currently, ReadMatrix sets its exponent = exponentNext, so we will never do a shift here.
// Any shifting should be handled by matrixHelper while loading and converting the matrix to integer.
// matrices are held in pointers, so need to dereference
result.append("*" + r.name + "->A");
return true;
}
if (op instanceof Rows) {
Rows r = (Rows) op;
r.operands[0].render(this);
result.append(".rows ()");
return true;
}
if (op instanceof Sat) {
Sat s = (Sat) op;
Operator a = s.operands[0];
Operator lower = s.operands[1];
Operator upper;
if (s.operands.length >= 3)
upper = s.operands[2];
else
upper = lower;
result.append("max (");
if (s.operands.length == 2)
result.append("-1 * (");
renderType(lower);
if (s.operands.length == 2)
result.append(")");
result.append(", min (");
renderType(upper);
result.append(", ");
renderType(a);
result.append("))");
return true;
}
if (op instanceof SquareRoot) {
SquareRoot s = (SquareRoot) op;
Operator a = s.operands[0];
result.append("sqrt (");
a.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + s.exponentNext);
result.append(")");
return true;
}
if (op instanceof SumSquares) {
SumSquares ss = (SumSquares) op;
Operator A = ss.operands[0];
result.append("sumSquares (");
A.render(this);
if (useExponent)
result.append(", " + A.exponentNext + ", " + ss.exponentNext);
result.append(")");
return true;
}
if (op instanceof Tangent) {
Tangent t = (Tangent) op;
Operator a = t.operands[0];
result.append("tan (");
a.render(this);
if (useExponent)
result.append(", " + a.exponentNext + ", " + t.exponentNext);
result.append(")");
return true;
}
if (op instanceof Uniform) {
Uniform u = (Uniform) op;
result.append("uniform<" + job.T + "> (");
for (int i = 0; i < u.operands.length; i++) {
if (i > 0)
result.append(", ");
u.operands[i].render(this);
}
result.append(")");
return true;
}
return super.render(op);
}
Aggregations