Search in sources :

Example 1 with Simulator

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();
}
Also used : Path(java.nio.file.Path) PrintStream(java.io.PrintStream) EquationSet(gov.sandia.n2a.eqset.EquationSet) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) HostSystem(gov.sandia.n2a.execenvs.HostSystem) Simulator(gov.sandia.n2a.backend.internal.Simulator) FileNotFoundException(java.io.FileNotFoundException)

Example 2 with Simulator

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());
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Random(java.util.Random) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 3 with Simulator

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) EventStep(gov.sandia.n2a.backend.internal.EventStep) BufferedReader(java.io.BufferedReader) Text(gov.sandia.n2a.language.type.Text) FileReader(java.io.FileReader) IOException(java.io.IOException) Simulator(gov.sandia.n2a.backend.internal.Simulator) File(java.io.File) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 4 with Simulator

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;
}
Also used : Type(gov.sandia.n2a.language.Type) Text(gov.sandia.n2a.language.type.Text) InstanceTemporaries(gov.sandia.n2a.backend.internal.InstanceTemporaries) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 5 with Simulator

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)));
            }
        }
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Text(gov.sandia.n2a.language.type.Text) Simulator(gov.sandia.n2a.backend.internal.Simulator) File(java.io.File) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Simulator (gov.sandia.n2a.backend.internal.Simulator)6 Scalar (gov.sandia.n2a.language.type.Scalar)5 Type (gov.sandia.n2a.language.Type)4 Matrix (gov.sandia.n2a.language.type.Matrix)3 Text (gov.sandia.n2a.language.type.Text)3 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)2 File (java.io.File)2 Random (java.util.Random)2 EventStep (gov.sandia.n2a.backend.internal.EventStep)1 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)1 EquationSet (gov.sandia.n2a.eqset.EquationSet)1 HostSystem (gov.sandia.n2a.execenvs.HostSystem)1 BufferedReader (java.io.BufferedReader)1 FileNotFoundException (java.io.FileNotFoundException)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 Path (java.nio.file.Path)1