Search in sources :

Example 1 with BuildMatrix

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

the class JobC method prepareDynamicObjects.

/**
 *        Build complex sub-expressions into a single local variable that can be referenced by the equation.
 */
public void prepareDynamicObjects(Operator op, final CRenderer context, final boolean init, final String pad) throws Exception {
    // Pass 1 -- Strings and matrix expressions
    Visitor visitor1 = new Visitor() {

        public boolean visit(Operator op) {
            if (op instanceof BuildMatrix) {
                BuildMatrix m = (BuildMatrix) op;
                int rows = m.getRows();
                int cols = m.getColumns();
                String matrixName = "Matrix" + matrixNames.size();
                matrixNames.put(m, matrixName);
                if (rows == 3 && cols == 1)
                    context.result.append(pad + "Vector3 " + matrixName + ";\n");
                else
                    context.result.append(pad + "Matrix<float> " + matrixName + " (" + rows + ", " + cols + ");\n");
                for (int r = 0; r < rows; r++) {
                    if (cols == 1) {
                        context.result.append(pad + matrixName + "[" + r + "] = ");
                        m.operands[0][r].render(context);
                        context.result.append(";\n");
                    } else {
                        for (int c = 0; c < cols; c++) {
                            context.result.append(pad + matrixName + "(" + r + "," + c + ") = ");
                            m.operands[c][r].render(context);
                            context.result.append(";\n");
                        }
                    }
                }
                return false;
            }
            if (op instanceof Add) {
                Add a = (Add) op;
                String stringName = stringNames.get(a);
                if (stringName != null) {
                    context.result.append(pad + "String " + stringName + ";\n");
                    for (Operator o : flattenAdd(a)) {
                        context.result.append(pad + stringName + " += ");
                        o.render(context);
                        context.result.append(";\n");
                    }
                    return false;
                }
            }
            return true;
        }
    };
    op.visit(visitor1);
    // Pass 2 -- Input functions
    Visitor visitor2 = new Visitor() {

        public boolean visit(Operator op) {
            if (op instanceof ReadMatrix) {
                ReadMatrix r = (ReadMatrix) op;
                if (!(r.operands[0] instanceof Constant)) {
                    String matrixName = matrixNames.get(r);
                    String stringName = stringNames.get(r.operands[0]);
                    context.result.append(pad + "MatrixInput * " + matrixName + " = matrixHelper (" + stringName + ");\n");
                }
                return false;
            }
            if (op instanceof Input) {
                Input i = (Input) op;
                if (!(i.operands[0] instanceof Constant)) {
                    String inputName = inputNames.get(i);
                    String stringName = stringNames.get(i.operands[0]);
                    context.result.append(pad + "InputHolder * " + inputName + " = inputHelper (" + stringName + ");\n");
                    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");
                    }
                }
                // I/O functions can be nested
                return true;
            }
            if (op instanceof Output) {
                Output o = (Output) op;
                if (!(o.operands[0] instanceof Constant)) {
                    String outputName = outputNames.get(o);
                    String stringName = stringNames.get(o.operands[0]);
                    context.result.append(pad + "OutputHolder * " + outputName + " = outputHelper (" + stringName + ");\n");
                    // Detect raw flag
                    if (o.operands.length > 3) {
                        Operator op3 = o.operands[3];
                        if (op3 instanceof Constant) {
                            if (op3.toString().contains("raw")) {
                                context.result.append(pad + outputName + "->raw = true;\n");
                            }
                        }
                    }
                }
                return true;
            }
            return true;
        }
    };
    op.visit(visitor2);
}
Also used : Operator(gov.sandia.n2a.language.Operator) Add(gov.sandia.n2a.language.operator.Add) Input(gov.sandia.n2a.language.function.Input) Visitor(gov.sandia.n2a.language.Visitor) Constant(gov.sandia.n2a.language.Constant) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Output(gov.sandia.n2a.language.function.Output) Text(gov.sandia.n2a.language.type.Text) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix)

Aggregations

BuildMatrix (gov.sandia.n2a.language.BuildMatrix)1 Constant (gov.sandia.n2a.language.Constant)1 Operator (gov.sandia.n2a.language.Operator)1 Visitor (gov.sandia.n2a.language.Visitor)1 Input (gov.sandia.n2a.language.function.Input)1 Output (gov.sandia.n2a.language.function.Output)1 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)1 Add (gov.sandia.n2a.language.operator.Add)1 Text (gov.sandia.n2a.language.type.Text)1