use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class Output method determineVariableName.
// This method should be called by analysis, with v set to the variable that holds this equation.
public void determineVariableName(Variable v) {
// Ensure that the first operand is a file name.
// If no file name is specified, stdout is used. We get the same effect by specifying the empty string, so insert it here.
// This makes parameter processing much simpler elsewhere.
int length = operands.length;
if (length > 0 && !isStringExpression(v, operands[0])) {
variableName = variableName0;
Operator[] newOperands = new Operator[length + 1];
for (int i = 0; i < length; i++) newOperands[i + 1] = operands[i];
newOperands[0] = new Constant(new Text());
operands = newOperands;
} else {
variableName = variableName1;
}
hasColumnName = operands.length > 2 && !(operands[2] instanceof Constant && ((Constant) operands[2]).value.toString().isEmpty());
if (// Column name not specified
!hasColumnName) {
if (variableName == null)
variableName = v.nameString();
EquationSet container = v.container;
if (// regular part
container.connectionBindings == null) {
dependOnIndex(v, container);
} else // connection
{
// depend on all endpoints
for (ConnectionBinding c : container.connectionBindings) {
dependOnIndex(v, c.endpoint);
}
}
}
}
use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class Grid method eval.
public Type eval(Instance context) throws EvaluationException {
// collect parameters into arrays
int i = (int) Math.floor(((Scalar) operands[0].eval(context)).value);
int nx = 1;
int ny = 1;
int nz = 1;
boolean raw = false;
if (operands.length >= 2)
nx = (int) Math.floor(((Scalar) operands[1].eval(context)).value);
if (operands.length >= 3)
ny = (int) Math.floor(((Scalar) operands[2].eval(context)).value);
if (operands.length >= 4)
nz = (int) Math.floor(((Scalar) operands[3].eval(context)).value);
if (operands.length >= 5) {
Type mode = operands[4].eval(context);
if (mode instanceof Text && ((Text) mode).value.contains("raw"))
raw = true;
}
// compute xyz in stride order
Matrix result = new MatrixDense(3, 1);
// stride x
int sx = ny * nz;
if (raw) {
// (i / sx) is an integer operation, so remainder is truncated.
result.set(0, i / sx);
i %= sx;
result.set(1, i / nz);
result.set(2, i % nz);
} else {
result.set(0, ((i / sx) + 0.5) / nx);
i %= sx;
result.set(1, ((i / nz) + 0.5) / ny);
result.set(2, ((i % nz) + 0.5) / nz);
}
return result;
}
use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class Input method getRow.
public Holder getRow(Instance context, double line) {
Simulator simulator = Simulator.instance.get();
// If we can't cache a line from the requested stream, then semantics of this function are lost, so give up.
if (simulator == null)
return null;
Holder H = null;
try {
String mode = getMode();
boolean smooth = mode.contains("smooth");
boolean time = smooth || mode.contains("time");
// get an input holder
String path = ((Text) operands[0].eval(context)).value;
H = Holder.get(simulator, path, time);
if (H.time != time && !timeWarning) {
Backend.err.get().println("WARNING: Changed time mode for input(" + path + ")");
timeWarning = true;
}
// remember for use by caller
H.smooth = smooth;
if (!H.time && Double.isInfinite(line))
line = 0;
H.getRow(line);
} catch (IOException e) {
return null;
}
return H;
}
use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class BuildMatrix method simplify.
public Operator simplify(Variable from) {
int cols = operands.length;
if (cols == 0)
return this;
int rows = operands[0].length;
if (rows == 0)
return this;
// potential constant to replace us
Matrix A = new MatrixDense(rows, cols);
// any element that is not constant will change this to false
boolean isConstant = true;
for (int c = 0; c < cols; c++) {
for (int r = 0; r < rows; r++) {
if (operands[c][r] == null) {
A.set(r, c, 0);
} else {
operands[c][r] = operands[c][r].simplify(from);
if (// stop evaluating if we already know we are not constant
isConstant) {
if (operands[c][r] instanceof Constant) {
Type o = ((Constant) operands[c][r]).value;
if (o instanceof Scalar)
A.set(r, c, ((Scalar) o).value);
else if (o instanceof Text)
A.set(r, c, Double.valueOf(((Text) o).value));
else if (o instanceof Matrix)
A.set(r, c, ((Matrix) o).get(0, 0));
else
throw new EvaluationException("Can't construct matrix element from the given type.");
} else {
isConstant = false;
}
}
}
}
}
if (isConstant) {
from.changed = true;
return new Constant(A);
}
return this;
}
use of gov.sandia.n2a.language.type.Text in project n2a by frothga.
the class JobC method prepareStaticObjects.
public void prepareStaticObjects(Operator op, final CRenderer context, final String pad) throws Exception {
Visitor visitor = new Visitor() {
public boolean visit(Operator op) {
if (op instanceof Output) {
Output o = (Output) op;
if (// column name is generated
o.operands.length < 3) {
String stringName = stringNames.get(op);
BackendDataC bed = (BackendDataC) context.part.backendData;
if (context.global ? bed.needGlobalPath : bed.needLocalPath) {
context.result.append(pad + "path (" + stringName + ");\n");
context.result.append(pad + stringName + " += \"." + o.variableName + "\";\n");
} else {
context.result.append(pad + stringName + " = \"" + o.variableName + "\";\n");
}
} else if (// mode flags are present
o.operands.length > 3) {
if (o.operands[0] instanceof Constant) {
// Detect raw flag
Operator op3 = o.operands[3];
if (op3 instanceof Constant) {
if (op3.toString().contains("raw")) {
String outputName = outputNames.get(o.operands[0].toString());
context.result.append(pad + outputName + "->raw = true;\n");
}
}
}
}
// Continue to drill down, because I/O functions can be nested.
return true;
}
if (op instanceof Input) {
Input i = (Input) op;
if (i.operands[0] instanceof Constant) {
String inputName = inputNames.get(i.operands[0].toString());
if (!context.global) {
context.result.append(pad + inputName + "->epsilon = " + resolve(context.bed.dt.reference, context, false) + " / 1000;\n");
}
// Detect time flag
String mode = "";
if (i.operands.length > 3) {
// just assuming it's a constant string
mode = i.operands[3].toString();
} else if (i.operands[1] instanceof Constant) {
Constant c = (Constant) i.operands[1];
if (c.value instanceof Text)
mode = c.toString();
}
if (mode.contains("time")) {
context.result.append(pad + inputName + "->time = true;\n");
}
}
return true;
}
return true;
}
};
op.visit(visitor);
}
Aggregations