use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class JobC method generateStatic.
public void generateStatic(RendererC context, EquationSet s) {
for (EquationSet p : s.parts) generateStatic(context, p);
context.setPart(s);
BackendDataC bed = context.bed;
StringBuilder result = context.result;
class CheckStatic implements Visitor {
public boolean global;
public boolean visit(Operator op) {
for (ProvideOperator po : extensions) {
Boolean result = po.generateStatic(context, op);
if (result != null)
return result;
}
if (op instanceof BuildMatrix) {
BuildMatrix m = (BuildMatrix) op;
m.name = "Matrix" + matrixNames.size();
matrixNames.put(m, m.name);
return false;
}
if (op instanceof Constant) {
Constant constant = (Constant) op;
Type m = constant.value;
if (m instanceof Matrix) {
Matrix A = (Matrix) m;
int rows = A.rows();
int cols = A.columns();
constant.name = "Matrix" + matrixNames.size();
matrixNames.put(constant, constant.name);
result.append("MatrixFixed<" + T + "," + rows + "," + cols + "> " + constant.name + " = {");
String initializer = "";
for (int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
initializer += context.print(A.get(r, c), constant.exponent) + ", ";
}
}
if (initializer.length() > 2)
initializer = initializer.substring(0, initializer.length() - 2);
result.append(initializer + "};\n");
}
// Don't try to descend tree from here
return false;
}
if (op instanceof Function) {
Function f = (Function) op;
if (// Handle computed strings
f instanceof Output) {
Output o = (Output) f;
if (// We need to auto-generate the column name.
!o.hasColumnName) {
o.columnName = "columnName" + stringNames.size();
stringNames.put(op, o.columnName);
if (global) {
bed.setGlobalNeedPath(s);
bed.globalColumns.add(o.columnName);
} else {
bed.setLocalNeedPath(s);
bed.localColumns.add(o.columnName);
}
}
if (// Mode is calculated
o.operands.length > 3 && o.operands[3] instanceof Add) {
Add a = (Add) o.operands[3];
a.name = "columnMode" + stringNames.size();
stringNames.put(op, a.name);
}
}
// Detect functions that need static handles
if (f.operands.length > 0) {
Operator operand0 = f.operands[0];
if (operand0 instanceof Constant) {
Constant c = (Constant) operand0;
if (c.value instanceof Text) {
String fileName = ((Text) c.value).value;
if (f instanceof ReadMatrix) {
ReadMatrix r = (ReadMatrix) f;
r.name = matrixNames.get(fileName);
if (r.name == null) {
r.name = "Matrix" + matrixNames.size();
matrixNames.put(fileName, r.name);
mainMatrix.add(r);
result.append("MatrixInput<" + T + "> * " + r.name + ";\n");
}
} else if (f instanceof Input) {
Input i = (Input) f;
i.name = inputNames.get(fileName);
if (i.name == null) {
i.name = "Input" + inputNames.size();
inputNames.put(fileName, i.name);
mainInput.add(i);
result.append("InputHolder<" + T + "> * " + i.name + ";\n");
}
} else if (f instanceof Output) {
Output o = (Output) f;
o.name = outputNames.get(fileName);
if (o.name == null) {
o.name = "Output" + outputNames.size();
outputNames.put(fileName, o.name);
mainOutput.add(o);
result.append("OutputHolder<" + T + "> * " + o.name + ";\n");
}
}
}
} else // Dynamic file name (no static handle)
{
boolean error = false;
if (f instanceof ReadMatrix) {
ReadMatrix r = (ReadMatrix) f;
matrixNames.put(op, r.name = "Matrix" + matrixNames.size());
stringNames.put(operand0, r.fileName = "fileName" + stringNames.size());
if (operand0 instanceof Add)
((Add) operand0).name = r.fileName;
else
error = true;
} else if (f instanceof Input) {
Input i = (Input) f;
inputNames.put(op, i.name = "Input" + inputNames.size());
stringNames.put(operand0, i.fileName = "fileName" + stringNames.size());
if (operand0 instanceof Add)
((Add) operand0).name = i.fileName;
else
error = true;
} else if (f instanceof Output) {
Output o = (Output) f;
outputNames.put(op, o.name = "Output" + outputNames.size());
stringNames.put(operand0, o.fileName = "fileName" + stringNames.size());
if (operand0 instanceof Add)
((Add) operand0).name = o.fileName;
else
error = true;
}
if (error) {
Backend.err.get().println("ERROR: File name must be a string expression.");
throw new AbortRun();
}
}
}
// Functions could be nested, so continue descent.
return true;
}
return true;
}
}
CheckStatic checkStatic = new CheckStatic();
for (Variable v : s.ordered) {
checkStatic.global = v.hasAttribute("global");
v.visit(checkStatic);
}
}
use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class JobC method type.
public String type(Variable v) {
if (v.type instanceof Matrix) {
Matrix m = (Matrix) v.type;
int rows = m.rows();
int cols = m.columns();
// Known dimension, so use more efficient storage.
if (rows > 0 && cols > 0)
return "MatrixFixed<" + T + "," + rows + "," + cols + ">";
// Arbitrary dimension
return "Matrix<" + T + ">";
}
if (v.type instanceof Text)
return "String";
return T;
}
use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class BuildMatrix method eval.
public Type eval(Instance context) throws EvaluationException {
int columns = operands.length;
if (columns == 0)
return new MatrixDense();
int rows = operands[0].length;
if (rows == 0)
return new MatrixDense();
Matrix result = new MatrixDense(rows, columns);
for (int c = 0; c < columns; c++) {
for (int r = 0; r < rows; r++) {
if (operands[c][r] == null) {
result.set(r, c, 0);
} else {
Type o = operands[c][r].eval(context);
if (o instanceof Scalar)
result.set(r, c, ((Scalar) o).value);
else if (o instanceof Text)
result.set(r, c, Double.valueOf(((Text) o).value));
else if (o instanceof Matrix)
result.set(r, c, ((Matrix) o).get(0, 0));
else
throw new EvaluationException("Can't construct matrix element from the given type.");
}
}
}
return result;
}
use of gov.sandia.n2a.language.type.Text 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 implements 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;
}
use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class Output method eval.
public Type eval(Instance context) {
Type result = operands[1].eval(context);
Simulator simulator = Simulator.instance.get();
if (simulator == null)
return result;
String mode = null;
if (operands.length > 3)
mode = operands[3].eval(context).toString();
String path = ((Text) operands[0].eval(context)).value;
boolean raw = mode != null && mode.contains("raw");
Holder H = Holder.get(simulator, path, raw);
String column = getColumnName(context);
double now;
if (simulator.currentEvent == null)
now = 0;
else
now = (float) simulator.currentEvent.t;
if (result instanceof Matrix) {
Matrix A = (Matrix) result;
int rows = A.rows();
int cols = A.columns();
if (rows == 1) {
for (int c = 0; c < cols; c++) H.trace(now, column + "(" + c + ")", (float) A.get(0, c), mode);
} else if (cols == 1) {
for (int r = 0; r < rows; r++) H.trace(now, column + "(" + r + ")", (float) A.get(r, 0), mode);
} else {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
H.trace(now, column + "(" + r + "," + c + ")", (float) A.get(r, c), mode);
}
}
}
} else {
H.trace(now, column, (float) ((Scalar) result).value, mode);
}
return result;
}
Aggregations