use of gov.sandia.n2a.language.type.Scalar 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;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Part method init.
/**
* Note: specifically for Parts, call resolve() 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 InstanceTemporaries(this, simulator, true);
// update $n and assign $index
((Population) container.valuesObject[temp.bed.populationIndex]).insert(this);
// Note: these do not require resolve(). Instead, they access their target directly through the endpoints array.
if (temp.bed.count != null) {
int length = temp.bed.count.length;
for (int i = 0; i < length; i++) {
if (temp.bed.count[i] >= 0) {
Part p = (Part) valuesObject[temp.bed.endpoints + i];
p.valuesFloat[temp.bed.count[i]]++;
}
}
}
// force $live to be set before anything else
if (temp.bed.liveStorage == InternalBackendData.LIVE_STORED)
set(temp.bed.live, new Scalar(1));
for (Variable v : temp.bed.localInitSpecial) {
Type result = v.eval(temp);
if (result != null && v.writeIndex >= 0)
temp.set(v, result);
// Note that some valuesObject entries may be left null. This is OK, because Instance.get() will return
// a zero-equivalent value if it finds null. Ditto for non-$variables below.
}
for (Variable v : temp.bed.localBufferedSpecial) {
temp.setFinal(v, temp.getFinal(v));
}
if (temp.bed.lastT != null)
temp.setFinal(temp.bed.lastT, new Scalar(simulator.currentEvent.t));
// non-$variables
for (Variable v : temp.bed.localInitRegular) {
Type result = v.eval(temp);
if (result != null && v.writeIndex >= 0)
temp.set(v, result);
}
for (Variable v : temp.bed.localBufferedRegular) {
temp.setFinal(v, temp.getFinal(v));
}
// v.type should be pre-loaded with zero-equivalent values
for (Variable v : temp.bed.localBufferedExternalWrite) set(v, v.type);
// Request event monitors
for (EventTarget et : temp.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);
}
}
int populations = equations.parts.size();
for (int i = 0; i < populations; i++) ((Population) valuesObject[i]).init(simulator);
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Population method finish.
public boolean finish(Simulator simulator) {
InternalBackendData bed = (InternalBackendData) equations.backendData;
// This work is split between here and after the finalize code below.
if (bed.populationCanResize && bed.populationCanGrowOrDie && bed.n.derivative == null) {
double oldN = ((Scalar) get(bed.n)).value;
double newN = ((Scalar) getFinal(bed.n)).value;
if (// $n was explicitly changed, so its value takes precedence
newN != oldN)
// $n was explicitly changed, so its value takes precedence
simulator.resize(this, (int) newN);
else
// -1 means to update $n from this.n. This can only be done after other parts are finalized, as they may impose structural dynamics via $p or $type.
simulator.resize(this, -1);
}
for (Variable v : bed.globalBufferedExternal) setFinal(v, getFinal(v));
for (Variable v : bed.globalBufferedExternalWrite) {
switch(v.assignment) {
case Variable.ADD:
// initial value is zero-equivalent (additive identity)
set(v, v.type);
break;
case Variable.MULTIPLY:
case Variable.DIVIDE:
// multiplicative identity
if (v.type instanceof Matrix)
set(v, ((Matrix) v.type).identity());
else
set(v, new Scalar(1));
break;
case Variable.MIN:
if (v.type instanceof Matrix)
set(v, ((Matrix) v.type).clear(Double.POSITIVE_INFINITY));
else
set(v, new Scalar(Double.POSITIVE_INFINITY));
break;
case Variable.MAX:
if (v.type instanceof Matrix)
set(v, ((Matrix) v.type).clear(Double.NEGATIVE_INFINITY));
else
set(v, new Scalar(Double.NEGATIVE_INFINITY));
break;
}
}
if (bed.populationCanResize) {
// This is the finalized value of $n.
int requestedN = (int) ((Scalar) get(bed.n)).value;
if (bed.populationCanGrowOrDie) {
if (// $n' exists
bed.n.derivative != null) {
// the rate of change in $n is pre-determined, so it relentlessly overrides any other structural dynamics
simulator.resize(this, requestedN);
}
} else // $n is the only kind of structural dynamics, so only do a resize() when needed
{
if (requestedN != n)
simulator.resize(this, requestedN);
}
}
return true;
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Population method integrate.
public void integrate(Simulator simulator, double dt) {
InternalBackendData bed = (InternalBackendData) equations.backendData;
for (Variable v : bed.globalIntegrated) {
double a = ((Scalar) get(v)).value;
double aa = ((Scalar) get(v.derivative)).value;
setFinal(v, new Scalar(a + aa * dt));
}
}
use of gov.sandia.n2a.language.type.Scalar in project n2a by frothga.
the class Population method insert.
@SuppressWarnings("unchecked")
public void insert(Part p) {
n++;
InternalBackendData bed = (InternalBackendData) equations.backendData;
if (bed.index != null) {
int index;
if (valuesObject[bed.indexAvailable] == null) {
index = (int) valuesFloat[bed.indexNext]++;
} else {
ArrayList<Integer> availableIndex = (ArrayList<Integer>) valuesObject[bed.indexAvailable];
index = availableIndex.remove(availableIndex.size() - 1);
if (availableIndex.size() < 1)
valuesObject[bed.indexAvailable] = null;
}
p.set(bed.index, new Scalar(index));
if (bed.instances >= 0) {
ArrayList<Part> instances = (ArrayList<Part>) valuesObject[bed.instances];
if (instances == null) {
instances = new ArrayList<Part>(index + 1);
valuesObject[bed.instances] = instances;
}
for (int size = instances.size(); size <= index; size++) instances.add(null);
instances.set(index, p);
if (equations.connected) {
p.valuesFloat[bed.newborn] = 1;
valuesFloat[bed.firstborn] = Math.min(valuesFloat[bed.firstborn], index);
}
}
}
}
Aggregations