Search in sources :

Example 11 with Text

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

the class Output method determineVariableName.

// This method should be called by analysis, with v set to the variable that holds this equation.
public void determineVariableName(Variable v) {
    // Ensure that the first operand is a file name.
    // If no file name is specified, stdout is used. We get the same effect by specifying the empty string, so insert it here.
    // This makes parameter processing much simpler elsewhere.
    int length = operands.length;
    if (length > 0 && !isStringExpression(v, operands[0])) {
        variableName = variableName0;
        Operator[] newOperands = new Operator[length + 1];
        for (int i = 0; i < length; i++) newOperands[i + 1] = operands[i];
        newOperands[0] = new Constant(new Text());
        operands = newOperands;
    } else {
        variableName = variableName1;
    }
    hasColumnName = operands.length > 2 && !(operands[2] instanceof Constant && ((Constant) operands[2]).value.toString().isEmpty());
    if (// Column name not specified
    !hasColumnName) {
        if (variableName == null)
            variableName = v.nameString();
        EquationSet container = v.container;
        if (// regular part
        container.connectionBindings == null) {
            dependOnIndex(v, container);
        } else // connection
        {
            // depend on all endpoints
            for (ConnectionBinding c : container.connectionBindings) {
                dependOnIndex(v, c.endpoint);
            }
        }
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) Constant(gov.sandia.n2a.language.Constant) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) Text(gov.sandia.n2a.language.type.Text)

Example 12 with Text

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

Example 13 with Text

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

the class Input method getRow.

public Holder getRow(Instance context, double line) {
    Simulator simulator = Simulator.instance.get();
    // 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 {
        String mode = getMode();
        boolean smooth = mode.contains("smooth");
        boolean time = smooth || mode.contains("time");
        // get an input holder
        String path = ((Text) operands[0].eval(context)).value;
        H = Holder.get(simulator, path, time);
        if (H.time != time && !timeWarning) {
            Backend.err.get().println("WARNING: Changed time mode for input(" + path + ")");
            timeWarning = true;
        }
        // remember for use by caller
        H.smooth = smooth;
        if (!H.time && Double.isInfinite(line))
            line = 0;
        H.getRow(line);
    } catch (IOException e) {
        return null;
    }
    return H;
}
Also used : Text(gov.sandia.n2a.language.type.Text) IOException(java.io.IOException) Simulator(gov.sandia.n2a.backend.internal.Simulator)

Example 14 with Text

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

the class BuildMatrix method simplify.

public Operator simplify(Variable from) {
    int cols = operands.length;
    if (cols == 0)
        return this;
    int rows = operands[0].length;
    if (rows == 0)
        return this;
    // potential constant to replace us
    Matrix A = new MatrixDense(rows, cols);
    // any element that is not constant will change this to false
    boolean isConstant = true;
    for (int c = 0; c < cols; c++) {
        for (int r = 0; r < rows; r++) {
            if (operands[c][r] == null) {
                A.set(r, c, 0);
            } else {
                operands[c][r] = operands[c][r].simplify(from);
                if (// stop evaluating if we already know we are not constant
                isConstant) {
                    if (operands[c][r] instanceof Constant) {
                        Type o = ((Constant) operands[c][r]).value;
                        if (o instanceof Scalar)
                            A.set(r, c, ((Scalar) o).value);
                        else if (o instanceof Text)
                            A.set(r, c, Double.valueOf(((Text) o).value));
                        else if (o instanceof Matrix)
                            A.set(r, c, ((Matrix) o).get(0, 0));
                        else
                            throw new EvaluationException("Can't construct matrix element from the given type.");
                    } else {
                        isConstant = false;
                    }
                }
            }
        }
    }
    if (isConstant) {
        from.changed = true;
        return new Constant(A);
    }
    return this;
}
Also used : Matrix(gov.sandia.n2a.language.type.Matrix) ASTConstant(gov.sandia.n2a.language.parse.ASTConstant) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Text(gov.sandia.n2a.language.type.Text) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 15 with Text

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

the class JobC method prepareStaticObjects.

public void prepareStaticObjects(Operator op, final CRenderer context, final String pad) throws Exception {
    Visitor visitor = new Visitor() {

        public boolean visit(Operator op) {
            if (op instanceof Output) {
                Output o = (Output) op;
                if (// column name is generated
                o.operands.length < 3) {
                    String stringName = stringNames.get(op);
                    BackendDataC bed = (BackendDataC) context.part.backendData;
                    if (context.global ? bed.needGlobalPath : bed.needLocalPath) {
                        context.result.append(pad + "path (" + stringName + ");\n");
                        context.result.append(pad + stringName + " += \"." + o.variableName + "\";\n");
                    } else {
                        context.result.append(pad + stringName + " = \"" + o.variableName + "\";\n");
                    }
                } else if (// mode flags are present
                o.operands.length > 3) {
                    if (o.operands[0] instanceof Constant) {
                        // Detect raw flag
                        Operator op3 = o.operands[3];
                        if (op3 instanceof Constant) {
                            if (op3.toString().contains("raw")) {
                                String outputName = outputNames.get(o.operands[0].toString());
                                context.result.append(pad + outputName + "->raw = true;\n");
                            }
                        }
                    }
                }
                // Continue to drill down, because I/O functions can be nested.
                return true;
            }
            if (op instanceof Input) {
                Input i = (Input) op;
                if (i.operands[0] instanceof Constant) {
                    String inputName = inputNames.get(i.operands[0].toString());
                    if (!context.global) {
                        context.result.append(pad + inputName + "->epsilon = " + resolve(context.bed.dt.reference, context, false) + " / 1000;\n");
                    }
                    // Detect time flag
                    String mode = "";
                    if (i.operands.length > 3) {
                        // just assuming it's a constant string
                        mode = i.operands[3].toString();
                    } else if (i.operands[1] instanceof Constant) {
                        Constant c = (Constant) i.operands[1];
                        if (c.value instanceof Text)
                            mode = c.toString();
                    }
                    if (mode.contains("time")) {
                        context.result.append(pad + inputName + "->time = true;\n");
                    }
                }
                return true;
            }
            return true;
        }
    };
    op.visit(visitor);
}
Also used : Operator(gov.sandia.n2a.language.Operator) Input(gov.sandia.n2a.language.function.Input) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Output(gov.sandia.n2a.language.function.Output) Text(gov.sandia.n2a.language.type.Text)

Aggregations

Text (gov.sandia.n2a.language.type.Text)21 Matrix (gov.sandia.n2a.language.type.Matrix)12 Scalar (gov.sandia.n2a.language.type.Scalar)12 Constant (gov.sandia.n2a.language.Constant)10 Operator (gov.sandia.n2a.language.Operator)10 Type (gov.sandia.n2a.language.Type)10 AccessVariable (gov.sandia.n2a.language.AccessVariable)7 Visitor (gov.sandia.n2a.language.Visitor)7 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)6 Input (gov.sandia.n2a.language.function.Input)6 Output (gov.sandia.n2a.language.function.Output)6 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)6 Simulator (gov.sandia.n2a.backend.internal.Simulator)5 EquationSet (gov.sandia.n2a.eqset.EquationSet)5 Variable (gov.sandia.n2a.eqset.Variable)5 Add (gov.sandia.n2a.language.operator.Add)5 Event (gov.sandia.n2a.language.function.Event)4 MatrixDense (gov.sandia.n2a.linear.MatrixDense)4 Backend (gov.sandia.n2a.plugins.extpoints.Backend)3 ConnectionMatrix (gov.sandia.n2a.eqset.EquationSet.ConnectionMatrix)2