Skip to content

Commit f4476a7

Browse files
committed
Add ncol_ to aerosol_state for internal loops
1 parent 7331e72 commit f4476a7

6 files changed

Lines changed: 52 additions & 15 deletions

File tree

src/chemistry/aerosol/aerosol_instances_mod.F90

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,21 +272,21 @@ subroutine aerosol_instances_init_states(phys_state, pbuf2d)
272272
iaermod = iaermod + 1
273273
if (associated(aero_props_all(iaermod, ilist)%obj)) then
274274
aero_states_all(iaermod, ilist, lchnk)%obj => &
275-
modal_aerosol_state(phys_state(lchnk), pbuf, ilist)
275+
modal_aerosol_state(phys_state(lchnk)%ncol, phys_state(lchnk), pbuf, ilist)
276276
end if
277277
end if
278278
if (carma_active_) then
279279
iaermod = iaermod + 1
280280
if (associated(aero_props_all(iaermod, ilist)%obj)) then
281281
aero_states_all(iaermod, ilist, lchnk)%obj => &
282-
carma_aerosol_state(phys_state(lchnk), pbuf, ilist)
282+
carma_aerosol_state(phys_state(lchnk)%ncol, phys_state(lchnk), pbuf, ilist)
283283
end if
284284
end if
285285
if (bulk_active_) then
286286
iaermod = iaermod + 1
287287
if (associated(aero_props_all(iaermod, ilist)%obj)) then
288288
aero_states_all(iaermod, ilist, lchnk)%obj => &
289-
bulk_aerosol_state(phys_state(lchnk), pbuf, ilist)
289+
bulk_aerosol_state(phys_state(lchnk)%ncol, phys_state(lchnk), pbuf, ilist)
290290
end if
291291
end if
292292
end do
@@ -342,15 +342,15 @@ subroutine aerosol_instances_create_states(list_idx, state, pbuf, aero_states, n
342342
iaermod = 0
343343
if (modal_active_) then
344344
iaermod = iaermod + 1
345-
aero_states(iaermod)%obj => modal_aerosol_state(state, pbuf, list_idx)
345+
aero_states(iaermod)%obj => modal_aerosol_state(state%ncol, state, pbuf, list_idx)
346346
end if
347347
if (carma_active_) then
348348
iaermod = iaermod + 1
349-
aero_states(iaermod)%obj => carma_aerosol_state(state, pbuf, list_idx)
349+
aero_states(iaermod)%obj => carma_aerosol_state(state%ncol, state, pbuf, list_idx)
350350
end if
351351
if (bulk_active_) then
352352
iaermod = iaermod + 1
353-
aero_states(iaermod)%obj => bulk_aerosol_state(state, pbuf, list_idx)
353+
aero_states(iaermod)%obj => bulk_aerosol_state(state%ncol, state, pbuf, list_idx)
354354
end if
355355

356356
end subroutine aerosol_instances_create_states

src/chemistry/aerosol/aerosol_state_mod.F90

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ module aerosol_state_mod
2525
!! class can be extended for a specific aerosol package.
2626
type, abstract :: aerosol_state
2727
integer :: list_idx_ = 0 ! radiation climate/diagnostic list index
28+
integer :: ncol_ = 0 ! number of active columns
2829
contains
2930
procedure :: list_idx => get_list_idx
3031
procedure :: set_list_idx
32+
procedure :: ncol => get_ncol
33+
procedure :: set_ncol
3134
procedure(aero_get_transported), deferred :: get_transported
3235
procedure(aero_set_transported), deferred :: set_transported
3336
procedure(aero_get_amb_total_bin_mmr), deferred :: ambient_total_bin_mmr
@@ -300,6 +303,23 @@ subroutine set_list_idx(self, list_idx)
300303
self%list_idx_ = list_idx
301304
end subroutine set_list_idx
302305

306+
!------------------------------------------------------------------------------
307+
! returns the number of active columns
308+
!------------------------------------------------------------------------------
309+
pure integer function get_ncol(self)
310+
class(aerosol_state), intent(in) :: self
311+
get_ncol = self%ncol_
312+
end function get_ncol
313+
314+
!------------------------------------------------------------------------------
315+
! sets the number of active columns
316+
!------------------------------------------------------------------------------
317+
subroutine set_ncol(self, ncol)
318+
class(aerosol_state), intent(inout) :: self
319+
integer, intent(in) :: ncol
320+
self%ncol_ = ncol
321+
end subroutine set_ncol
322+
303323
!------------------------------------------------------------------------------
304324
! returns aerosol number, volume concentrations, and bulk hygroscopicity
305325
!------------------------------------------------------------------------------

src/chemistry/aerosol/bulk_aerosol_state_mod.F90

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ module bulk_aerosol_state_mod
7676

7777
!------------------------------------------------------------------------------
7878
!------------------------------------------------------------------------------
79-
function constructor(state,pbuf,list_idx) result(newobj)
79+
function constructor(ncol,state,pbuf,list_idx) result(newobj)
8080
!REMOVECAM: host-model specific dimensions
8181
use ppgrid, only: pcols, pver
8282
!REMOVECAM_END
8383

84+
integer, intent(in) :: ncol
8485
type(physics_state), target :: state
8586
type(physics_buffer_desc), pointer :: pbuf(:)
8687
integer, intent(in), optional :: list_idx
@@ -97,12 +98,17 @@ function constructor(state,pbuf,list_idx) result(newobj)
9798
newobj%state => state
9899
newobj%pbuf => pbuf
99100

101+
! set number of active columns internally to prevent loops from accessing beyond
102+
! meaningful data in arrays
103+
call newobj%set_ncol(ncol)
104+
100105
if (present(list_idx)) call newobj%set_list_idx(list_idx)
101106

102107
! Allocate per-object workspace for derived number fields.
103108
! Thread-safe: in CAM, each chunk has its own state object.
104109
allocate(newobj%num_work_(pcols, pver), stat=ierr)
105110
if (ierr /= 0) call endrun('bulk_aerosol_state constructor: num_work_ allocation error')
111+
newobj%num_work_(:,:) = 0._r8
106112
allocate(newobj%zero_fld_(pcols, pver), stat=ierr)
107113
if (ierr /= 0) call endrun('bulk_aerosol_state constructor: zero_fld_ allocation error')
108114
newobj%zero_fld_(:,:) = 0._r8
@@ -209,20 +215,24 @@ subroutine get_ambient_num(self, bin_ndx, num)
209215
real(r8), pointer :: mmr(:,:)
210216
real(r8) :: ntm
211217
character(len=32) :: aname
218+
integer :: nc
212219

213220
! Derive number mixing ratio from mass: num = mmr * num_to_mass_aer (* bam_sulfate_scale for sulfate).
214221
! This matches the inline computation formerly in microp_aero.F90 and nucleate_ice_cam.F90.
215222
! Computed into per-object workspace (num_work_); callers must use or copy before the next call.
223+
! Only active columns (1:ncol) are computed to avoid FPE on uninitialised padding columns.
224+
225+
nc = self%ncol()
216226

217227
call rad_cnst_get_aer_mmr(self%list_idx_, bin_ndx, self%state, self%pbuf, mmr)
218228
call rad_aer_get_props(self%list_idx_, bin_ndx, num_to_mass_aer=ntm, aername=aname)
219229

220230
! Apply bam_sulfate_scale to sulfate/volcanic aerosol
221231
select case ( to_lower( aname(:4) ) )
222232
case ('sulf', 'volc') ! both treated as 'sulfate' in aero_props%get type.
223-
self%num_work_(:,:) = mmr(:,:) * ntm * bam_sulfate_scale
233+
self%num_work_(:nc,:) = mmr(:nc,:) * ntm * bam_sulfate_scale
224234
case default
225-
self%num_work_(:,:) = mmr(:,:) * ntm
235+
self%num_work_(:nc,:) = mmr(:nc,:) * ntm
226236
end select
227237

228238
num => self%num_work_

src/chemistry/aerosol/carma_aerosol_state_mod.F90

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ module carma_aerosol_state_mod
6767

6868
!------------------------------------------------------------------------------
6969
!------------------------------------------------------------------------------
70-
function constructor(state,pbuf,list_idx) result(newobj)
70+
function constructor(ncol,state,pbuf,list_idx) result(newobj)
71+
integer, intent(in) :: ncol
7172
type(physics_state), target, optional :: state
7273
type(physics_buffer_desc), pointer, optional :: pbuf(:)
7374
integer, intent(in), optional :: list_idx
@@ -85,6 +86,10 @@ function constructor(state,pbuf,list_idx) result(newobj)
8586
newobj%state => state
8687
newobj%pbuf => pbuf
8788

89+
! set number of active columns internally to prevent loops from accessing beyond
90+
! meaningful data in arrays
91+
call newobj%set_ncol(ncol)
92+
8893
if (present(list_idx)) call newobj%set_list_idx(list_idx)
8994

9095
end function constructor
@@ -190,7 +195,7 @@ subroutine get_ambient_num(self, bin_ndx, num)
190195
integer :: igroup, ibin, rc, nchr, ncol
191196
real(r8) :: nmr(pcols,pver)
192197

193-
ncol = self%state%ncol
198+
ncol = self%ncol()
194199

195200
call rad_aer_get_info_by_bin(self%list_idx_, bin_ndx, bin_name=bin_name)
196201

@@ -221,7 +226,7 @@ subroutine get_cldbrne_num(self, bin_ndx, num)
221226
integer :: igroup, ibin, rc, nchr, ncol
222227
real(r8) :: nmr(pcols,pver)
223228

224-
ncol = self%state%ncol
229+
ncol = self%ncol()
225230

226231
call rad_aer_get_info_by_bin(self%list_idx_, bin_ndx, bin_name=bin_name)
227232

@@ -323,7 +328,7 @@ subroutine icenuc_size_wght_val(self, bin_ndx, col_ndx, lyr_ndx, species_type, u
323328

324329
real(r8) :: wght_arr(pcols,pver)
325330

326-
call self%icenuc_size_wght(bin_ndx, self%state%ncol, pver, species_type, use_preexisting_ice, wght_arr)
331+
call self%icenuc_size_wght(bin_ndx, self%ncol(), pver, species_type, use_preexisting_ice, wght_arr)
327332

328333
wght = wght_arr(col_ndx,lyr_ndx)
329334

src/chemistry/aerosol/modal_aerosol_state_mod.F90

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ module modal_aerosol_state_mod
6464

6565
!------------------------------------------------------------------------------
6666
!------------------------------------------------------------------------------
67-
function constructor(state,pbuf,list_idx) result(newobj)
67+
function constructor(ncol,state,pbuf,list_idx) result(newobj)
68+
integer, intent(in) :: ncol
6869
type(physics_state), target :: state
6970
type(physics_buffer_desc), pointer :: pbuf(:)
7071
integer, intent(in), optional :: list_idx
@@ -79,6 +80,7 @@ function constructor(state,pbuf,list_idx) result(newobj)
7980
return
8081
end if
8182

83+
call newobj%set_ncol(ncol)
8284
newobj%state => state
8385
newobj%pbuf => pbuf
8486

src/chemistry/carma_aero/carma_aero_gasaerexch.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ subroutine carma_aero_gasaerexch_sub( state, &
412412
type(carma_aerosol_state), pointer :: aero_state
413413

414414
!----------------------------------------------------------------------
415-
aero_state => carma_aerosol_state(state, pbuf)
415+
aero_state => carma_aerosol_state(ncol, state, pbuf)
416416

417417
! map CARMA soa to working soa(nbins,nsoa)
418418

0 commit comments

Comments
 (0)