use of gov.sandia.n2a.backend.internal.Simulator in project n2a by frothga.
the class XyceBackend method start.
@Override
public void start(final MNode job) {
Thread t = new Thread() {
@Override
public void run() {
Path jobDir = Paths.get(job.get()).getParent();
try {
Backend.err.set(new PrintStream(jobDir.resolve("err").toFile()));
} catch (FileNotFoundException e) {
}
try {
Files.createFile(jobDir.resolve("started"));
// Ensure essential metadata is set
if (job.child("$metadata", "duration") == null)
job.set("$metadata", "duration", "1.0");
if (job.child("$metadata", "seed") == null)
job.set("$metadata", "seed", System.currentTimeMillis());
if (job.child("$metadata", "backend.xyce.integrator") == null)
job.set("$metadata", "backend.xyce.integrator", "trapezoid");
// set up job info
HostSystem env = HostSystem.get(job.getOrDefault("$metadata", "host", "localhost"));
String xyce = env.getNamedValue("xyce.binary");
Path cirFile = jobDir.resolve("model.cir");
// "prn" doesn't work, at least on windows
Path prnFile = jobDir.resolve("result");
EquationSet e = new EquationSet(job);
Simulator simulator = InternalBackend.constructStaticNetwork(e, jobDir.toString());
analyze(e);
// Just in case a $p expression says something different than $metadata.duration
String duration = e.getNamedValue("duration");
if (!duration.isEmpty())
job.set(duration, "$metadata", "duration");
FileWriter writer = new FileWriter(cirFile.toFile());
generateNetlist(job, simulator, writer);
writer.close();
PrintStream ps = Backend.err.get();
if (ps != System.err) {
ps.close();
Backend.err.remove();
}
long pid = env.submitJob(job, xyce + " " + env.quotePath(cirFile) + " -o " + env.quotePath(prnFile));
job.set("$metadata", "pid", pid);
} catch (AbortRun a) {
} catch (Exception e) {
e.printStackTrace(Backend.err.get());
}
PrintStream ps = err.get();
if (ps != System.err)
ps.close();
}
};
t.setDaemon(true);
t.start();
}
use of gov.sandia.n2a.backend.internal.Simulator 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.backend.internal.Simulator 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;
}
use of gov.sandia.n2a.backend.internal.Simulator in project n2a by frothga.
the class Output method eval.
public Type eval(Instance context) {
Type result = operands[1].eval(context);
Simulator simulator = Simulator.getSimulator(context);
if (simulator == null)
return result;
String path = ((Text) operands[0].eval(context)).value;
Holder H = simulator.outputs.get(path);
if (H == null) {
H = new Holder(simulator, path);
if (operands.length > 3)
H.raw = operands[3].eval(context).toString().contains("raw");
}
// Determine column name
String column;
if (// column name is specified
operands.length > 2) {
column = operands[2].eval(context).toString();
} else // auto-generate column name
{
if (context instanceof InstanceTemporaries)
context = ((InstanceTemporaries) context).wrapped;
column = (String) context.valuesObject[index];
if (column == null) {
String prefix = context.path();
if (prefix.isEmpty())
column = variableName;
else
column = prefix + "." + variableName;
context.valuesObject[index] = column;
}
}
double now;
if (simulator.currentEvent == null)
now = 0;
else
now = (float) simulator.currentEvent.t;
H.trace(now, column, (float) ((Scalar) result).value);
return result;
}
use of gov.sandia.n2a.backend.internal.Simulator in project n2a by frothga.
the class ReadMatrix method eval.
public Type eval(Instance context) {
Simulator simulator = Simulator.getSimulator(context);
// absence of simulator indicates analysis phase, so opening files is unnecessary
if (simulator == null)
return new Scalar(0);
String path = ((Text) operands[0].eval(context)).value;
Matrix A = simulator.matrices.get(path);
if (A == null) {
A = Matrix.factory(new File(path).getAbsoluteFile());
simulator.matrices.put(path, A);
}
String mode = "";
int lastParm = operands.length - 1;
if (lastParm > 0) {
Type parmValue = operands[lastParm].eval(context);
if (parmValue instanceof Text)
mode = ((Text) parmValue).value;
}
if (mode.equals("columns"))
return new Scalar(A.columns());
if (mode.equals("rows"))
return new Scalar(A.rows());
int rows = A.rows();
int columns = A.columns();
int lastRow = rows - 1;
int lastColumn = columns - 1;
double row = ((Scalar) operands[1].eval(context)).value;
double column = ((Scalar) operands[2].eval(context)).value;
if (mode.equals("raw")) {
int r = (int) Math.floor(row);
int c = (int) Math.floor(column);
if (r < 0)
r = 0;
else if (r >= rows)
r = lastRow;
if (c < 0)
c = 0;
else if (c >= columns)
c = lastColumn;
return new Scalar(A.get(r, c));
} else {
row *= lastRow;
column *= lastColumn;
int r = (int) Math.floor(row);
int c = (int) Math.floor(column);
if (r < 0) {
if (c < 0)
return new Scalar(A.get(0, 0));
else if (c >= lastColumn)
return new Scalar(A.get(0, lastColumn));
else {
double b = column - c;
return new Scalar((1 - b) * A.get(0, c) + b * A.get(0, c + 1));
}
} else if (r >= lastRow) {
if (c < 0)
return new Scalar(A.get(lastRow, 0));
else if (c >= lastColumn)
return new Scalar(A.get(lastRow, lastColumn));
else {
double b = column - c;
return new Scalar((1 - b) * A.get(lastRow, c) + b * A.get(lastRow, c + 1));
}
} else {
double a = row - r;
double a1 = 1 - a;
if (c < 0)
return new Scalar(a1 * A.get(r, 0) + a * A.get(r + 1, 0));
else if (c >= lastColumn)
return new Scalar(a1 * A.get(r, lastColumn) + a * A.get(r + 1, lastColumn));
else {
double b = column - c;
return new Scalar((1 - b) * (a1 * A.get(r, c) + a * A.get(r + 1, c)) + b * (a1 * A.get(r, c + 1) + a * A.get(r + 1, c + 1)));
}
}
}
}
Aggregations