Search in sources :

Example 1 with Output

use of gov.sandia.n2a.language.function.Output in project n2a by frothga.

the class JobC method generateStatic.

public void generateStatic(final EquationSet s, final StringBuilder result) {
    for (EquationSet p : s.parts) generateStatic(p, result);
    // Generate static definitions
    final BackendDataC bed = (BackendDataC) s.backendData;
    class CheckStatic extends Visitor {

        public boolean global;

        public boolean visit(Operator op) {
            if (op instanceof Constant) {
                Type m = ((Constant) op).value;
                if (m instanceof Matrix) {
                    Matrix A = (Matrix) m;
                    int rows = A.rows();
                    int cols = A.columns();
                    String matrixName = "Matrix" + matrixNames.size();
                    matrixNames.put(op, matrixName);
                    if (rows == 3 && cols == 1)
                        result.append("Vector3 " + matrixName + " = Matrix<float>");
                    else
                        result.append("Matrix<float> " + matrixName);
                    result.append(" (\"" + A + "\");\n");
                }
                // Don't try to descend tree from here
                return false;
            }
            if (op instanceof Function) {
                Function f = (Function) op;
                if (// We need to auto-generate the column name.
                f instanceof Output && f.operands.length < 3) {
                    String stringName = "columnName" + stringNames.size();
                    stringNames.put(op, stringName);
                    if (global) {
                        bed.setGlobalNeedPath(s);
                        bed.globalColumns.add(stringName);
                    } else {
                        bed.setLocalNeedPath(s);
                        bed.localColumns.add(stringName);
                    }
                }
                // Detect functions that need static handles
                if (f.operands.length > 0) {
                    Operator operand0 = f.operands[0];
                    if (operand0 instanceof Constant) {
                        Constant c = (Constant) operand0;
                        Type o = c.value;
                        if (o instanceof Text) {
                            String fileName = ((Text) o).value;
                            if (op instanceof ReadMatrix) {
                                if (!matrixNames.containsKey(fileName)) {
                                    String matrixName = "Matrix" + matrixNames.size();
                                    matrixNames.put(fileName, matrixName);
                                    result.append("MatrixInput * " + matrixName + " = matrixHelper (\"" + fileName + "\");\n");
                                }
                            } else if (f instanceof Input) {
                                if (!inputNames.containsKey(fileName)) {
                                    String inputName = "Input" + inputNames.size();
                                    inputNames.put(fileName, inputName);
                                    result.append("InputHolder * " + inputName + " = inputHelper (\"" + fileName + "\");\n");
                                }
                            } else if (f instanceof Output) {
                                if (!outputNames.containsKey(fileName)) {
                                    String outputName = "Output" + outputNames.size();
                                    outputNames.put(fileName, outputName);
                                    result.append("OutputHolder * " + outputName + " = outputHelper (\"" + fileName + "\");\n");
                                }
                            }
                        }
                    } else // Dynamic file name (no static handle)
                    {
                        if (f instanceof ReadMatrix) {
                            matrixNames.put(op, "Matrix" + matrixNames.size());
                            stringNames.put(operand0, "fileName" + stringNames.size());
                        } else if (f instanceof Input) {
                            inputNames.put(op, "Input" + inputNames.size());
                            stringNames.put(operand0, "fileName" + stringNames.size());
                        } else if (f instanceof Output) {
                            outputNames.put(op, "Output" + outputNames.size());
                            stringNames.put(operand0, "fileName" + stringNames.size());
                        }
                    }
                }
                // 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) 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) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix) 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) BuildMatrix(gov.sandia.n2a.language.BuildMatrix) Matrix(gov.sandia.n2a.language.type.Matrix) Output(gov.sandia.n2a.language.function.Output)

Example 2 with Output

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

Example 3 with Output

use of gov.sandia.n2a.language.function.Output 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 4 with Output

use of gov.sandia.n2a.language.function.Output 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, RendererC context, boolean init, String pad) {
    final BackendDataC bed = context.bed;
    // 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();
                context.result.append(pad + "MatrixFixed<" + T + "," + rows + "," + cols + "> " + m.name + ";\n");
                for (int r = 0; r < rows; r++) {
                    if (cols == 1) {
                        context.result.append(pad + m.name + "[" + r + "] = ");
                        m.operands[0][r].render(context);
                        context.result.append(";\n");
                    } else {
                        for (int c = 0; c < cols; c++) {
                            context.result.append(pad + m.name + "(" + r + "," + c + ") = ");
                            m.operands[c][r].render(context);
                            context.result.append(";\n");
                        }
                    }
                }
                return false;
            }
            if (op instanceof Add) {
                Add a = (Add) op;
                if (a.name != null) {
                    context.result.append(pad + "String " + a.name + ";\n");
                    for (Operator o : flattenAdd(a)) {
                        context.result.append(pad + a.name + " += ");
                        o.render(context);
                        context.result.append(";\n");
                    }
                    return false;
                }
            }
            return true;
        }
    };
    op.visit(visitor1);
    // Pass 2 -- I/O functions
    Visitor visitor2 = new Visitor() {

        public boolean visit(Operator op) {
            if (op instanceof ReadMatrix) {
                ReadMatrix r = (ReadMatrix) op;
                if (!(r.operands[0] instanceof Constant)) {
                    context.result.append(pad + "MatrixInput<" + T + "> * " + r.name + " = matrixHelper<" + T + "> (" + r.fileName);
                    if (T.equals("int"))
                        context.result.append(", " + r.exponent);
                    context.result.append(");\n");
                }
                return false;
            }
            if (op instanceof Input) {
                Input i = (Input) op;
                if (!(i.operands[0] instanceof Constant)) {
                    context.result.append(pad + "InputHolder<" + T + "> * " + i.name + " = inputHelper<" + T + "> (" + i.fileName);
                    if (T.equals("int"))
                        context.result.append(", " + i.exponent);
                    context.result.append(");\n");
                    String mode = i.getMode();
                    boolean smooth = mode.contains("smooth");
                    boolean time = smooth || mode.contains("time");
                    if (time) {
                        if (time)
                            context.result.append(pad + i.name + "->time = true;\n");
                        if (smooth)
                            context.result.append(pad + i.name + "->smooth = true;\n");
                        if (!context.global && !T.equals("int")) {
                            boolean lvalue = !bed.dt.hasAttribute("constant");
                            context.result.append(pad + i.name + "->epsilon = " + resolve(bed.dt.reference, context, lvalue) + " / 1000.0");
                            if (T.equals("float"))
                                context.result.append("f");
                            context.result.append(";\n");
                        }
                    }
                }
                // I/O functions can be nested
                return true;
            }
            if (op instanceof Output) {
                Output o = (Output) op;
                if (!(o.operands[0] instanceof Constant)) {
                    context.result.append(pad + "OutputHolder<" + T + "> * " + o.name + " = outputHelper<" + T + "> (" + o.fileName + ");\n");
                    if (o.operands.length > 3) {
                        if (o.operands[3] instanceof Constant) {
                            if (o.operands[3].getString().contains("raw")) {
                                context.result.append(pad + o.name + "->raw = true;\n");
                            }
                        } else if (o.operands[3] instanceof Add) {
                            // Even though we could check the assembled string in generated code, it's cleaner to check for a constant substring now.
                            Add a = (Add) o.operands[3];
                            for (Operator fa : flattenAdd(a)) {
                                if (fa.getString().contains("raw")) {
                                    context.result.append(pad + o.name + "->raw = true;\n");
                                    break;
                                }
                            }
                        }
                    }
                }
                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) ExtensionPoint(gov.sandia.n2a.plugins.ExtensionPoint) ReadMatrix(gov.sandia.n2a.language.function.ReadMatrix)

Example 5 with Output

use of gov.sandia.n2a.language.function.Output in project n2a by frothga.

the class InternalBackendData method analyze.

public void analyze(final EquationSet s) {
    boolean headless = AppData.properties.getBoolean("headless");
    if (!headless)
        System.out.println(s.name);
    if (s.connectionBindings != null) {
        // Note that populations have already been allocated in the constructor.
        endpoints = countLocalObject;
        countLocalObject += s.connectionBindings.size();
    }
    fastExit = s.metadata.getFlag("backend", "all", "fastExit");
    for (// we want the sub-lists to be ordered correctly
    Variable v : // we want the sub-lists to be ordered correctly
    s.ordered) {
        if (!headless) {
            String className = "null";
            if (v.type != null)
                className = v.type.getClass().getSimpleName();
            String dimensionName = "";
            if (v.unit != null)
                dimensionName = v.unit.toString();
            System.out.println("  " + v.nameString() + " " + v.attributeString() + " " + className + " " + dimensionName);
        }
        if (v.name.equals("$connect"))
            connect = v;
        else if (v.name.equals("$index"))
            index = v;
        else if (v.name.equals("$init"))
            init = v;
        else if (v.name.equals("$live"))
            live = v;
        else if (v.name.equals("$n") && v.order == 0)
            n = v;
        else if (v.name.equals("$p") && v.order == 0)
            p = v;
        else if (v.name.equals("$type"))
            type = v;
        else if (v.name.equals("$xyz") && v.order == 0)
            xyz = v;
        else if (v.name.equals("$t")) {
            if (v.order == 0)
                t = v;
            else if (v.order == 1)
                dt = v;
        }
        boolean initOnly = v.hasAttribute("initOnly");
        boolean emptyCombiner = v.isEmptyCombiner();
        boolean updates = !initOnly && v.equations.size() > 0 && !emptyCombiner && (v.derivative == null || v.hasAttribute("updates"));
        boolean temporary = v.hasAttribute("temporary");
        boolean unusedTemporary = temporary && !v.hasUsers();
        if (v.hasAttribute("externalWrite"))
            v.externalWrite = true;
        if (v.hasAttribute("global")) {
            v.global = true;
            v.visit(new Visitor() {

                public boolean visit(Operator op) {
                    if (op instanceof AccessVariable) {
                        AccessVariable av = (AccessVariable) op;
                        if (av.reference.resolution.size() > 0)
                            addReferenceGlobal(av.reference, s);
                        return false;
                    }
                    if (op instanceof Output) {
                        Output o = (Output) op;
                        if (!o.hasColumnName) {
                            o.index = countGlobalObject++;
                            namesGlobalObject.add("columnName" + o.index);
                        }
                        // Continue descent, because parameters of output() may contain variable references
                        return true;
                    }
                    return true;
                }
            });
            if (// eliminate non-computed values, unless they refer to a variable outside the immediate equation set
            !v.hasAny(new String[] { "constant", "accessor", "readOnly" }) || v.hasAll(new String[] { "constant", "reference" })) {
                if (updates)
                    globalUpdate.add(v);
                if (!unusedTemporary && !emptyCombiner)
                    globalInit.add(v);
                if (v.hasAttribute("reference")) {
                    addReferenceGlobal(v.reference, s);
                } else if (!temporary && !v.hasAttribute("dummy")) {
                    if (!v.hasAttribute("preexistent"))
                        globalMembers.add(v);
                    boolean external = false;
                    if (v.externalWrite || v.assignment != Variable.REPLACE) {
                        external = true;
                        globalBufferedExternalWrite.add(v);
                    }
                    if (external || (v.hasAttribute("externalRead") && updates)) {
                        external = true;
                        globalBufferedExternal.add(v);
                    }
                    if (!external && v.hasAttribute("cycle")) {
                        globalBufferedInternal.add(v);
                        if (!initOnly)
                            globalBufferedInternalUpdate.add(v);
                    }
                }
            }
        } else // local
        {
            v.visit(new Visitor() {

                public boolean visit(Operator op) {
                    if (op instanceof AccessVariable) {
                        AccessVariable av = (AccessVariable) op;
                        if (av.reference.resolution.size() > 0)
                            addReferenceLocal(av.reference, s);
                        return false;
                    }
                    if (op instanceof Output) {
                        Output o = (Output) op;
                        if (!o.hasColumnName) {
                            o.index = countLocalObject++;
                            namesLocalObject.add("columnName" + o.index);
                        }
                        // Continue descent, because parameters of output() may contain variable references
                        return true;
                    }
                    return true;
                }
            });
            if (!v.hasAny(new String[] { "constant", "accessor", "readOnly" }) || v.hasAll(new String[] { "constant", "reference" })) {
                if (updates)
                    localUpdate.add(v);
                if (!unusedTemporary && !emptyCombiner && !forbiddenLocalInit.contains(v.name))
                    localInit.add(v);
                if (v.hasAttribute("reference")) {
                    addReferenceLocal(v.reference, s);
                } else if (!temporary && !v.hasAttribute("dummy")) {
                    if (!v.hasAttribute("preexistent"))
                        localMembers.add(v);
                    boolean external = false;
                    if (v.externalWrite || v.assignment != Variable.REPLACE) {
                        external = true;
                        localBufferedExternalWrite.add(v);
                    }
                    if (external || (v.hasAttribute("externalRead") && updates)) {
                        external = true;
                        localBufferedExternal.add(v);
                    }
                    if (!external && v.hasAttribute("cycle")) {
                        localBufferedInternal.add(v);
                        if (!initOnly)
                            localBufferedInternalUpdate.add(v);
                    }
                }
            }
        }
    }
    for (// we need these to be in order by differential level, not by dependency
    Variable v : // we need these to be in order by differential level, not by dependency
    s.variables) {
        if (v.derivative != null && !v.hasAny(new String[] { "constant", "initOnly" })) {
            if (v.hasAttribute("global"))
                globalIntegrated.add(v);
            else
                localIntegrated.add(v);
        }
    }
    if (dt != null && dt.hasAttribute("constant")) {
        setDt = true;
        // However, if the nearest container that defines $t' matches our value, then don't set $t'.
        if (s.container != null) {
            Variable pdt = s.container.findDt();
            if (pdt != null && pdt.hasAttribute("constant")) {
                double value = dt.equations.first().expression.getDouble();
                double pvalue = pdt.equations.first().expression.getDouble();
                setDt = value != pvalue;
            }
        }
    }
    determineOrderInit("$init", s, localInit);
    determineOrderInit("$init", s, globalInit);
    singleton = s.isSingleton(true);
    populationCanGrowOrDie = s.lethalP || s.lethalType || s.canGrow();
    if (n != null && !singleton) {
        populationCanResize = globalMembers.contains(n);
        // See EquationSet.forceTemporaryStorageForSpecials() for a related issue.
        if (!populationCanResize && populationCanGrowOrDie && n.hasUsers()) {
            Backend.err.get().println("WARNING: $n can change (due to structural dynamics) but it was detected as a constant. Equations that depend on $n may give incorrect results.");
        }
    }
    if (index != null && !singleton) {
        indexNext = allocateGlobalFloat("indexNext");
        indexAvailable = allocateGlobalObject("indexAvailable");
    }
    if (// track instances
    singleton || s.connected || s.needInstanceTracking || populationCanResize) {
        // The reason populationCanResize forces use of the instances array is to enable pruning of parts when $n decreases.
        // The reason to "track instances" for a singleton is to allocate a slot for direct storage of the single instance in valuesObject.
        instances = allocateGlobalObject("instances");
        if (// in addition, track newly created instances
        s.connected) {
            if (!singleton)
                firstborn = allocateGlobalFloat("firstborn");
            newborn = allocateLocalFloat("newborn");
        }
    }
    // Just give name of connection part itself.
    if (s.connectionBindings != null) {
        singleConnection = true;
        for (ConnectionBinding cb : s.connectionBindings) {
            if (cb.endpoint.container != s.container || !cb.endpoint.isSingleton(true)) {
                singleConnection = false;
                break;
            }
        }
    }
    if (p != null) {
        Pdependencies = new ArrayList<Variable>();
        PdependenciesTemp = new ArrayList<Variable>();
        for (Variable t : s.ordered) {
            boolean temporary = t.hasAttribute("temporary");
            if ((temporary || localMembers.contains(t)) && p.dependsOn(t) != null) {
                Pdependencies.add(t);
                if (temporary)
                    PdependenciesTemp.add(t);
            }
        }
        determineOrderInit("$connect", s, Pdependencies);
        // determineOrderInit() is not needed for PdepenciesTemp, because temps are already in the correct order.
        // Default is no polling
        String pollString = "-1";
        if (p.metadata != null)
            pollString = p.metadata.getOrDefault(pollString, "poll");
        poll = new UnitValue(pollString).get();
        if (poll >= 0) {
            pollDeadline = allocateGlobalFloat("pollDeadline");
            pollSorted = allocateGlobalObject("pollSorted");
        }
    }
    if (type != null) {
        for (EquationEntry e : type.equations) {
            Split split = (Split) e.expression;
            split.index = type.reference.variable.container.splits.indexOf(split.parts);
        }
    }
    if (xyz != null) {
        XYZdependencies = new ArrayList<Variable>();
        XYZdependenciesTemp = new ArrayList<Variable>();
        for (Variable t : s.ordered) {
            boolean temporary = t.hasAttribute("temporary");
            if ((temporary || localMembers.contains(t)) && xyz.dependsOn(t) != null) {
                XYZdependencies.add(t);
                if (temporary)
                    XYZdependenciesTemp.add(t);
            }
        }
    }
    populationIndex = 0;
    if (// check for null specifically to guard against the Wrapper equation set (which is not fully constructed)
    s.container != null && s.container.parts != null) {
        populationIndex = s.container.parts.indexOf(s);
    }
    if (// connection-specific stuff
    s.connectionBindings != null) {
        int size = s.connectionBindings.size();
        // endpoints is allocated at the top of this function, because it is needed for reference handling in the variable analysis loop
        projectDependencies = new Object[size];
        projectReferences = new Object[size];
        count = new int[size];
        k = new Variable[size];
        max = new Variable[size];
        min = new Variable[size];
        project = new Variable[size];
        radius = new Variable[size];
        for (int i = 0; i < s.connectionBindings.size(); i++) {
            ConnectionBinding c = s.connectionBindings.get(i);
            count[i] = -1;
            k[i] = s.find(new Variable(c.alias + ".$k"));
            max[i] = s.find(new Variable(c.alias + ".$max"));
            min[i] = s.find(new Variable(c.alias + ".$min"));
            project[i] = s.find(new Variable(c.alias + ".$project"));
            radius[i] = s.find(new Variable(c.alias + ".$radius"));
            if (c.endpoint.accountableConnections != null) {
                AccountableConnection query = new AccountableConnection(s, c.alias);
                AccountableConnection ac = c.endpoint.accountableConnections.floor(query);
                if (// Only true if this endpoint is accountable.
                ac.equals(query)) {
                    // Allocate space for counter in target part
                    InternalBackendData endpointBed = (InternalBackendData) c.endpoint.backendData;
                    count[i] = endpointBed.allocateLocalFloat(s.prefix() + ".$count");
                    if (// $count is referenced explicitly, so need to finish setting it up
                    ac.count != null) {
                        ac.count.readIndex = ac.count.writeIndex = count[i];
                    }
                }
            }
            // Note that countLocalObject has already been incremented above
            namesLocalObject.add(c.alias);
            if (project[i] != null) {
                ArrayList<Variable> dependencies = new ArrayList<Variable>();
                // Always assign, even if empty.
                projectDependencies[i] = dependencies;
                for (Variable t : s.ordered) {
                    if (project[i].dependsOn(t) != null) {
                        dependencies.add(t);
                    }
                }
                final TreeSet<VariableReference> references = new TreeSet<VariableReference>();
                class ProjectVisitor implements Visitor {

                    public boolean visit(Operator op) {
                        if (op instanceof AccessVariable) {
                            AccessVariable av = (AccessVariable) op;
                            if (av.reference.resolution.size() > 0)
                                references.add(av.reference);
                            return false;
                        }
                        return true;
                    }
                }
                ProjectVisitor visitor = new ProjectVisitor();
                project[i].visit(visitor);
                for (Variable v : dependencies) v.visit(visitor);
                if (references.size() > 0)
                    projectReferences[i] = references;
            }
            c.resolution = translateResolution(c.resolution, s);
        }
    }
    // Locals
    for (Variable v : localMembers) {
        // in the object array rather than the float array.
        if (v.type instanceof Scalar && v.reference.variable == v) {
            v.readIndex = v.writeIndex = allocateLocalFloat(v.nameString());
        } else {
            v.readIndex = v.writeIndex = allocateLocalObject(v.nameString());
        }
    }
    for (Variable v : localBufferedExternal) {
        if (v.type instanceof Scalar && v.reference.variable == v) {
            v.writeIndex = allocateLocalFloat("next_" + v.nameString());
        } else {
            v.writeIndex = allocateLocalObject("next_" + v.nameString());
        }
    }
    for (Variable v : localBufferedInternal) {
        v.writeTemp = true;
        if (v.type instanceof Scalar && v.reference.variable == v) {
            v.writeIndex = allocateLocalTempFloat("next_" + v.nameString());
        } else {
            v.writeIndex = allocateLocalTempObject("next_" + v.nameString());
        }
    }
    // Globals
    for (Variable v : globalMembers) {
        if (v.type instanceof Scalar && v.reference.variable == v) {
            v.readIndex = v.writeIndex = allocateGlobalFloat(v.nameString());
        } else {
            v.readIndex = v.writeIndex = allocateGlobalObject(v.nameString());
        }
    }
    for (Variable v : globalBufferedExternal) {
        if (v.type instanceof Scalar && v.reference.variable == v) {
            v.writeIndex = allocateGlobalFloat("next_" + v.nameString());
        } else {
            v.writeIndex = allocateGlobalObject("next_" + v.nameString());
        }
    }
    for (Variable v : globalBufferedInternal) {
        v.writeTemp = true;
        if (v.type instanceof Scalar && v.reference.variable == v) {
            v.writeIndex = allocateGlobalTempFloat("next_" + v.nameString());
        } else {
            v.writeIndex = allocateGlobalTempObject("next_" + v.nameString());
        }
    }
    // fully temporary values
    for (Variable v : s.variables) {
        if (!v.hasAttribute("temporary"))
            continue;
        v.readTemp = v.writeTemp = true;
        if (v.hasAttribute("global")) {
            if (v.type instanceof Scalar && v.reference.variable == v) {
                v.readIndex = v.writeIndex = allocateGlobalTempFloat(v.nameString());
            } else {
                v.readIndex = v.writeIndex = allocateGlobalTempObject(v.nameString());
            }
        } else {
            if (v.type instanceof Scalar && v.reference.variable == v) {
                v.readIndex = v.writeIndex = allocateLocalTempFloat(v.nameString());
            } else {
                v.readIndex = v.writeIndex = allocateLocalTempObject(v.nameString());
            }
        }
    }
    if (live.hasAttribute("constant"))
        liveStorage = LIVE_CONSTANT;
    else if (live.hasAttribute("accessor"))
        liveStorage = LIVE_ACCESSOR;
    else
        // $live is "initOnly"
        liveStorage = LIVE_STORED;
    for (VariableReference r : localReference) r.resolution = translateResolution(r.resolution, s);
    for (VariableReference r : globalReference) r.resolution = translateResolution(r.resolution, s);
}
Also used : Operator(gov.sandia.n2a.language.Operator) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Visitor(gov.sandia.n2a.language.Visitor) AccessVariable(gov.sandia.n2a.language.AccessVariable) AccountableConnection(gov.sandia.n2a.eqset.EquationSet.AccountableConnection) VariableReference(gov.sandia.n2a.eqset.VariableReference) ConnectionBinding(gov.sandia.n2a.eqset.EquationSet.ConnectionBinding) ArrayList(java.util.ArrayList) UnitValue(gov.sandia.n2a.language.UnitValue) Scalar(gov.sandia.n2a.language.type.Scalar) TreeSet(java.util.TreeSet) Output(gov.sandia.n2a.language.function.Output) EquationEntry(gov.sandia.n2a.eqset.EquationEntry) Split(gov.sandia.n2a.language.Split)

Aggregations

Output (gov.sandia.n2a.language.function.Output)14 Operator (gov.sandia.n2a.language.Operator)13 Visitor (gov.sandia.n2a.language.Visitor)10 AccessVariable (gov.sandia.n2a.language.AccessVariable)9 Input (gov.sandia.n2a.language.function.Input)9 Constant (gov.sandia.n2a.language.Constant)8 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)7 Variable (gov.sandia.n2a.eqset.Variable)6 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)6 Add (gov.sandia.n2a.language.operator.Add)6 Text (gov.sandia.n2a.language.type.Text)6 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)4 Type (gov.sandia.n2a.language.Type)4 Matrix (gov.sandia.n2a.language.type.Matrix)4 EquationSet (gov.sandia.n2a.eqset.EquationSet)3 Scalar (gov.sandia.n2a.language.type.Scalar)3 InstanceTemporaries (gov.sandia.n2a.backend.internal.InstanceTemporaries)2 Population (gov.sandia.n2a.backend.internal.Population)2 Symbol (gov.sandia.n2a.backend.xyce.netlist.Symbol)2 XyceRenderer (gov.sandia.n2a.backend.xyce.netlist.XyceRenderer)2