-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleSolver_constDerivative.cs
107 lines (92 loc) · 3.57 KB
/
SimpleSolver_constDerivative.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.IO;
using System;
using UnityEngine;
// These are the MathNet Numerics Libraries needed
// They need to dragged and dropped into the Unity assets plugins folder!
using SparseMatrix = MathNet.Numerics.LinearAlgebra.Double.SparseMatrix;
using Matrix = MathNet.Numerics.LinearAlgebra.Matrix<double>;
using Vector = MathNet.Numerics.LinearAlgebra.Vector<double>;
using Double = MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.Data.Text;
using solvers = MathNet.Numerics.LinearAlgebra.Double.Solvers;
using C2M2.NeuronalDynamics.UGX;
using Grid = C2M2.NeuronalDynamics.UGX.Grid;
namespace C2M2.NeuronalDynamics.Simulation
{
public class SimpleSolver_constDerivative : NDSimulation
{
//Set cell biological constants
public const double res = 10.0;
public const double gk = 36.0;
public const double gna = 120.0;
public const double gl = 0.3;
public const double ek = -12.0;
public const double ena = 220; //70;//112.0;
public const double el = 0.6;
public const double cap = 0.09;
public const double ni = 0.5, mi = 0.4, hi = 0.2; //state probabilities
public const double vstart = 55;
private Vector U;
public override float GetSimulationTime() => i * (float)k;
double k;
// Keep track of i locally so that we know which simulation frame to send to other scripts
private int i = -1;
public override double[] Get1DValues()
{
//return (U != null && i > -1) ? U.SubMatrix(0, U.RowCount, i, 1).ToColumnMajorArray() : null;
Debug.Log("Here is state: " + U.ToString());
//i = i + 1;
return (U != null) ? U.SubVector(0, NeuronCell.vertCount).ToArray() : null;
}
// Receive new simulation 1D index/value pairings
public override void Set1DValues(Tuple<int, double>[] newValues)
{
foreach (Tuple<int, double> newVal in newValues)
{
int j = newVal.Item1;
double val = newVal.Item2 * vstart;
U[j] += val;
}
}
protected override void Solve()
{
InitNeuronCell();
int numVert = NeuronCell.vertCount;
int nT = 9000;
double endTime = 25;
k = endTime / nT;
// I am beginning to wonder if the for loop is even necessary
// Maybe do a solve on every timestep call to solve and increment i by 1
//This for loop is for solving the simple ode dV/dt = 1;
//for (i = 0; i < nT; i++)
//{
Debug.Log("Hello" + U.ToString());
// Forward Euler Solve for Vnext = Vcurr+ k*f(Vcurr)
// Here f(V) = 1;
U.Add(k, U);
i = i + 1;
//}
}
#region Local Functions
private void InitNeuronCell()
{
U = Vector.Build.Dense(NeuronCell.vertCount);
U.SetSubVector(0, NeuronCell.vertCount, ic(NeuronCell.vertCount));
//U = Vector.Build.Dense(myCell.vertCount);
}
private static Vector ic(int size)
{
double[] outV = new double[size];
for (int j = 0; j < size; j++)
{
outV[j] = 0.002;
}
return Vector.Build.Dense(outV);
}
#endregion
}
}