use of gov.sandia.n2a.language.type.Instance in project n2a by frothga.
the class Part method init.
/**
* Note: specifically for Parts, call resolve() separately before calling init().
* This is to accommodate the connection process, which must probe values in a part
* (which may include references) before calling init().
*/
public void init(Simulator simulator) {
InstanceTemporaries temp = new InstanceInit(this, simulator);
InternalBackendData bed = temp.bed;
// update $n and assign $index
((Population) container.valuesObject[bed.populationIndex]).insert(this);
// Note: these do not require resolve(). Instead, they access their target directly through the endpoints array.
if (bed.count != null) {
int length = bed.count.length;
for (int i = 0; i < length; i++) {
if (bed.count[i] >= 0) {
Part p = (Part) valuesObject[bed.endpoints + i];
p.valuesFloat[bed.count[i]]++;
}
}
}
// Initialize variables
// Note that some valuesObject entries could be left null. This is OK, because Instance.get() will return
// a zero-equivalent value if it finds null.
// So our intial values can be applied correctly.
clearExternalWriteInit(bed.localBufferedExternalWrite);
for (Variable v : bed.localInit) {
Type result = v.eval(temp);
if (result == null || v.reference.variable.writeIndex < 0)
continue;
// with its finish() step. It would be as if the part had an extra cycle inserted.
if (v.reference.variable == v)
temp.applyResultInit(v, result);
else
((Instance) valuesObject[v.reference.index]).applyResultInit(v.reference.variable, result);
}
if (bed.liveStorage == InternalBackendData.LIVE_STORED)
set(bed.live, new Scalar(1));
if (bed.lastT != null)
temp.setFinal(bed.lastT, new Scalar(simulator.currentEvent.t));
if (bed.type != null)
temp.setFinal(bed.type, new Scalar(0));
if (bed.setDt)
simulator.move(this, ((Scalar) bed.dt.type).value);
// Prepare variables that have a combiner, in case they get written before the first finish().
// skips REPLACE it because it is unnecessary when called from update(). Handle REPLACE separately ...
clearExternalWriteBuffers(bed.localBufferedExternalWrite);
// This must come after the variables are initialized. Otherwise, there is no point.
for (Variable v : bed.localBufferedExternalWrite) if (v.assignment == Variable.REPLACE)
temp.set(v, temp.get(v));
// Request event monitors
for (EventTarget et : bed.eventTargets) {
for (EventSource es : et.sources) {
Part source;
if (es.reference == null)
source = this;
else
source = (Part) valuesObject[es.reference.index];
@SuppressWarnings("unchecked") ArrayList<Instance> monitors = (ArrayList<Instance>) source.valuesObject[es.monitorIndex];
monitors.add(this);
}
}
if (equations.parts.size() > 0) {
// If there are parts at all, then orderedParts must be filled in correctly. Otherwise it may be null.
for (EquationSet s : equations.orderedParts) ((Population) valuesObject[s.priority]).init(simulator);
}
}
use of gov.sandia.n2a.language.type.Instance in project n2a by frothga.
the class SumSquares method determineExponent.
public void determineExponent(ExponentContext context) {
// This function is related to both Norm and Power. See those classes for similar processing.
// A
Operator op0 = operands[0];
op0.determineExponent(context);
// Determine number of elements in A
Instance instance = new Instance() {
// all AccessVariable objects will reach here first, and get back the Variable.type field
public Type get(VariableReference r) throws EvaluationException {
return r.variable.type;
}
};
Matrix A = (Matrix) op0.eval(instance);
int Asize = A.rows() * A.columns();
if (op0.exponent != UNKNOWN) {
// log2(Asize)
int shift = (int) Math.floor(Math.log(Asize) / Math.log(2));
int centerNew = MSB / 2;
int exponentNew = op0.centerPower() * 2 + shift + MSB - centerNew;
updateExponent(context, exponentNew, centerNew);
}
}
Aggregations