@@ -105,98 +105,113 @@ subroutine getMTCMatrix1(me, djdep, djres)
105105 ! ! Initialised `BedSediment` object, including all layers and included `FineSediment`
106106 ! ! objects
107107 function createBedSediment1 (me , x , y , w ) result(r)
108- class(BedSediment) :: me ! ! Self-reference
109- integer :: x ! ! x index of the containing water body
110- integer :: y ! ! y index of the containing water body
111- integer :: w ! ! w index of the containing water body
112- type (Result) :: r ! ! Returned `Result` object
113- type (BedSedimentLayer), allocatable :: bsl1 ! LOCAL object of type BedSedimentLayer, for implementation of polymorphism
114- integer :: L ! LOCAL loop counter
115- integer :: allst ! LOCAL array allocation status
116- character (len= 256 ) :: tr ! LOCAL error trace
117- character (len= 16 ), parameter :: ms = " Allocation error" ! LOCAL allocation error message
118- type (ErrorInstance) :: err (1 )
108+ class(BedSediment) :: me
109+ integer :: x, y, w
110+ type (Result) :: r
111+ type (BedSedimentLayer), allocatable :: bsl1
112+ integer :: L, allst
113+ character (len= 256 ) :: tr
114+ character (len= 16 ), parameter :: ms = " Allocation error"
115+ type (ErrorInstance) :: err (1 )
116+ integer :: nx, ny
117+ logical :: inbounds
119118
120- me% name = trim (ref(' BedSediment' , x, y, w))
121- me% nSizeClasses = C% nSizeClassesSpm ! set number of size classes from global value
122- me% nfComp = C% nFracCompsSpm ! set number of compositional fractions from global value
123- tr = trim (me% name) // " %createBedSediment1" ! procedure name as trace
119+ me% name = trim (ref(' BedSediment' , x, y, w))
120+ ! >>> FIX: set indices so later DATASET(x,y,...) lookups are valid
121+ me% x = x
122+ me% y = y
123+ ! <<<
124124
125- ! Initialise Contaminant mass pools matrix
126- allocate (me% m_contaminant(C% nSedimentLayers + 3 ), stat= allst)
127- if (allst /= 0 ) then
128- err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
129- call r% addError(err (1 ))
130- call LOGR% toFile(errors= err)
131- return
132- end if
133- do L = 1 , C% nSedimentLayers + 3
134- ! Pass DATASET%nc and provide required arguments from DATASET
135- r = me% m_contaminant(L)% create_from_data( &
136- data = DATASET% nc, &
137- compartment= ' sediment' , &
138- contaminantDensity= DATASET% contaminantDensity, &
139- soilAttachmentEfficiency= DATASET% soilConstantAttachmentEfficiency, &
140- riverAttachmentEfficiency= DATASET% riverAttachmentEfficiency, &
141- estuaryAttachmentEfficiency= DATASET% estuaryAttachmentEfficiency, &
142- k_diss_pristine= DATASET% contaminant_k_diss_pristine, &
143- k_diss_transformed= DATASET% contaminant_k_diss_transformed, &
144- k_transform_pristine= DATASET% contaminant_k_transform_pristine, &
145- waterTemperature= DATASET% waterTemperature(1 ) & ! Use first timestep or adjust as needed
146- )
147- if (r% hasCriticalError()) then
148- call LOGR% toFile(errors= r% getErrors())
125+ me% nSizeClasses = C% nSizeClassesSpm
126+ me% nfComp = C% nFracCompsSpm
127+ tr = trim (me% name) // " %createBedSediment1"
128+
129+ ! Initialise Contaminant mass pools matrix
130+ allocate (me% m_contaminant(C% nSedimentLayers + 3 ), stat= allst)
131+ if (allst /= 0 ) then
132+ err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
133+ call r% addError(err (1 ))
134+ call LOGR% toFile(errors= err)
149135 return
150136 end if
151- if (L > 2 .and. allocated (DATASET% initialContaminantConcsSediment)) then
152- me% m_contaminant(L)% c = DATASET% initialContaminantConcsSediment(me% x, me% y, :, :, :)
153- if (allocated (DATASET% initialDissolvedConcsSediment)) then
154- me% m_contaminant(L)% m_dissolved = DATASET% initialDissolvedConcsSediment(me% x, me% y)
137+
138+ do L = 1 , C% nSedimentLayers + 3
139+ r = me% m_contaminant(L)% create_from_data( &
140+ compartment= ' sediment' , & ! keep as used before; 'soil' also acceptable if your API expects it
141+ contaminantDensity= DATASET% contaminantDensity, &
142+ soilAttachmentEfficiency= DATASET% soilConstantAttachmentEfficiency, &
143+ riverAttachmentEfficiency= DATASET% riverAttachmentEfficiency, &
144+ estuaryAttachmentEfficiency= DATASET% estuaryAttachmentEfficiency, &
145+ k_diss_pristine= DATASET% contaminant_k_diss_pristine, &
146+ k_diss_transformed= DATASET% contaminant_k_diss_transformed, &
147+ k_transform_pristine= DATASET% contaminant_k_transform_pristine, &
148+ waterTemperature= real (DATASET% waterTemperature(1 ), dp) )
149+ if (r% hasCriticalError()) then
150+ call LOGR% toFile(errors= r% getErrors())
151+ return
155152 end if
156- end if
157- end do
158153
159- allocate (me% colBedSedimentLayers(C% nSedimentLayers), stat= allst)
160- if (allst /= 0 ) then
161- err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
162- call r% addError(err (1 ))
163- call LOGR% toFile(errors= err)
164- return
165- end if
166- me% n_delta_sed = C% nSedimentLayers + 3
167- allocate (me% delta_sed(C% nSedimentLayers + 3 , C% nSedimentLayers + 3 , me% nSizeClasses), stat= allst)
168- if (allst /= 0 ) then
169- err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
170- call r% addError(err (1 ))
171- call LOGR% toFile(errors= err)
172- return
173- end if
174- me% delta_sed = 0.0_dp
175- allocate (me% delta_sed_csr(C% nSizeClassesSpm), stat= allst)
176- if (allst /= 0 ) then
177- err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
178- call r% addError(err (1 ))
179- call LOGR% toFile(errors= err)
180- return
181- end if
154+ ! Seed initial concentrations for actual sediment layers (L>2) if provided
155+ if (L > 2 .and. allocated (DATASET% initialContaminantConcsSediment)) then
156+ nx = size (DATASET% initialContaminantConcsSediment, 1 )
157+ ny = size (DATASET% initialContaminantConcsSediment, 2 )
158+ inbounds = (me% x>= 1 .and. me% y>= 1 .and. me% x<= nx .and. me% y<= ny)
159+ if (inbounds) then
160+ me% m_contaminant(L)% c = DATASET% initialContaminantConcsSediment(me% x, me% y, :, :, :)
161+ if (allocated (DATASET% initialDissolvedConcsSediment)) then
162+ me% m_contaminant(L)% m_dissolved = DATASET% initialDissolvedConcsSediment(me% x, me% y)
163+ end if
164+ else
165+ me% m_contaminant(L)% c = 0.0_dp
166+ me% m_contaminant(L)% m_dissolved = 0.0_dp
167+ end if
168+ end if
169+ end do
182170
183- do L = 1 , C% nSedimentLayers
184- allocate (bsl1)
185- call r% addErrors(.errors. bsl1% create(L))
186- allocate (me% colBedSedimentLayers(L)% item, source= bsl1, stat= allst)
187- deallocate (bsl1)
171+ allocate (me% colBedSedimentLayers(C% nSedimentLayers), stat= allst)
188172 if (allst /= 0 ) then
189173 err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
190174 call r% addError(err (1 ))
191175 call LOGR% toFile(errors= err)
192176 return
193177 end if
194- if (r% hasCriticalError()) then
195- call r% addToTrace(tr)
178+
179+ me% n_delta_sed = C% nSedimentLayers + 3
180+ allocate (me% delta_sed(C% nSedimentLayers + 3 , C% nSedimentLayers + 3 , me% nSizeClasses), stat= allst)
181+ if (allst /= 0 ) then
182+ err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
183+ call r% addError(err (1 ))
184+ call LOGR% toFile(errors= err)
185+ return
186+ end if
187+ me% delta_sed = 0.0_dp
188+
189+ allocate (me% delta_sed_csr(C% nSizeClassesSpm), stat= allst)
190+ if (allst /= 0 ) then
191+ err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
192+ call r% addError(err (1 ))
193+ call LOGR% toFile(errors= err)
196194 return
197195 end if
198- end do
199- end function
196+
197+ do L = 1 , C% nSedimentLayers
198+ allocate (bsl1)
199+ call r% addErrors(.errors. bsl1% create(L))
200+ allocate (me% colBedSedimentLayers(L)% item, source= bsl1, stat= allst)
201+ deallocate (bsl1)
202+ if (allst /= 0 ) then
203+ err (1 ) = ErrorInstance(code= 1 , message= ms, trace= [tr])
204+ call r% addError(err (1 ))
205+ call LOGR% toFile(errors= err)
206+ return
207+ end if
208+ if (r% hasCriticalError()) then
209+ call r% addToTrace(tr)
210+ return
211+ end if
212+ end do
213+ end function
214+
200215
201216 ! > **Function purpose** <br>
202217 ! ! Deallocate all allocatable variables and call destroy methods for all
0 commit comments