Search in sources :

Example 6 with Text

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

the class JobC method generateStatic.

public void generateStatic(RendererC context, EquationSet s) {
    for (EquationSet p : s.parts) generateStatic(context, p);
    context.setPart(s);
    BackendDataC bed = context.bed;
    StringBuilder result = context.result;
    class CheckStatic implements Visitor {

        public boolean global;

        public boolean visit(Operator op) {
            for (ProvideOperator po : extensions) {
                Boolean result = po.generateStatic(context, op);
                if (result != null)
                    return result;
            }
            if (op instanceof BuildMatrix) {
                BuildMatrix m = (BuildMatrix) op;
                m.name = "Matrix" + matrixNames.size();
                matrixNames.put(m, m.name);
                return false;
            }
            if (op instanceof Constant) {
                Constant constant = (Constant) op;
                Type m = constant.value;
                if (m instanceof Matrix) {
                    Matrix A = (Matrix) m;
                    int rows = A.rows();
                    int cols = A.columns();
                    constant.name = "Matrix" + matrixNames.size();
                    matrixNames.put(constant, constant.name);
                    result.append("MatrixFixed<" + T + "," + rows + "," + cols + "> " + constant.name + " = {");
                    String initializer = "";
                    for (int c = 0; c < cols; c++) {
                        for (int r = 0; r < rows; r++) {
                            initializer += context.print(A.get(r, c), constant.exponent) + ", ";
                        }
                    }
                    if (initializer.length() > 2)
                        initializer = initializer.substring(0, initializer.length() - 2);
                    result.append(initializer + "};\n");
                }
                // Don't try to descend tree from here
                return false;
            }
            if (op instanceof Function) {
                Function f = (Function) op;
                if (// Handle computed strings
                f instanceof Output) {
                    Output o = (Output) f;
                    if (// We need to auto-generate the column name.
                    !o.hasColumnName) {
                        o.columnName = "columnName" + stringNames.size();
                        stringNames.put(op, o.columnName);
                        if (global) {
                            bed.setGlobalNeedPath(s);
                            bed.globalColumns.add(o.columnName);
                        } else {
                            bed.setLocalNeedPath(s);
                            bed.localColumns.add(o.columnName);
                        }
                    }
                    if (// Mode is calculated
                    o.operands.length > 3 && o.operands[3] instanceof Add) {
                        Add a = (Add) o.operands[3];
                        a.name = "columnMode" + stringNames.size();
                        stringNames.put(op, a.name);
                    }
                }
                // Detect functions that need static handles
                if (f.operands.length > 0) {
                    Operator operand0 = f.operands[0];
                    if (operand0 instanceof Constant) {
                        Constant c = (Constant) operand0;
                        if (c.value instanceof Text) {
                            String fileName = ((Text) c.value).value;
                            if (f instanceof ReadMatrix) {
                                ReadMatrix r = (ReadMatrix) f;
                                r.name = matrixNames.get(fileName);
                                if (r.name == null) {
                                    r.name = "Matrix" + matrixNames.size();
                                    matrixNames.put(fileName, r.name);
                                    mainMatrix.add(r);
                                    result.append("MatrixInput<" + T + "> * " + r.name + ";\n");
                                }
                            } else if (f instanceof Input) {
                                Input i = (Input) f;
                                i.name = inputNames.get(fileName);
                                if (i.name == null) {
                                    i.name = "Input" + inputNames.size();
                                    inputNames.put(fileName, i.name);
                                    mainInput.add(i);
                                    result.append("InputHolder<" + T + "> * " + i.name + ";\n");
                                }
                            } else if (f instanceof Output) {
                                Output o = (Output) f;
                                o.name = outputNames.get(fileName);
                                if (o.name == null) {
                                    o.name = "Output" + outputNames.size();
                                    outputNames.put(fileName, o.name);
                                    mainOutput.add(o);
                                    result.append("OutputHolder<" + T + "> * " + o.name + ";\n");
                                }
                            }
                        }
                    } else // Dynamic file name (no static handle)
                    {
                        boolean error = false;
                        if (f instanceof ReadMatrix) {
                            ReadMatrix r = (ReadMatrix) f;
                            matrixNames.put(op, r.name = "Matrix" + matrixNames.size());
                            stringNames.put(operand0, r.fileName = "fileName" + stringNames.size());
                            if (operand0 instanceof Add)
                                ((Add) operand0).name = r.fileName;
                            else
                                error = true;
                        } else if (f instanceof Input) {
                            Input i = (Input) f;
                            inputNames.put(op, i.name = "Input" + inputNames.size());
                            stringNames.put(operand0, i.fileName = "fileName" + stringNames.size());
                            if (operand0 instanceof Add)
                                ((Add) operand0).name = i.fileName;
                            else
                                error = true;
                        } else if (f instanceof Output) {
                            Output o = (Output) f;
                            outputNames.put(op, o.name = "Output" + outputNames.size());
                            stringNames.put(operand0, o.fileName = "fileName" + stringNames.size());
                            if (operand0 instanceof Add)
                                ((Add) operand0).name = o.fileName;
                            else
                                error = true;
                        }
                        if (error) {
                            Backend.err.get().println("ERROR: File name must be a string expression.");
                            throw new AbortRun();
                        }
                    }
                }
                // Functions could be nested, so continue descent.
                return true;
            }
            return true;
        }
    }
    CheckStatic checkStatic = new CheckStatic();
    for (Variable v : s.ordered) {
        checkStatic.global = v.hasAttribute("global");
        v.visit(checkStatic);
    }
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) EquationSet(gov.sandia.n2a.eqset.EquationSet) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) AbortRun(gov.sandia.n2a.plugins.extpoints.Backend.AbortRun) Function(gov.sandia.n2a.language.Function) Type(gov.sandia.n2a.language.Type) Input(gov.sandia.n2a.language.function.Input) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) ConnectionMatrix(gov.sandia.n2a.eqset.EquationSet.ConnectionMatrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Output(gov.sandia.n2a.language.function.Output)

Example 7 with Text

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

the class JobC method type.

public String type(Variable v) {
    if (v.type instanceof Matrix) {
        Matrix m = (Matrix) v.type;
        int rows = m.rows();
        int cols = m.columns();
        // Known dimension, so use more efficient storage.
        if (rows > 0 && cols > 0)
            return "MatrixFixed<" + T + "," + rows + "," + cols + ">";
        // Arbitrary dimension
        return "Matrix<" + T + ">";
    }
    if (v.type instanceof Text)
        return "String";
    return T;
}
Also used : ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) ConnectionMatrix(gov.sandia.n2a.eqset.EquationSet.ConnectionMatrix) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) Text(gov.sandia.n2a.language.type.Text) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint)

Example 8 with Text

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

the class BuildMatrix method eval.

public Type eval(Instance context) throws EvaluationException {
    int columns = operands.length;
    if (columns == 0)
        return new MatrixDense();
    int rows = operands[0].length;
    if (rows == 0)
        return new MatrixDense();
    Matrix result = new MatrixDense(rows, columns);
    for (int c = 0; c < columns; c++) {
        for (int r = 0; r < rows; r++) {
            if (operands[c][r] == null) {
                result.set(r, c, 0);
            } else {
                Type o = operands[c][r].eval(context);
                if (o instanceof Scalar)
                    result.set(r, c, ((Scalar) o).value);
                else if (o instanceof Text)
                    result.set(r, c, Double.valueOf(((Text) o).value));
                else if (o instanceof Matrix)
                    result.set(r, c, ((Matrix) o).get(0, 0));
                else
                    throw new EvaluationException("Can't construct matrix element from the given type.");
            }
        }
    }
    return result;
}
Also used : 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 9 with Text

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

the class Output method isStringExpression.

/**
 *        Determines if the given operator is a string expression.
 *        This is the case if any operator it depends on is a string.
 *        If an operand is a variable, then its equations must contain a string.
 */
public static boolean isStringExpression(Variable v, Operator op) {
    class StringVisitor implements Visitor {

        boolean foundString;

        Variable from;

        public StringVisitor(Variable from) {
            this.from = from;
            from.visited = null;
        }

        public boolean visit(Operator op) {
            if (op instanceof Constant) {
                Constant c = (Constant) op;
                if (c.value instanceof Text)
                    foundString = true;
                return false;
            }
            if (op instanceof AccessVariable) {
                AccessVariable av = (AccessVariable) op;
                Variable v = av.reference.variable;
                // Prevent infinite recursion
                Variable p = from;
                while (p != null) {
                    if (p == v)
                        return false;
                    p = p.visited;
                }
                v.visited = from;
                from = v;
                v.visit(this);
                from = v.visited;
                return false;
            }
            // no reason to dig any further
            if (foundString)
                return false;
            // Add is the only operator that can propagate string values. All other operators and functions return scalars or matrices.
            return op instanceof Add;
        }
    }
    StringVisitor visitor = new StringVisitor(v);
    op.visit(visitor);
    return visitor.foundString;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) AccessVariable(gov.sandia.n2a.language.AccessVariable) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) Text(gov.sandia.n2a.language.type.Text)

Example 10 with Text

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

the class Output method eval.

public Type eval(Instance context) {
    Type result = operands[1].eval(context);
    Simulator simulator = Simulator.instance.get();
    if (simulator == null)
        return result;
    String mode = null;
    if (operands.length > 3)
        mode = operands[3].eval(context).toString();
    String path = ((Text) operands[0].eval(context)).value;
    boolean raw = mode != null && mode.contains("raw");
    Holder H = Holder.get(simulator, path, raw);
    String column = getColumnName(context);
    double now;
    if (simulator.currentEvent == null)
        now = 0;
    else
        now = (float) simulator.currentEvent.t;
    if (result instanceof Matrix) {
        Matrix A = (Matrix) result;
        int rows = A.rows();
        int cols = A.columns();
        if (rows == 1) {
            for (int c = 0; c < cols; c++) H.trace(now, column + "(" + c + ")", (float) A.get(0, c), mode);
        } else if (cols == 1) {
            for (int r = 0; r < rows; r++) H.trace(now, column + "(" + r + ")", (float) A.get(r, 0), mode);
        } else {
            for (int r = 0; r < rows; r++) {
                for (int c = 0; c < cols; c++) {
                    H.trace(now, column + "(" + r + "," + c + ")", (float) A.get(r, c), mode);
                }
            }
        }
    } else {
        H.trace(now, column, (float) ((Scalar) result).value, mode);
    }
    return result;
}
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) Scalar(gov.sandia.n2a.language.type.Scalar)

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