use of gov.sandia.n2a.language.type.Scalar 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);
}
use of gov.sandia.n2a.language.type.Scalar 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;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Gaussian method eval.
public Type eval(Instance context) throws EvaluationException {
Random random;
Simulator simulator = Simulator.getSimulator(context);
if (simulator == null)
random = new Random();
else
random = simulator.random;
if (operands.length == 0)
return new Scalar(random.nextGaussian());
Type sigma = operands[0].eval(context);
if (sigma instanceof Scalar) {
return new Scalar(random.nextGaussian() * ((Scalar) sigma).value);
} else if (sigma instanceof Matrix) {
Matrix scale = (Matrix) sigma;
int rows = scale.rows();
int columns = scale.columns();
if (columns == 1) {
Matrix result = new MatrixDense(rows, 1);
for (int i = 0; i < rows; i++) result.set(i, random.nextGaussian() * scale.get(i, 0));
return result;
} else if (rows == 1) {
Matrix result = new MatrixDense(columns, 1);
for (int i = 0; i < columns; i++) result.set(i, random.nextGaussian() * scale.get(0, i));
return result;
} else {
Matrix temp = new MatrixDense(columns, 1);
for (int i = 0; i < columns; i++) temp.set(i, random.nextGaussian());
return sigma.multiply(temp);
}
} else {
// We could throw an exception, but this is easy enough.
return new Scalar(random.nextGaussian());
}
}
use of gov.sandia.n2a.language.type.Scalar 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.Scalar in project n2a by frothga.
the class Input method getRow.
public Holder getRow(Instance context, Type op1, boolean time) {
Simulator simulator = Simulator.getSimulator(context);
// 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 {
// get an input holder
String path = ((Text) operands[0].eval(context)).value;
H = simulator.inputs.get(path);
if (H == null) {
H = new Holder();
if (// not ideal; reading stdin should be reserved for headless operation
path.isEmpty())
// not ideal; reading stdin should be reserved for headless operation
H.stream = new BufferedReader(new InputStreamReader(System.in));
else
H.stream = new BufferedReader(new FileReader(new File(path).getAbsoluteFile()));
// sqrt (epsilon for time representation (currently double)), about 1e-8
H.epsilon = Math.sqrt(Math.ulp(1.0));
if (simulator.currentEvent instanceof EventStep)
H.epsilon = Math.min(H.epsilon, ((EventStep) simulator.currentEvent).dt / 1000);
simulator.inputs.put(path, H);
}
if (op1 instanceof Scalar)
H.getRow(((Scalar) op1).value, time);
else
H.getRow(0, time);
} catch (IOException e) {
return null;
}
return H;
}
Aggregations