Search in sources :

Example 6 with Type

use of gov.sandia.n2a.language.Type 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;
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 7 with Type

use of gov.sandia.n2a.language.Type 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 8 with Type

use of gov.sandia.n2a.language.Type 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)

Example 9 with Type

use of gov.sandia.n2a.language.Type in project n2a by frothga.

the class AND method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    if (operand0 instanceof Constant) {
        Type c0 = ((Constant) operand0).value;
        if (c0 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c0).value;
            if (value == 0) {
                operand1.releaseDependencies(from);
                return new Constant(new Scalar(0));
            }
            return operand1;
        }
    } else if (operand1 instanceof Constant) {
        Type c1 = ((Constant) operand1).value;
        if (c1 instanceof Scalar) {
            from.changed = true;
            double value = ((Scalar) c1).value;
            if (value == 0) {
                operand0.releaseDependencies(from);
                return new Constant(new Scalar(0));
            }
            return operand0;
        }
    }
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 10 with Type

use of gov.sandia.n2a.language.Type in project n2a by frothga.

the class Multiply method simplify.

public Operator simplify(Variable from) {
    Operator result = super.simplify(from);
    if (result != this)
        return result;
    // This will be reversed below if we don't actually make a change.
    from.changed = true;
    if (operand0 instanceof Constant) {
        Type c0 = ((Constant) operand0).value;
        if (c0 instanceof Scalar) {
            double value = ((Scalar) c0).value;
            if (value == 1)
                return operand1;
            if (value == 0) {
                operand1.releaseDependencies(from);
                return new Constant(new Scalar(0));
            }
        }
    } else if (operand1 instanceof Constant) {
        Type c1 = ((Constant) operand1).value;
        if (c1 instanceof Scalar) {
            double value = ((Scalar) c1).value;
            if (value == 1)
                return operand0;
            if (value == 0) {
                operand0.releaseDependencies(from);
                return new Constant(new Scalar(0));
            }
        }
    }
    from.changed = false;
    return this;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Type (gov.sandia.n2a.language.Type)24 Scalar (gov.sandia.n2a.language.type.Scalar)17 Operator (gov.sandia.n2a.language.Operator)10 Constant (gov.sandia.n2a.language.Constant)9 Variable (gov.sandia.n2a.eqset.Variable)8 AccessVariable (gov.sandia.n2a.language.AccessVariable)7 Matrix (gov.sandia.n2a.language.type.Matrix)6 Text (gov.sandia.n2a.language.type.Text)6 Instance (gov.sandia.n2a.language.type.Instance)5 Simulator (gov.sandia.n2a.backend.internal.Simulator)4 EquationSet (gov.sandia.n2a.eqset.EquationSet)4 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)3 ArrayList (java.util.ArrayList)3 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)2 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)2 ParseException (gov.sandia.n2a.language.ParseException)2 Visitor (gov.sandia.n2a.language.Visitor)2 Random (java.util.Random)2 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)1 Conversion (gov.sandia.n2a.backend.internal.InternalBackendData.Conversion)1