Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions Assets/Scripts/C2M2/CellData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class CellData
public int refinementLevel;
public double timeStep;
public double endTime;
// Gating variable data
public GatingVariableData[] gates;
// Ion‐channel data
public ChannelData[] channels;

// Clamp data
[System.Serializable]
Expand All @@ -35,14 +39,22 @@ public struct ClampData

// Simulation state
public double[] U;
public double[] M;
public double[] N;
public double[] H;

public double[] Upre;
public double[] Mpre;
public double[] Npre;
public double[] Hpre;

// Gating Variables
[System.Serializable]
public class GatingVariableData {
public string name;
public double[] current;
public double[] previous;
}

// Ion Channels
[System.Serializable]
public class ChannelData {
public string name;
public bool isActive;
}

// Graph data
[System.Serializable]
Expand Down
10 changes: 3 additions & 7 deletions Assets/Scripts/C2M2/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ public class GameManager : MonoBehaviour

// vectors used in SparseSolve; for loading
public double[] U;
public double[] M;
public double[] N;
public double[] H;

public double[] Upre;
public double[] Mpre;
public double[] Npre;
public double[] Hpre;
public Dictionary<string, double[]> currentStates;
public Dictionary<string, double[]> previousStates;


// for loading a file
private bool loading = false;
Expand Down
78 changes: 61 additions & 17 deletions Assets/Scripts/C2M2/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using TMPro;

Expand Down Expand Up @@ -139,14 +140,34 @@ public void Save()
data = new CellData();

data.U = sim.Get1DValues(); // voltage at every node
data.M = sim.getM(); // M vector
data.N = sim.getN(); // N vector
data.H = sim.getH(); // H vector

data.Upre = sim.getUpre(); // Upre vector
data.Mpre = sim.getMpre(); // Mpre vector
data.Npre = sim.getNpre(); // Npre vector
data.Hpre = sim.getHpre(); // Hpre vector

// Gating Variable Vectors
var curr = sim.getCurrentStates();
var prev = sim.getPreviousStates();

var gatingVariableList = new List<CellData.GatingVariableData>();
foreach (var kvp in curr)
{
gatingVariableList.Add(new CellData.GatingVariableData {
name = kvp.Key,
current = kvp.Value,
previous = prev[kvp.Key], // lookup by the same key
});
}

data.gates = gatingVariableList.ToArray();

// Save active ion channels
var channelList = new List<CellData.ChannelData>();
foreach (var channel in sim.ionChannels) {
bool active = sim.activeIonChannels.Contains(channel);
channelList.Add(new CellData.ChannelData{
name = channel.Name,
isActive = active
});
}
data.channels = channelList.ToArray();

data.simID = sim.simID;
data.pos = sim.transform.position;
Expand Down Expand Up @@ -230,7 +251,7 @@ IEnumerator ShowSaveButton()
SaveButtonVisible(true);
}

/// <summary>
/// <summary>
/// Load a file. Return true if successful.
/// </summary>
public bool Load(String f)
Expand Down Expand Up @@ -277,6 +298,7 @@ public bool Load(String f)

int ID = 0; // this will be the current ID when placing a new cell after loading


for (; i <= limit; i++)
{
// retrieve saved data
Expand All @@ -288,16 +310,23 @@ public bool Load(String f)

// restore vectors
gm.U = data.U;
gm.M = data.M;
gm.N = data.N;
gm.H = data.H;

gm.Upre = data.Upre;
gm.Mpre = data.Mpre;
gm.Npre = data.Npre;
gm.Hpre = data.Hpre;

// Build Gating Variables and Ion Channels

var currStates = new Dictionary<string,double[]>();
var prevStates = new Dictionary<string,double[]>();

foreach(var g in data.gates) {
currStates[g.name] = g.current;
prevStates[g.name] = g.previous;
}

gm.currentStates = currStates;
gm.previousStates = prevStates;

GameObject go;

try
{
go = loader.Load(new RaycastHit()); // load the cell
Expand All @@ -313,9 +342,20 @@ public bool Load(String f)
finishedLoading = true;
return false;
}

SparseSolverTestv1 sim = go.GetComponent<SparseSolverTestv1>();

// 1) initialize ion channels (so sim.ionChannels is populated)
sim.InitializeIonChannel();
// 2) clear active channels then enable/disable each one according to what was saved
sim.activeIonChannels.Clear();
// Load Ion Channels
foreach (var cd in data.channels) {
// find the matching IonChannel object
var match = sim.ionChannels.FirstOrDefault(ch => ch.Name == cd.name);
if (match != null && cd.isActive) sim.activeIonChannels.Add(match);
}

// restore cell ID
sim.simID = data.simID;
ID = sim.simID;
Expand Down Expand Up @@ -352,12 +392,16 @@ public bool Load(String f)
for (int j = 0; j < data.graphs.Length; j++)
{
var graphObj = Instantiate(graphPrefab);

NDLineGraph g = graphObj.GetComponent<NDLineGraph>();
g.ndgraph.FocusVert = data.graphs[j].vertex;
g.ndgraph.simulation = sim;
graphM.graphs.Add(g.ndgraph);
foreach (Vector3 v in data.graphs[j].positions)
{
g.positions.Add(v);

}
}
}

Expand Down Expand Up @@ -385,7 +429,7 @@ public bool Load(String f)
}
syn = Instantiate(GameManager.instance.synapseManagerPrefab.GetComponent<SynapseManager>().synapsePrefab, ndsim.transform).GetComponentInChildren<Synapse>();
syn.AttachToSimulation(ndsim, synD.syns[j].synVert);
syn.SwitchModel(synD.syns[j].model);
// syn.SwitchModel(synD.syns[j].model);
}

finishedLoading = true; // this is for ChangeGradient
Expand Down
58 changes: 58 additions & 0 deletions Assets/Scripts/C2M2/NeuronalDynamics/IonChannels/IonChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using Vector = MathNet.Numerics.LinearAlgebra.Vector<double>;
namespace C2M2.NeuronalDynamics.Simulation
{
public class GatingVariable
{
// Name of the gating variable (i.e: n, m, h)
public string Name { get; }

// Alpha and Beta functions for the gating variable
public Func<Vector, Vector> Alpha { get; }
public Func<Vector, Vector> Beta { get; }

public double Exponent { get; }

// Probability is the initial state probability for the gating variable
public double Probability { get; set; }

// New flag: if true, this gating variable is computed instantaneously. Default: set to false.
public bool IsInstant { get; set; }

public GatingVariable(string name, Func<Vector, Vector> alpha, Func<Vector, Vector> beta, double exponent, double probability, int nodeCount, bool isInstant = false)
{
Name = name;
Alpha = alpha;
Beta = beta;
Exponent = exponent;
Probability = probability;
IsInstant = isInstant;
}
}

public class IonChannel
{
public string Name { get; set; } // Name of the ion channel (e.g., K+ channel)
public double Conductance { get; set; } // g (conductance)
public double ReversalPotential { get; set; } // E (reversal potential)
public List<GatingVariable> GatingVariables { get; set; } // List of gating variables (can be dynamic)

public IonChannel(string name, double conductance, double reversalPotential)
{
Name = name;
Conductance = conductance;
ReversalPotential = reversalPotential;
GatingVariables = new List<GatingVariable>();
}
public void AddGatingVariable(GatingVariable gatingVariable)
{
GatingVariables.Add(gatingVariable);
}

public void RemoveGatingVariable(GatingVariable gatingVariable)
{
GatingVariables.Remove(gatingVariable);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading