package simulation;
import java.io.File;
import java.util.concurrent.Callable;
import miscellaneous.DirectoryManager;
import miscellaneous.LSVList;
import reaction.Reaction;
import recorders.Recorder;
import tables.DimensionMap;
import vsv.VSVModel;
import weka.core.converters.JEXTableWriter;
public class TestSim implements Callable<Integer> {
public VSVModel model;
public DimensionMap params;
public double t, duration, dt, dtRecorder;
public File outputFile = null;
// Results Stuff
public Recorder recorder = new Recorder();
public TestSim()
{
this.outputFile = new File(DirectoryManager.getUniqueAbsoluteTempPath(JEXTableWriter.ARFF_FILE));
}
public static void main(String[] args)
{
Recorder.setHostDirectory("/Users/jaywarrick/Desktop/JavaModelOutputs");
// Recorder.setHostDirectory("C:\\Users\\ctimm\\Desktop\\JavaModelOutputs");
TestSim sim = new TestSim();
VSVModel model = new VSVModel(); // initializes the model (cell, virus and reactions)
model.initialize();
sim.setParams(model, 6 * 60 * 60, 1.0, 15 * 60); // sets the simulation parameters
sim.call();
}
/**
*
* @param model2
* @param duration
* total infection time
* @param dt
* time increment
* @param dtRecorder
* recorded time increment
* @param outputPath
*
* @questions What does mature fnc do? Why do you mature everything in the pool? What is madeit? a parameter to indicate where the simulation stopped?
*/
public void setParams(VSVModel model, double duration, double dt, double dtRecorder)
{
this.model = model;
this.duration = duration;
this.dt = dt;
this.dtRecorder = dtRecorder;
// Print the reactions used
LSVList reactions = new LSVList();
reactions.add(" ");
for (Reaction r : model.rxns)
{
reactions.add(r.toString());
}
Recorder.log(reactions.toString(), this);
}
@Override
public Integer call()
{
return this.simulate_RK4();
}
/**
* Runs the simulation
*
* @param duration
* @param dt
* @param dtRecorder
*/
public Integer simulate_Euler()
{
this.recorder.reset();
// Simulation
this.t = 0;
while (this.t < this.duration)
{
// Add matured items to the pool
this.model.cell.mature(this.t);
double decay = this.model.cellDecay(this.t);
// records the amounts of items in the cell pools at given time
this.recorder.record(this.duration, this.t, this.model.cell, this.dtRecorder, "TestSim", decay);
if(this.t % 100 == 0)
{
double madeIt = this.t;
this.t = madeIt;
}
// calculate rates based on each reaction in the simulation
for (Reaction rxn : this.model.rxns)
{
rxn.calculateBaseRate();
rxn.applyStoichAndRate();
}
this.model.cell.simpleLinearIntegrate(this.dt, this.t, decay);
// Increment time here. This way...
// time 0 is recorded initially at beginning of loop (after harvesting mature items)
// t is current time for calculating delays (i.e. maturation time = currentTime + delay) during integration
// t is incremented dt for next loop
this.t = this.t + this.dt;
}
this.recorder.saveData(this.duration, this.dt, this.outputFile);
this.model = null;
this.recorder = null;
return 0;
}
/**
* Runs the simulation
*
* @param duration
* @param dt
* @param dtRecorder
*/
public Integer simulate_RK4()
{
this.recorder.reset();
// Simulation
this.t = 0;
while (this.t < this.duration)
{
// Add matured items to the pool
this.model.cell.mature(this.t);
double decay = this.model.cellDecay(this.t);
// records the amounts of items in the cell pools at given time
this.recorder.record(this.duration, this.t, this.model.cell, this.dtRecorder, "TestSim", decay);
VSVModel modelk1 = this.getModelk1(this.model);
this.model.cell.addRates(modelk1.cell, 1.0 / 6.0);
VSVModel modelk2 = this.getModelk2(modelk1);
modelk1 = null;
this.model.cell.addRates(modelk2.cell, 2.0 / 6.0);
VSVModel modelk3 = this.getModelk3(modelk2);
modelk2 = null;
this.model.cell.addRates(modelk3.cell, 2.0 / 6.0);
VSVModel modelk4 = this.getModelk4(modelk3);
modelk3 = null;
this.model.cell.addRates(modelk4.cell, 1.0 / 6.0);
modelk4 = null;
this.model.cell.simpleLinearIntegrate(this.dt, this.t, decay);
// Increment time here. This way...
// time 0 is recorded initially at beginning of loop (after harvesting mature items)
// t is current time for calculating delays (i.e. maturation time = currentTime + delay) during integration
// t is incremented dt for next loop
this.t = this.t + this.dt;
}
this.recorder.saveData(this.duration, this.dt, this.outputFile);
this.model = null;
this.recorder = null;
return 0;
}
private VSVModel getModelk1(VSVModel initialModel)
{
VSVModel modelk1 = this.model.copy();
modelk1.cell.clearRates();
// calculate rates based on each reaction in the simulation
for (Reaction rxn : modelk1.rxns)
{
rxn.calculateBaseRate();
rxn.applyStoichAndRate();
}
return modelk1;
}
private VSVModel getModelk2(VSVModel modelk1)
{
double decay = modelk1.cellDecay(this.t);
modelk1.cell.simpleLinearIntegrate(this.dt / 2, this.t, decay);
modelk1.cell.mature(this.t);
// calculate rates based on each reaction in the simulation
for (Reaction rxn : modelk1.rxns)
{
rxn.calculateBaseRate();
rxn.applyStoichAndRate();
}
return modelk1;
}
private VSVModel getModelk3(VSVModel modelk2)
{
VSVModel modelk3 = this.model.copy();
modelk3.cell.setRates(modelk2.cell);
double decay = modelk3.cellDecay(this.t);
modelk3.cell.simpleLinearIntegrate(this.dt / 2, this.t, decay);
modelk3.cell.mature(this.t);
// calculate rates based on each reaction in the simulation
for (Reaction rxn : modelk3.rxns)
{
rxn.calculateBaseRate();
rxn.applyStoichAndRate();
}
return modelk3;
}
private VSVModel getModelk4(VSVModel modelk3)
{
VSVModel modelk4 = this.model.copy();
modelk4.cell.setRates(modelk3.cell);
double decay = modelk4.cellDecay(this.t);
modelk4.cell.simpleLinearIntegrate(this.dt, this.t, decay);
modelk4.cell.mature(this.t);
// calculate rates based on each reaction in the simulation
for (Reaction rxn : modelk4.rxns)
{
rxn.calculateBaseRate();
rxn.applyStoichAndRate();
}
return modelk4;
}
}
// ////////////////////////////////////////////////////////////////////////////////////////
// OLD METHODS
// //////////////////////////////////////////////////////////////////////////////////////
// public void recordData(double t, double dtRecorder)
// {
// if(t/dtRecorder >= ticker + 1 || t == 0.0)
// {
// // update the ticker
// ticker = (int) (t/dtRecorder);
//
// // update the available numbers
// SimplePool temp = this.pool.copy();
// this.total.put(t, temp);
//
// // // update the total numbers
// // temp = temp.copy();
// // temp.add(reactantDelayPool.getAll());
// // this.total.put(t, temp);
// }
// }
// private void calculateCumulativeRates()
// {
// this.reactantRates.clear();
// this.productRates.clear();
//
// // get the rate of usage of each reactant for all the reactions in this
// simulation
// for(Reaction reaction : reactions)
// {
// // Calculate the rates of molecule usage/production for each molecule in the
// reaction based on available reactants
// reaction.calculateRates(this.pool);
//
// // dA/dt_RXN1 + dA/dt_RXN2 + ... + dA/dt_RXNn for each molecule (reactant or
// product)
// // Reactants
// this.reactantRates = Reaction.addMaps(this.reactantRates,
// reaction.reactantRates);
//
// //Products
// this.productRates = Reaction.addMaps(this.productRates,
// reaction.productRates);
// }
// }
// public String getResults()
// {
// LSVList ret = new LSVList();
// for(SimplePool result : this.total.values())
// {
// CSVList line = new CSVList();
// line.add("time");
// for(String reactant : result.keySet())
// {
// line.add(""+reactant);
// }
// ret.add(line.toString());
// break;
// }
// for(Entry<Double,SimplePool> result : this.total.entrySet())
// {
// CSVList line = new CSVList();
// line.add(result.getKey().toString());
// for(String reactant : result.getValue().keySet())
// {
// line.add(""+result.getValue().get(reactant));
// }
// ret.add(line.toString());
// }
// return ret.toString();
// }