Search in sources :

Example 16 with Constant

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

the class OR 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)
                return operand1;
            else
                return new Constant(new Scalar(1));
        }
    } 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)
                return operand0;
            else
                return new Constant(new Scalar(1));
        }
    }
    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 17 with Constant

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

the class Scalar method convert.

/**
 *        General utility: given a string containing a number with units, convert to the scaled SI value.
 */
public static double convert(String expression) {
    try {
        Operator op = Operator.parse(expression);
        double sign = 1;
        if (op instanceof Negate) {
            op = ((Negate) op).operand;
            sign = -1;
        }
        if (!(op instanceof Constant))
            return 0;
        Type result = ((Constant) op).value;
        if (result instanceof Scalar)
            return ((Scalar) result).value * sign;
    } catch (ParseException e) {
    }
    return 0;
}
Also used : Operator(gov.sandia.n2a.language.Operator) Type(gov.sandia.n2a.language.Type) Constant(gov.sandia.n2a.language.Constant) Negate(gov.sandia.n2a.language.operator.Negate) ParseException(gov.sandia.n2a.language.ParseException)

Example 18 with Constant

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

the class ExportJob method genericPart.

public void genericPart(MPart part, Element result, String... skip) {
    EquationSet partEquations = getEquations(part);
    List<Element> resultElements = new ArrayList<Element>();
    List<String> skipList = Arrays.asList(skip);
    for (MNode c : part) {
        MPart p = (MPart) c;
        String key = p.key();
        // Skip connection bindings. They are unpacked elsewhere.
        if (partEquations.findConnection(key) != null)
            continue;
        if (key.startsWith("$"))
            continue;
        if (skipList.contains(key))
            continue;
        if (p.isPart()) {
            genericPart(p, resultElements);
        } else {
            // We need to check two things:
            // * Has the variable been overridden after it was declared in the base part?
            // * Is it an expression or a constant?
            // An override that is an expression should trigger a LEMS extension part.
            // A constant that is either overridden or required should be emitted here.
            boolean expression = true;
            String value = p.get();
            Variable v = partEquations.find(new Variable(key));
            try {
                // This could convert an expression to a constant.
                Type evalue = v.eval(context);
                // TODO: if it is a simple AccessVariable, then it shouldn't be viewed as an expression.
                Operator op = Operator.parse(value);
                if (op instanceof Constant) {
                    // "direct" value
                    Type dvalue = ((Constant) op).value;
                    expression = !evalue.equals(dvalue);
                }
            } catch (Exception e) {
            }
            boolean overridden = p.isFromTopDocument() || isOverride(part.get("$inherit").replace("\"", ""), key);
            String name = sequencer.bestFieldName(result, p.get("$metadata", "backend.lems.param"));
            if (name.isEmpty())
                name = key;
            boolean required = sequencer.isRequired(result, name);
            if (required || (overridden && !expression)) {
                // biophysicalUnits() should return strings (non-numbers) unmodified
                result.setAttribute(name, biophysicalUnits(p.get()));
            }
        }
    }
    sequencer.append(result, resultElements);
}
Also used : Operator(gov.sandia.n2a.language.Operator) EquationSet(gov.sandia.n2a.eqset.EquationSet) MPart(gov.sandia.n2a.eqset.MPart) AccessVariable(gov.sandia.n2a.language.AccessVariable) Variable(gov.sandia.n2a.eqset.Variable) Constant(gov.sandia.n2a.language.Constant) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MNode(gov.sandia.n2a.db.MNode) IncommensurableException(javax.measure.IncommensurableException) UnconvertibleException(javax.measure.UnconvertibleException) ParseException(gov.sandia.n2a.language.ParseException) Type(gov.sandia.n2a.language.Type)

Example 19 with Constant

use of gov.sandia.n2a.language.Constant 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

Constant (gov.sandia.n2a.language.Constant)19 Operator (gov.sandia.n2a.language.Operator)14 AccessVariable (gov.sandia.n2a.language.AccessVariable)11 Scalar (gov.sandia.n2a.language.type.Scalar)10 Type (gov.sandia.n2a.language.Type)9 Text (gov.sandia.n2a.language.type.Text)6 Visitor (gov.sandia.n2a.language.Visitor)5 EquationSet (gov.sandia.n2a.eqset.EquationSet)4 Variable (gov.sandia.n2a.eqset.Variable)4 ParseException (gov.sandia.n2a.language.ParseException)3 Input (gov.sandia.n2a.language.function.Input)3 Output (gov.sandia.n2a.language.function.Output)3 BuildMatrix (gov.sandia.n2a.language.BuildMatrix)2 Function (gov.sandia.n2a.language.Function)2 ReadMatrix (gov.sandia.n2a.language.function.ReadMatrix)2 Add (gov.sandia.n2a.language.operator.Add)2 TreeSet (java.util.TreeSet)2 MNode (gov.sandia.n2a.db.MNode)1 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)1 MPart (gov.sandia.n2a.eqset.MPart)1