Search in sources :

Example 11 with Scalar

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

the class Part method integrate.

public void integrate(Simulator simulator) {
    InternalBackendData bed = (InternalBackendData) equations.backendData;
    int populations = equations.parts.size();
    // nothing to do
    if (bed.localIntegrated.isEmpty() && populations == 0)
        return;
    double dt;
    if (bed.lastT == null)
        dt = ((EventStep) simulator.currentEvent).dt;
    else
        dt = simulator.currentEvent.t - ((Scalar) get(bed.lastT)).value;
    // nothing to do
    if (dt <= 0)
        return;
    // Integrate variables
    for (Variable v : bed.localIntegrated) {
        double a = ((Scalar) get(v)).value;
        double aa = ((Scalar) get(v.derivative)).value;
        setFinal(v, new Scalar(a + aa * dt));
    }
    for (int i = 0; i < populations; i++) ((Population) valuesObject[i]).integrate(simulator, dt);
}
Also used : Variable(gov.sandia.n2a.eqset.Variable) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 12 with Scalar

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

the class Part method getP.

public double getP(Simulator simulator) {
    InstancePreLive temp = new InstancePreLive(this, simulator);
    // N2A language defines default to be 1 (always create)
    if (temp.bed.p == null)
        return 1;
    for (Variable v : temp.bed.Pdependencies) {
        Type result = v.eval(temp);
        if (result != null && v.writeIndex >= 0)
            temp.set(v, result);
    }
    Type result = temp.bed.p.eval(temp);
    if (result == null)
        return 1;
    return ((Scalar) result).value;
}
Also used : Type(gov.sandia.n2a.language.Type) Variable(gov.sandia.n2a.eqset.Variable) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 13 with Scalar

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

the class Gaussian method eval.

public Type eval(Instance context) throws EvaluationException {
    Random random;
    Simulator simulator = Simulator.getSimulator(context);
    if (simulator == null)
        random = new Random();
    else
        random = simulator.random;
    if (operands.length == 0)
        return new Scalar(random.nextGaussian());
    Type sigma = operands[0].eval(context);
    if (sigma instanceof Scalar) {
        return new Scalar(random.nextGaussian() * ((Scalar) sigma).value);
    } else if (sigma instanceof Matrix) {
        Matrix scale = (Matrix) sigma;
        int rows = scale.rows();
        int columns = scale.columns();
        if (columns == 1) {
            Matrix result = new MatrixDense(rows, 1);
            for (int i = 0; i < rows; i++) result.set(i, random.nextGaussian() * scale.get(i, 0));
            return result;
        } else if (rows == 1) {
            Matrix result = new MatrixDense(columns, 1);
            for (int i = 0; i < columns; i++) result.set(i, random.nextGaussian() * scale.get(0, i));
            return result;
        } else {
            Matrix temp = new MatrixDense(columns, 1);
            for (int i = 0; i < columns; i++) temp.set(i, random.nextGaussian());
            return sigma.multiply(temp);
        }
    } else {
        // We could throw an exception, but this is easy enough.
        return new Scalar(random.nextGaussian());
    }
}
Also used : Type(gov.sandia.n2a.language.Type) Matrix(gov.sandia.n2a.language.type.Matrix) Random(java.util.Random) MatrixDense(gov.sandia.n2a.language.type.MatrixDense) Simulator(gov.sandia.n2a.backend.internal.Simulator) Scalar(gov.sandia.n2a.language.type.Scalar)

Example 14 with Scalar

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

Example 15 with Scalar

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

the class Input method getRow.

public Holder getRow(Instance context, Type op1, boolean time) {
    Simulator simulator = Simulator.getSimulator(context);
    // 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 {
        // get an input holder
        String path = ((Text) operands[0].eval(context)).value;
        H = simulator.inputs.get(path);
        if (H == null) {
            H = new Holder();
            if (// not ideal; reading stdin should be reserved for headless operation
            path.isEmpty())
                // not ideal; reading stdin should be reserved for headless operation
                H.stream = new BufferedReader(new InputStreamReader(System.in));
            else
                H.stream = new BufferedReader(new FileReader(new File(path).getAbsoluteFile()));
            // sqrt (epsilon for time representation (currently double)), about 1e-8
            H.epsilon = Math.sqrt(Math.ulp(1.0));
            if (simulator.currentEvent instanceof EventStep)
                H.epsilon = Math.min(H.epsilon, ((EventStep) simulator.currentEvent).dt / 1000);
            simulator.inputs.put(path, H);
        }
        if (op1 instanceof Scalar)
            H.getRow(((Scalar) op1).value, time);
        else
            H.getRow(0, time);
    } catch (IOException e) {
        return null;
    }
    return H;
}
Also used : InputStreamReader(java.io.InputStreamReader) EventStep(gov.sandia.n2a.backend.internal.EventStep) BufferedReader(java.io.BufferedReader) Text(gov.sandia.n2a.language.type.Text) FileReader(java.io.FileReader) IOException(java.io.IOException) Simulator(gov.sandia.n2a.backend.internal.Simulator) File(java.io.File) Scalar(gov.sandia.n2a.language.type.Scalar)

Aggregations

Scalar (gov.sandia.n2a.language.type.Scalar)42 Type (gov.sandia.n2a.language.Type)17 AccessVariable (gov.sandia.n2a.language.AccessVariable)14 Variable (gov.sandia.n2a.eqset.Variable)13 Matrix (gov.sandia.n2a.language.type.Matrix)11 Constant (gov.sandia.n2a.language.Constant)10 Operator (gov.sandia.n2a.language.Operator)10 Text (gov.sandia.n2a.language.type.Text)8 EquationSet (gov.sandia.n2a.eqset.EquationSet)6 ArrayList (java.util.ArrayList)6 Simulator (gov.sandia.n2a.backend.internal.Simulator)5 Instance (gov.sandia.n2a.language.type.Instance)5 MatrixDense (gov.sandia.n2a.language.type.MatrixDense)5 TreeSet (java.util.TreeSet)5 EventSource (gov.sandia.n2a.backend.internal.InternalBackendData.EventSource)4 EventTarget (gov.sandia.n2a.backend.internal.InternalBackendData.EventTarget)4 EquationEntry (gov.sandia.n2a.eqset.EquationEntry)3 VariableReference (gov.sandia.n2a.eqset.VariableReference)3 ConnectionBinding (gov.sandia.n2a.eqset.EquationSet.ConnectionBinding)2 EvaluationException (gov.sandia.n2a.language.EvaluationException)2