Search in sources :

Example 16 with Scalar

use of gov.sandia.n2a.language.type.Scalar 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 17 with Scalar

use of gov.sandia.n2a.language.type.Scalar 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 18 with Scalar

use of gov.sandia.n2a.language.type.Scalar 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 19 with Scalar

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

Example 20 with Scalar

use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.

the class MultiplyElementwise 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;
        }
    } else if (operand1 instanceof Constant) {
        Type c1 = ((Constant) operand1).value;
        if (c1 instanceof Scalar) {
            double value = ((Scalar) c1).value;
            if (value == 1)
                return operand0;
        }
    }
    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

Scalar (gov.sandia.n2a.language.type.Scalar)42 Type (gov.sandia.n2a.language.Type)17 AccessVariable (gov.sandia.n2a.language.AccessVariable)14 Variable (gov.sandia.n2a.eqset.Variable)13 Matrix (gov.sandia.n2a.language.type.Matrix)11 Constant (gov.sandia.n2a.language.Constant)10 Operator (gov.sandia.n2a.language.Operator)10 Text (gov.sandia.n2a.language.type.Text)8 EquationSet (gov.sandia.n2a.eqset.EquationSet)6 ArrayList (java.util.ArrayList)6 Simulator (gov.sandia.n2a.backend.internal.Simulator)5 Instance (gov.sandia.n2a.language.type.Instance)5 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)5 TreeSet (java.util.TreeSet)5 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)4 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)4 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)3 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)2 EvaluationException (gov.sandia.n2a.language.EvaluationException)2