-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterneuronPool.f90
287 lines (236 loc) · 12.3 KB
/
InterneuronPool.f90
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
! '''
! Neuromuscular simulator in Fortran.
! Copyright (C) 2021 Renato Naville Watanabe
! Pablo Alejandro
! Marina Cardoso de Oliveira
! This program is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! any later version.
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <http://www.gnu.org/licenses/>.
! Contact: [email protected]
! '''
module InterneuronPoolClass
use ConfigurationClass
!L! use mkl_spblas
use DynamicalArrays
use InterneuronClass
implicit none
private
integer, parameter :: wp = kind(1.0d0)
real(wp), parameter :: pi = 4 * atan(1.0_wp)
public :: InterneuronPool
type InterneuronPool
character(len = 2) :: poolKind
character(len = 15) :: pool
type(Configuration), pointer :: conf
integer :: Nnumber, totalNumberOfCompartments
type(Interneuron), dimension(:), allocatable :: unit
real(wp), dimension(:,:), allocatable :: poolSomaSpikes
real(wp), dimension(:), allocatable :: v_mV, iInjected, capacitanceInv
real(wp), dimension(:), allocatable :: iIonic, EqCurrent_nA
real(wp), dimension(:,:), allocatable :: G
integer :: spIndexing, spRows, spCols, spNumberOfElements, spOperation
integer, dimension(:), allocatable :: spRowStart, spRowEnd, spColIdx
real(wp), dimension(:), allocatable :: spValues
contains
procedure :: dVdt
procedure :: atualizeInterneuronPool
procedure :: listSpikes
procedure :: reset
end type InterneuronPool
interface InterneuronPool
module procedure init_InterneuronPool
end interface InterneuronPool
contains
type(InterneuronPool) function init_InterNeuronPool(conf, pool, group)
! '''
! Constructor
! - Inputs:
! + **conf**: Configuration object with the simulation parameters.
! + **pool**: string with Interneuron pool to which the motor unit belongs.
! '''
class(Configuration), intent(in), target :: conf
character(len = 15), intent(in) :: pool
character(len = 4), intent(in) :: group
character(len = 80) :: paramTag, paramChar
real(wp) :: paramReal
integer :: i, j, stat
! ## Indicates that is Motor Unit pool.
init_InterNeuronPool%poolKind = 'IN'
! ## Configuration object with the simulation parameters.
init_InterNeuronPool%conf => conf
! ## String with Motor unit pool to which the motor unit belongs.
init_InterNeuronPool%pool = trim(pool) // '_' // trim(group)
! ## Number of Neurons.
paramTag = 'Number_' // trim(init_InterNeuronPool%pool)
paramChar = init_InterNeuronPool%conf%parameterSet(paramTag, pool, 0)
read(paramChar, *)paramReal
init_InterNeuronPool%Nnumber = nint(paramReal)
! ## List of Interneuron objects.
allocate(init_InterNeuronPool%unit(init_InterNeuronPool%Nnumber))
do i = 1, init_InterNeuronPool%Nnumber
init_InterNeuronPool%unit(i) = Interneuron(init_InterNeuronPool%conf, init_InterNeuronPool%pool, i)
end do
! ## Vector with the instants of spikes in the soma compartment, in ms.
if (allocated(init_InterNeuronPool%poolSomaSpikes)) then
deallocate(init_InterNeuronPool%poolSomaSpikes)
end if
! ##
init_InterNeuronPool%totalNumberOfCompartments = 0
do i = 1, init_InterNeuronPool%Nnumber
init_InterNeuronPool%totalNumberOfCompartments = init_InterNeuronPool%totalNumberOfCompartments &
+ init_InterNeuronPool%unit(i)%compNumber
end do
allocate(init_InterNeuronPool%v_mV(init_InterNeuronPool%totalNumberOfCompartments))
allocate(init_InterNeuronPool%G(init_InterNeuronPool%totalNumberOfCompartments,&
init_InterNeuronPool%totalNumberOfCompartments))
allocate(init_InterNeuronPool%iInjected(init_InterNeuronPool%totalNumberOfCompartments))
allocate(init_InterNeuronPool%capacitanceInv(init_InterNeuronPool%totalNumberOfCompartments))
allocate(init_InterNeuronPool%iIonic(init_InterNeuronPool%totalNumberOfCompartments))
allocate(init_InterNeuronPool%EqCurrent_nA(init_InterNeuronPool%totalNumberOfCompartments))
init_InterNeuronPool%iInjected(:) = 0.0
init_InterNeuronPool%iIonic(:) = 0.0
! # Retrieving data from Interneuron class
init_InterNeuronPool%G(:,:) = 0.0
do i = 1, init_InterNeuronPool%Nnumber
init_InterNeuronPool%v_mV((i-1)*init_InterNeuronPool%unit(i)%compNumber+1:&
i*init_InterNeuronPool%unit(i)%compNumber) =&
init_InterNeuronPool%unit(i)%v_mV
! # With only one compartment, it is a diagonal matrix
init_InterNeuronPool%G(i:i,i:i) = init_InterNeuronPool%unit(i)%G
init_InterNeuronPool%capacitanceInv((i-1)*init_InterNeuronPool%unit(i)%compNumber+1:&
i*init_InterNeuronPool%unit(i)%compNumber) &
= init_InterNeuronPool%unit(i)%capacitanceInv
init_InterNeuronPool%EqCurrent_nA((i-1)*init_InterNeuronPool%unit(i)%compNumber+1:&
i*init_InterNeuronPool%unit(i)%compNumber) &
= init_InterNeuronPool%unit(i)%EqCurrent_nA
end do
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
init_InterNeuronPool%spIndexing = 1
init_InterNeuronPool%spRows = init_InterNeuronPool%totalNumberOfCompartments
init_InterNeuronPool%spCols = init_InterNeuronPool%totalNumberOfCompartments
init_InterNeuronPool%spNumberOfElements = 0
allocate(init_InterNeuronPool%spRowStart(init_InterNeuronPool%spRows))
allocate(init_InterNeuronPool%spRowEnd(init_InterNeuronPool%spRows))
init_InterNeuronPool%spRowStart(:) = 0
do i = 1, init_InterNeuronPool%totalNumberOfCompartments
do j = 1, init_InterNeuronPool%totalNumberOfCompartments
if (abs(init_InterNeuronPool%G(i,j))>1e-10) then
init_InterNeuronPool%spNumberOfElements = init_InterNeuronPool%spNumberOfElements + 1
if (init_InterNeuronPool%spRowStart(i) == 0) then
init_InterNeuronPool%spRowStart(i) = init_InterNeuronPool%spNumberOfElements
end if
call AddToList(init_InterNeuronPool%spValues, init_InterNeuronPool%G(i,j))
call integerAddToList(init_InterNeuronPool%spColIdx, j)
end if
end do
init_InterNeuronPool%spRowEnd(i) = init_InterNeuronPool%spNumberOfElements+1
end do
print '(A)', 'Interneuron Pool of ' // trim(pool) // ' ' // trim(group) // ' built'
end function
subroutine atualizeInterneuronPool(self, t)
! '''
! Update all parts of the Motor Unit pool. It consists
! to update all motor units, the activation signal and
! the muscle force.
! - Inputs:
! + **t**: current instant, in ms.
! '''
class(InterneuronPool), intent(inout) :: self
real(wp), intent(in) :: t
real(wp), dimension(self%totalNumberOfCompartments) :: k1, k2, k3, k4
real(wp), dimension(self%totalNumberOfCompartments) :: newStateTemp
real(wp), dimension(:), allocatable :: vUnit
real(wp) :: newtTemp
integer :: i
real(wp) :: vmax, vmin
vmin = -30.0
vmax = 120.0
k1 = self%dVdt(t, self%v_mV)
newStateTemp = self%v_mV + self%conf%timeStepByTwo_ms * k1
newtTemp = t + self%conf%timeStepByTwo_ms
k2 = self%dVdt(newtTemp, newStateTemp)
newStateTemp = self%v_mV + self%conf%timeStepByTwo_ms * k2
k3 = self%dVdt(newtTemp, newStateTemp)
newStateTemp = self%v_mV + self%conf%timeStep_ms * k3
newtTemp = t + self%conf%timeStep_ms
k4 = self%dVdt(newtTemp, newStateTemp)
self%v_mV = self%v_mV + self%conf%timeStepBySix_ms * (k1 + 2.0*k2 + 2.0*k3 + k4)
self%v_mV = merge(self%v_mV, vmax, self%v_mV < vmax)
self%v_mV = merge(self%v_mV, vmin, self%v_mV > vmin)
allocate(vUnit(self%unit(1)%compNumber))
do i = 1, self%Nnumber
vUnit = self%v_mV((i-1)*self%unit(i)%compNumber+1:i*self%unit(i)%compNumber)
call self%unit(i)%atualizeInterneuron(t, vUnit)
end do
end subroutine
function dVdt(self, t, V)
class(InterneuronPool), intent(inout) :: self
real(wp), intent(in) :: t
real(wp), intent(in) :: V(self%totalNumberOfCompartments)
real(wp), dimension(self%totalNumberOfCompartments) :: dVdt
integer :: i, j, stat
real(wp), dimension(self%totalNumberOfCompartments) :: matInt
do i = 1, self%Nnumber
do j = 1, self%unit(i)%compNumber
self%iIonic((i-1)*self%unit(i)%compNumber+j) = &
self%unit(i)%Compartments(j)%computeCurrent(t, V((i-1)*self%unit(i)%compNumber+j))
end do
end do
matInt = matmul(self%G, V)
dVdt = (self%iIonic + matInt + self%iInjected + self%EqCurrent_nA) * &
self%capacitanceInv
end function
subroutine listSpikes(self)
! '''
! List the spikes that occurred in the soma and in
! the terminal of the different motor units.
! '''
class(InterneuronPool) , intent(inout) :: self
integer :: i
integer, dimension(self%Nnumber) :: numberOfNewSpikesSoma
integer :: numberOfSpikesSoma
integer :: initInd, endInd
do i = 1, self%Nnumber
if (allocated(self%unit(i)%somaSpikeTrain)) then
numberOfNewSpikesSoma(i) = size(self%unit(i)%somaSpikeTrain)
else
numberOfNewSpikesSoma(i) = 0
end if
end do
numberOfSpikesSoma = sum(numberOfNewSpikesSoma)
allocate(self%poolSomaSpikes(numberOfSpikesSoma,2))
initInd = 1
do i = 1, self%Nnumber
if (allocated(self%unit(i)%somaSpikeTrain)) then
endInd = initInd + numberOfNewSpikesSoma(i) - 1
self%poolSomaSpikes(initInd:endInd,1) = self%unit(i)%somaSpikeTrain
self%poolSomaSpikes(initInd:endInd,2) = i
initInd = endInd+1
end if
end do
end subroutine
subroutine reset(self)
! '''
! '''
class(InterneuronPool) , intent(inout) :: self
integer :: i
if (allocated(self%poolSomaSpikes)) deallocate(self%poolSomaSpikes)
do i = 1, self%Nnumber
call self%unit(i)%reset()
end do
do i = 1, self%Nnumber
self%v_mV((i-1)*self%unit(i)%compNumber+1:&
i*self%unit(i)%compNumber) = &
self%unit(i)%v_mV
end do
print *, 'Interneuron pool reseted'
! read(*,*)
end subroutine
end module InterneuronPoolClass