/// <summary> /// Initializes a cell model for simulation solving /// </summary> /// <param name="solverMethod"> /// A <see cref="Solver.SolverMethods"/> /// </param> /// <param name="parameters"> /// A <see cref="SimulationParameters"/> /// </param> public unsafe void InitializeCellModel(Solver.SolverMethods solverMethod, SimulationParameters parameters) { // Find out number of variable species int n = 0; foreach (Species s in this.species) { if (!s.BoundaryCondition) { n++; } } this.N = n; // Cvode method if (solverMethod == Solver.SolverMethods.CVode_BDF_Newton || solverMethod == Solver.SolverMethods.CVode_Adams_Moulton) { double[] values = new double[n]; double[] absoluteTolerances = new double[n]; if (solverMethod == Solver.SolverMethods.CVode_BDF_Newton) { parameters.CvodeType = 2; } else { parameters.CvodeType = 1; } // Set initial values foreach (Species s in this.species) { if (!s.BoundaryCondition) { // collect the initial values values[speciesToIndexMap[s.ID]] = s.initialValue; // collect the absolute tolerances absoluteTolerances[speciesToIndexMap[s.ID]] = s.AbsoluteTolerance; } } // Create CVODE interface object this.solver = new Solver.CVode(n, values, new Solver.CVode.ModelFunction(delegate(double time, IntPtr y, IntPtr ydot, IntPtr fdata) { double* yp = (double*)y.ToPointer(); double* ydotp = (double*)ydot.ToPointer(); int i; double[] data = new double[this.N]; for (i = 0; i < this.N; i++) { data[i] = yp[i]; } double[] result = this.ModelDeltaFunction(time, data); // Get the values out for (i = 0; i < this.N; i++) { ydotp[i] = result[i]; } return 0; }), absoluteTolerances, parameters.RelativeTolerance, parameters.CvodeType); } else if (solverMethod == Solver.SolverMethods.Euler) { double[] values = new double[n]; // Set initial values foreach (Species s in this.species) { if (!s.BoundaryCondition) { // collect the initial values values[speciesToIndexMap[s.ID]] = s.initialValue; } } this.solver = new Solver.Euler(n, values, this.ModelDeltaFunction); } else if (solverMethod == Solver.SolverMethods.RungeKutta) { double[] values = new double[n]; // Set initial values foreach (Species s in this.species) { if (!s.BoundaryCondition) { // collect the initial values values[speciesToIndexMap[s.ID]] = s.initialValue; } } this.solver = new Solver.RungeKutta(n, values, this.ModelDeltaFunction); } }
/// <summary> /// Secondary constructor that takes a cell definition, /// a spatial context, a list of species and a species variables dictionary /// </summary> /// <param name="cellDefinition"> /// A <see cref="CellDefinition"/> /// </param> /// <param name="spatialContext"> /// A <see cref="SpatialContext"/> /// </param> /// <param name="species"> /// A <see cref="List`1"/> /// </param> /// <param name="speciesVariables"> /// A <see cref="Dictionary`2"/> /// </param> public CellInstance(CellDefinition cellDefinition, SpatialContext spatialContext, List<SBML.Species> species, Dictionary<string, double> speciesVariables, Dictionary<int, string> indexToSpeciesMap, Dictionary<string, int> speciesToIndexMap, Solver.SolverBase solver, List<SBML.ExtracellularComponents.ComponentWorldStateBase> components, System.Random random) { this.cellInstanceDefinition = cellDefinition; this.species = species; this.speciesVariables = speciesVariables; this.cellInstanceSpatialContext = spatialContext; this.indexToSpeciesMap = indexToSpeciesMap; this.speciesToIndexMap = speciesToIndexMap; this.solver = solver; this.components = components; this.randomObject = random; this.localSpeciesVariables = new Dictionary<string, double>(); this.localSpeciesDelta = new Dictionary<string, double>(); }