-
Notifications
You must be signed in to change notification settings - Fork 6
/
mo_elemeffects.f90
389 lines (322 loc) · 18.7 KB
/
mo_elemeffects.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
!> \file mo_elemeffects.f90
!> \brief Elementary Effects.
!> \details This module is calculating the Elementary effects of model parameters
!> using parameter sets sampled by the Morris method.
!> \author Juliane Mai
!> \date Mar 2012
MODULE mo_elemeffects
! This module is calculating the Elementary effects of model parameters
! using parameter sets sampled by the Morris method.
! Written Juliane Mai, Mar 2012
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2012 Juliane Mai
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
USE mo_kind, ONLY: i4, sp, dp
IMPLICIT NONE
PUBLIC :: elemeffects ! Elementary effects of model parameters
! ------------------------------------------------------------------
! NAME
! elemeffects
! PURPOSE
!> \brief Elementary Effects of model parameter.
!
!> \details Determine the Elementary Effects of model parameters using parameter sets sampled by
!> the Morris method.
! CALLING SEQUENCE
! call elemeffects(modeloutput,para,changedpara,elemeffect,counter, &
! modelstatus_in=modelstatus)
! INTENT(IN)
!> \param[in] "real(sp/dp) :: modeloutput(:)/modeloutput(:,:)" modeloutput(i)/ modeloutput(i,:) is
!> model output using parameter set \f$ i \f$
!> \param[in] "real(sp/dp) :: para(:,:)" array of parameters sets\n
!> parameter sets have to be a Morris sequence\n
!> values >= 0.0 and <= 1.0\n
!> size(para,1) number of sets\n
!> size(para,2) number of parameters\n
!> \param[in] "integer(i4) :: changedpara(size(para,1))" vector of parameter changed
!> between parameter set \f$ i \f$ and \f$ i+1 \f$
! INTENT(INOUT)
! None
! INTENT(OUT)
!> \param[out] "real(sp/dp) :: elemeffect(size(para,2))" elementary effect per parameter
!> \param[out] "integer(i4) :: counter(size(para,2))" ith elementary effect is determined using counter(i)
!> model outputs
! INTENT(IN), OPTIONAL
!> \param[in] "logical, optional :: modelstatus_in(size(para,1))"
!> array of status of the model, i.e. if parameter set
!> leads to valid model output\n
!> DEFAULT: .true. (parameter set valid)
! INTENT(INOUT), OPTIONAL
! None
! INTENT(OUT), OPTIONAL
! None
! RETURN
! None
!
! RESTRICTIONS
!> \note Parameter set needs to be a Morris sequence \n
!> --> see test_mo_elemeffects/morris for MATLAB files and readme
! EXAMPLE
! ! para and changedpara can be generated by MATLAB code
! ! para needs to be scaled between 0.0 and 1.0
! para(1) = (/ 0.4, 0.8 /)
! para(2) = (/ 0.8, 0.8 /)
! para(3) = (/ 0.8, 0.2 /)
! changedpara = (/ 1, 2, 0 /)
! modeloutput = (/ 3.2, 4.0, 0.8 /)
! call elemeffects(modeloutput,para,changedpara,elemeffect,counter)
!
! see also test_mo_elemeffects for detailled example
! LITERATURE
! Morris, M. D., 1991.
! Factorial sampling plans for preliminary computational experiments.
! Technometrics 33:161-174.
! Saltelli, A., M. Ratto, T. Andres, F. Campolongo, J. Cariboni, D. Gatelli, M. Saisana, and S. Tarantola, 2008.
! Global Sensitivity Analysis. The Primer.
! John Wiley & Sons Ltd.
! Campolongo, F., J. Cariboni, and A. Saltelli, 2007.
! An effective screening design for sensitivity analysis of large models.
! Environmental Modelling & Software 22:1509-1518.
! HISTORY
! Written, Juliane Mai, March 2012
INTERFACE elemeffects
MODULE PROCEDURE elemeffects_0d_dp, elemeffects_0d_sp, &
elemeffects_1d_dp, elemeffects_1d_sp
END INTERFACE elemeffects
! ------------------------------------------------------------------
PRIVATE
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
SUBROUTINE elemeffects_0d_dp(modeloutput_0d,para,changedpara,elemeffect,counter,modelstatus_in)
IMPLICIT NONE
REAL(DP), DIMENSION(:), INTENT(IN) :: modeloutput_0d ! vector of output the model
! ! generated with certain parameters
REAL(DP), DIMENSION(:,:), INTENT(IN) :: para ! matrix of parameters to test
! ! parameter sets have to be a
! ! morris sequence
! ! values >= 0.0 and <= 1.0
! ! size(para,1) number of sets
! ! size(para,2) number of parameters
INTEGER(I4), DIMENSION(size(para,1)), INTENT(IN) :: changedpara ! vector of parameter changed
! ! between parameter set i and i+1
REAL(DP), DIMENSION(size(para,2)), INTENT(OUT) :: elemeffect ! elementary effect of each parameter
INTEGER(I4), DIMENSION(size(para,2)), INTENT(OUT) :: counter ! ith elementary effect is
! ! based on counter(i) values
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: modelstatus_in ! vector of status of the model
! ! .true. if parameter set was valid
! local variables
INTEGER(I4) :: sets ! Number of parameter sets
INTEGER(I4) :: i, valid
LOGICAL, DIMENSION(size(modeloutput_0d,1)) :: modelstatus ! default .true.
sets = size(para,1)
elemeffect = 0.0_dp
counter = 0_i4
if (present(modelstatus_in)) then
modelstatus = modelstatus_in
else
modelstatus = .true.
end if
valid = 0
do i=1, sets
! only within a trajectory
if (changedpara(i) .gt. 0_i4) then
! determine model value
if ( modelstatus(i) .and. modelstatus(i+1) .and. &
abs(para(i+1,changedpara(i))-para(i,changedpara(i))) .gt. epsilon(0.0_dp) ) then
elemeffect(changedpara(i)) = elemeffect(changedpara(i)) &
+ abs( &
(modeloutput_0d(i+1)-modeloutput_0d(i))/ &
(para(i+1,changedpara(i))-para(i,changedpara(i))) &
)
counter(changedpara(i)) = counter(changedpara(i)) + 1_i4
end if
end if
! count valid parameter sets
if (modelstatus(i)) then
valid = valid + 1_i4
end if
end do
elemeffect(:) = elemeffect(:)/max(real(counter(:),dp), 1.0_dp)
END SUBROUTINE elemeffects_0d_dp
SUBROUTINE elemeffects_0d_sp(modeloutput_0d,para,changedpara,elemeffect,counter,modelstatus_in)
IMPLICIT NONE
REAL(SP), DIMENSION(:), INTENT(IN) :: modeloutput_0d ! vector of output the model
! ! generated with certain parameters
REAL(SP), DIMENSION(:,:), INTENT(IN) :: para ! matrix of parameters to test
! ! parameter sets have to be a
! ! morris sequence
! ! values >= 0.0 and <= 1.0
! ! size(para,1) number of sets
! ! size(para,2) number of parameters
INTEGER(I4), DIMENSION(size(para,1)), INTENT(IN) :: changedpara ! vector of parameter changed
! ! between parameter set i and i+1
REAL(SP), DIMENSION(size(para,2)), INTENT(OUT) :: elemeffect ! elementary effect of each parameter
INTEGER(I4), DIMENSION(size(para,2)), INTENT(OUT) :: counter ! ith elementary effect is
! ! based on counter(i) values
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: modelstatus_in ! vector of status of the model
! ! .true. if parameter set was valid
! local variables
INTEGER(I4) :: sets ! Number of parameter sets
INTEGER(I4) :: i, valid
LOGICAL, DIMENSION(size(modeloutput_0d,1)) :: modelstatus ! default .true.
sets = size(para,1)
elemeffect = 0.0_sp
counter = 0_i4
if (present(modelstatus_in)) then
modelstatus = modelstatus_in
else
modelstatus = .true.
end if
valid = 0
do i=1, sets
! only within a trajectory
if (changedpara(i) .gt. 0_i4) then
! determine model value
if ( modelstatus(i) .and. modelstatus(i+1) .and. &
abs(para(i+1,changedpara(i))-para(i,changedpara(i))) .gt. epsilon(0.0_sp) ) then
elemeffect(changedpara(i)) = elemeffect(changedpara(i)) &
+ abs( &
(modeloutput_0d(i+1)-modeloutput_0d(i))/ &
(para(i+1,changedpara(i))-para(i,changedpara(i))) &
)
counter(changedpara(i)) = counter(changedpara(i)) + 1_i4
end if
end if
! count valid parameter sets
if (modelstatus(i)) then
valid = valid + 1_i4
end if
end do
elemeffect(:) = elemeffect(:)/max(real(counter(:),sp), 1.0_sp)
END SUBROUTINE elemeffects_0d_sp
SUBROUTINE elemeffects_1d_dp(modeloutput_1d,para,changedpara,elemeffect,counter,modelstatus_in)
IMPLICIT NONE
REAL(DP), DIMENSION(:,:), INTENT(IN) :: modeloutput_1d ! matrix of output the model
! ! generated with certain parameters
! ! 1 row per parameter set
REAL(DP), DIMENSION(:,:), INTENT(IN) :: para ! matrix of parameters to test
! ! parameter sets have to be a
! ! morris sequence
! ! values >= 0.0 and <= 1.0
! ! size(para,1) number of sets
! ! size(para,2) number of parameters
INTEGER(I4), DIMENSION(size(para,1)), INTENT(IN) :: changedpara ! vector of parameter changed
! ! between parameter set i and i+1
REAL(DP), DIMENSION(size(para,2)), INTENT(OUT) :: elemeffect ! elementary effect of each parameter
INTEGER(I4), DIMENSION(size(para,2)), INTENT(OUT) :: counter ! ith elementary effect is
! ! based on counter(i) values
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: modelstatus_in ! vector of status of the model
! ! .true. if parameter set was valid
! local variables
INTEGER(I4) :: sets ! Number of parameter sets
LOGICAL, DIMENSION(size(modeloutput_1d,1)) :: modelstatus ! default .true.
INTEGER(I4) :: i, valid, n
if (present(modelstatus_in)) then
modelstatus = modelstatus_in
else
modelstatus = .true.
end if
n = size(modeloutput_1d,2) ! number of model outputs per parameter set
sets = size(para,1)
elemeffect = 0.0_dp
counter = 0_i4
valid = 0
do i=1, sets
! only within a trajectory
if (changedpara(i) .gt. 0_i4) then
! only if both both parameter sets were valid
if (modelstatus(i) .and. modelstatus(i+1) .and. &
abs(para(i+1,changedpara(i))-para(i,changedpara(i))) .gt. epsilon(1.0_dp) ) then
elemeffect(changedpara(i)) = elemeffect(changedpara(i)) &
+ sum( abs( &
(modeloutput_1d(i+1,:)-modeloutput_1d(i,:))/ &
(para(i+1,changedpara(i))-para(i,changedpara(i))) &
) )
counter(changedpara(i)) = counter(changedpara(i)) + n
end if
end if
! count valid parameter sets
if (modelstatus(i)) then
valid = valid + 1_i4
end if
end do
elemeffect(:) = elemeffect(:)/max(real(counter(:),dp), 1.0_dp)
counter(:) = int(real(counter(:),dp)/real(n,dp),i4)
END SUBROUTINE elemeffects_1d_dp
SUBROUTINE elemeffects_1d_sp(modeloutput_1d,para,changedpara,elemeffect,counter,modelstatus_in)
IMPLICIT NONE
REAL(SP), DIMENSION(:,:), INTENT(IN) :: modeloutput_1d ! matrix of output the model
! ! generated with certain parameters
! ! 1 row per parameter set
REAL(SP), DIMENSION(:,:), INTENT(IN) :: para ! matrix of parameters to test
! ! parameter sets have to be a
! ! morris sequence
! ! values >= 0.0 and <= 1.0
! ! size(para,1) number of sets
! ! size(para,2) number of parameters
INTEGER(I4), DIMENSION(size(para,1)), INTENT(IN) :: changedpara ! vector of parameter changed
! ! between parameter set i and i+1
REAL(SP), DIMENSION(size(para,2)), INTENT(OUT) :: elemeffect ! elementary effect of each parameter
INTEGER(I4), DIMENSION(size(para,2)), INTENT(OUT) :: counter ! ith elementary effect is
! ! based on counter(i) values
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: modelstatus_in ! vector of status of the model
! ! .true. if parameter set was valid
! local variables
INTEGER(I4) :: sets ! Number of parameter sets
LOGICAL, DIMENSION(size(modeloutput_1d,1)) :: modelstatus ! default .true.
INTEGER(I4) :: i, valid, n
if (present(modelstatus_in)) then
modelstatus = modelstatus_in
else
modelstatus = .true.
end if
n = size(modeloutput_1d,2) ! number of model outputs per parameter set
sets = size(para,1)
elemeffect = 0.0_sp
counter = 0_i4
valid = 0
do i=1, sets
! only within a trajectory
if (changedpara(i) .gt. 0_i4) then
! only if both both parameter sets were valid
if (modelstatus(i) .and. modelstatus(i+1) .and. &
abs(para(i+1,changedpara(i))-para(i,changedpara(i))) .gt. epsilon(1.0_sp) ) then
elemeffect(changedpara(i)) = elemeffect(changedpara(i)) &
+ sum( abs( &
(modeloutput_1d(i+1,:)-modeloutput_1d(i,:))/ &
(para(i+1,changedpara(i))-para(i,changedpara(i))) &
) )
counter(changedpara(i)) = counter(changedpara(i)) + n
end if
end if
! count valid parameter sets
if (modelstatus(i)) then
valid = valid + 1_i4
end if
end do
elemeffect(:) = elemeffect(:)/max(real(counter(:),sp), 1.0_sp)
counter(:) = int(real(counter(:),sp)/real(n,sp),i4)
END SUBROUTINE elemeffects_1d_sp
! ------------------------------------------------------------------
END MODULE mo_elemeffects