-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefine_ivp.f90
executable file
·219 lines (180 loc) · 6.33 KB
/
define_ivp.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
! v2.0
MODULE define_IVP
IMPLICIT NONE
! DX assigned a value in program that Uses define_IVP
! Physical constants
DOUBLE PRECISION,parameter :: R = 8.3144621D0, & ! Universal gas constant
! (J.mol^-1.K^-1 =
! kg.m^2.s^-2.mol^-1.K^-1)
Avogadro = 6.02214129D23, & ! Avogadro constant
pi = 3.14159265359, & ! pi
conc_min = 0.D0, & ! Treshold concentration
infinity = huge(1.D0)
integer :: neqn !, indxCG
DOUBLE PRECISION :: advectionRate, dx, scaleFactor
DOUBLE PRECISION, allocatable, dimension(:) :: gasFlux, photonFlux !,molm,cd,if0
! Spectral Radius
logical :: useSR = .FALSE.
DOUBLE PRECISION :: SRmax = 1D-8
! Sparse stoechiometry matrices and reaction rates
character*10, allocatable :: speciesList(:)
integer :: nbSpecies, nbReac, nbPhotoReac,&
spectrumRange(1:2) = (/ 50, 200 /),&
sp1, sp2
DOUBLE PRECISION :: spectralResolution = 1.D0
integer, allocatable :: D(:,:), L(:,:), LL(:,:)
integer, allocatable :: Dphoto(:,:), Lphoto(:,:)
DOUBLE PRECISION , allocatable :: reactionRates(:), Dk(:),&
crossSections(:,:), photoRates(:), yloc(:)
DOUBLE PRECISION, allocatable :: v(:), ly(:)
DOUBLE PRECISION, allocatable :: absorb(:,:), sumabs(:), intabs(:)
CONTAINS
subroutine THRESH(Y)
integer :: i
DOUBLE PRECISION :: Y(:)
do i = 1, size(Y)
Y(i) = max(Y(i),conc_min)
enddo
END subroutine THRESH
SUBROUTINE F_E(NEQN,T,Y,DY)
INTEGER, intent(in) :: NEQN
DOUBLE PRECISION, intent(in) :: T, Y(NEQN)
DOUBLE PRECISION, intent(out) :: DY(NEQN)
! Restore real concentrations
call thresh(Y)
yloc = y * scaleFactor
DY = 0.D0
! Photolysis
if (nbPhotoReac /= 0) call PHOTO_KINET_SPARSE(yloc,DY)
! Transport
DY = DY + ( gasFlux - advectionRate * yloc) / dx ! molec.cm^-3.s^-1
!+ cd*(conc0-conc(1:))/dx**2/2
! Back to scaled concentrations
dy = dy / scaleFactor
END SUBROUTINE F_E
SUBROUTINE F_I(GRID_POINT,NPDES,T,Y,DY,WANT_JAC,JAC)
INTEGER, intent(in) :: GRID_POINT,NPDES
LOGICAL, intent(in) :: WANT_JAC
DOUBLE PRECISION, intent(in) :: T, Y(NPDES)
DOUBLE PRECISION, intent(out) :: DY(NPDES), JAC(NPDES,NPDES)
if (nbReac == 0) then
DY = 0D0
if (WANT_JAC) JAC = 0D0
else
call thresh(Y)
yloc = Y * scaleFactor
call KINET_SPARSE(yloc,DY,WANT_JAC,JAC)
dy = dy / scaleFactor
endif
END SUBROUTINE F_I
DOUBLE PRECISION FUNCTION SR(NEQN,T,Y)
INTEGER, intent(in) :: NEQN
DOUBLE PRECISION, intent(in) :: T, Y(NEQN)
!SR = 4d0 / DX**2
SR = max(SRmax,5/DX)
END FUNCTION SR
SUBROUTINE PHOTO_KINET_SPARSE(y,dy)
DOUBLE PRECISION, intent(in) :: y(:)
DOUBLE PRECISION, intent(inout) :: dy(:)
integer :: imaxD, imaxL, ii, jj, i, j, k
imaxD = size(Dphoto,1)
imaxL = size(Lphoto,1)
absorb = 0D0
do i = 1, imaxL
ii = Lphoto(i,1)
absorb(ii,sp1:sp2) = crossSections(ii,sp1:sp2) * &
y(Lphoto(i,2)) ! cm^-1
enddo
do k = sp1, sp2
sumabs(k) = sum(absorb(1:nbPhotoReac,k))
end do
intabs = photonFlux * (1d0-exp(-sumabs*dx))/dx ! ph.cm^-3.s^-1.nm^-1
do k = sp1, sp2
if(sumabs(k) == 0D0) cycle
intabs(k) = intabs(k) / sumabs(k) ! ph.cm^-2.s^-1.nm^-1
end do
photoRates = 0D0
do i = 1, imaxL
ii = Lphoto(i,1)
photoRates(ii) = photoRates(ii) + &
dot_product(intabs,absorb(ii,:)) * &
spectralResolution !cm^-3.s^-1
enddo
do i = 1, imaxD
ii = Dphoto(i,2)
dy(ii) = dy(ii) + Dphoto(i,3) * photoRates(Dphoto(i,1))
enddo
END SUBROUTINE PHOTO_KINET_SPARSE
SUBROUTINE KINET_SPARSE(y,dy,want_jac,jac)
! Adapted from Cangiani2012 : Biochemical Pathways Simulation
! Dk = t(D*k) (done in calling program)
! dC = Dk %*% apply(parms$L,1,function(x) prod(y^x))
! NB: Dense matrices dimensions : reactions x species
! NB: Description of sparse matrices D & L as triplets (reac:i,species:j,val)
DOUBLE PRECISION, intent(in) :: y(:)
DOUBLE PRECISION, intent(out) :: dy(:)
logical, intent(in) :: want_jac
DOUBLE PRECISION, optional,intent(out) :: jac(:,:)
integer :: imaxD, imaxL, imaxLL, &
ii, jj, i, j
DOUBLE PRECISION :: tmp
imaxD = size(D,1)
imaxL = size(L,1)
! v_Dense = L_Sparse %*% ly_Dense
ly = log(y)
v = 0D0
do i = 1, imaxL
ii = L(i,1)
v(ii) = v(ii) + L(i,3)*ly(L(i,2))
enddo
v = exp(v)
! dy_Dense = Dk_Sparse %*% v_Dense
dy = 0D0
do i = 1, imaxD
ii = D(i,2)
dy(ii) = dy(ii) + Dk(i)*v(D(i,1))
enddo
if(want_jac) then ! Compute jacobian
jac = 0D0
do j = 1, size(y)
! Contract L matrix
! 1- select reactions involving species j
v = 0D0
do i = 1, imaxL
if (L(i,2) == j) then
ii = L(i,1)
v(ii) = v(ii) + 1D0
endif
enddo
! 2- reduce L matrix according to v/=0
LL = 0
jj = 0
do i = 1, imaxL
ii = L(i,1)
if (v(ii) == 0D0) cycle
jj = jj + 1
LL(jj,:) = L(i,:)
enddo
imaxLL = jj
! v_Dense = LL_Sparse %*% ly_Dense
v = 0D0
do i = 1, imaxLL
ii = LL(i,1)
jj = LL(i,2)
tmp = LL(i,3)
if(v(ii) == 0D0) v(ii) = 1D0 ! Leave unconcerned reactions to 0
if(jj == j) then
v(ii) = v(ii) * tmp * y(jj)**(tmp-1)
else
v(ii) = v(ii) * y(jj)**tmp
endif
enddo
! jac_Dense = Dk_Sparse %*% v_Dense
do i = 1, imaxD
ii = D(i,2)
jac(ii,j) = jac(ii,j) + Dk(i)*v(D(i,1))
enddo
enddo
endif
END SUBROUTINE KINET_SPARSE
END MODULE define_IVP