Skip to content

Commit

Permalink
refac(histogram): make components private
Browse files Browse the repository at this point in the history
  • Loading branch information
rouson committed Jan 28, 2024
1 parent 0f1e3d1 commit c9fec55
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 22 deletions.
20 changes: 10 additions & 10 deletions cloud-microphysics/app/tensor-statistics.f90
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ subroutine compute_histograms(base_name)
]

block
integer line, h, file_unit
integer bin, h, file_unit
integer, parameter :: num_inputs = 7
type(histogram_t) :: histogram(num_inputs)

Expand All @@ -148,13 +148,13 @@ subroutine compute_histograms(base_name)

do h = 1, size(histogram)
write(file_unit,*), &
"# unmapped range for ", histogram(h)%variable_name,":", histogram(h)%unmapped_min, histogram(h)%unmapped_max
"# unmapped range for ", histogram(h)%variable_name(),":", histogram(h)%unmapped_range()
end do

write(file_unit,'(5x,a,8(10x,a))'),"bin", (histogram(h)%variable_name, h=1,size(histogram)) ! column headings
write(file_unit,'(5x,a,8(10x,a))'),"bin", (histogram(h)%variable_name(), h=1,size(histogram)) ! column headings

do line = 1, size(histogram(1)%bin_midpoint)
write(file_unit, *), histogram(1)%bin_midpoint(line), (histogram(h)%frequency(line), h=1,size(histogram))
do bin = 1, histogram(1)%num_bins()
write(file_unit, *), histogram(1)%bin_midpoint(bin), (histogram(h)%bin_frequency(bin), h=1,size(histogram))
end do

close(file_unit)
Expand Down Expand Up @@ -207,7 +207,7 @@ subroutine compute_histograms(base_name)
call assert(.not. any(ieee_is_nan(dqs_dt)), ".not. any(ieee_is_nan(dqs_dt)")

block
integer line, h, file_unit
integer bin, h, file_unit
integer, parameter :: num_outputs = 5
type(histogram_t) :: histogram(num_outputs)

Expand All @@ -226,13 +226,13 @@ subroutine compute_histograms(base_name)

do h = 1, size(histogram)
write(file_unit,*), &
"# unmapped range for ", histogram(h)%variable_name,":", histogram(h)%unmapped_min, histogram(h)%unmapped_max
"# unmapped range for ", histogram(h)%variable_name(),":", histogram(h)%unmapped_range()
end do

write(file_unit,'(5x,a,5(10x,a))'),"bin", (histogram(h)%variable_name, h=1,size(histogram)) ! column headings
write(file_unit,'(5x,a,5(10x,a))'),"bin", (histogram(h)%variable_name(), h=1,size(histogram)) ! column headings

do line = 1, size(histogram(1)%bin_midpoint)
write(file_unit, *), histogram(1)%bin_midpoint(line), (histogram(h)%frequency(line), h=1,size(histogram))
do bin = 1, histogram(1)%num_bins()
write(file_unit, *), histogram(1)%bin_midpoint(bin), (histogram(h)%bin_frequency(bin), h=1,size(histogram))
end do

close(file_unit)
Expand Down
51 changes: 47 additions & 4 deletions cloud-microphysics/src/histogram_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ module histogram_m
public :: histogram_t

type histogram_t
!private
character(len=:), allocatable :: variable_name
real unmapped_min, unmapped_max
real, allocatable :: frequency(:), bin_midpoint(:)
private
character(len=:), allocatable :: variable_name_
real unmapped_min_, unmapped_max_
real, allocatable :: frequency_(:), bin_midpoint_(:)
contains
procedure variable_name
procedure unmapped_range
procedure num_bins
procedure bin_midpoint
procedure bin_frequency
end type

interface histogram_t
Expand All @@ -25,4 +31,41 @@ pure module function histogram_on_unit_interval(v, variable_name, num_bins) resu

end interface

interface

pure module function num_bins(self) result(bins)
implicit none
class(histogram_t), intent(in) :: self
integer bins
end function

pure module function variable_name(self) result(name)
implicit none
class(histogram_t), intent(in) :: self
character(len=:), allocatable :: name
end function

pure module function unmapped_range(self) result(raw_range)
implicit none
class(histogram_t), intent(in) :: self
integer, parameter :: num_end_points = 2
real raw_range(num_end_points)
end function

pure module function bin_midpoint(self, bin) result(midpoint)
implicit none
class(histogram_t), intent(in) :: self
integer, intent(in) :: bin
real midpoint
end function

pure module function bin_frequency(self, bin) result(frequency)
implicit none
class(histogram_t), intent(in) :: self
integer, intent(in) :: bin
real frequency
end function

end interface

end module histogram_m
36 changes: 28 additions & 8 deletions cloud-microphysics/src/histogram_s.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@

contains

module procedure variable_name
name = self%variable_name_
end procedure

module procedure unmapped_range
raw_range = [self%unmapped_min_, self%unmapped_max_]
end procedure

module procedure num_bins
bins = size(self%bin_midpoint_)
end procedure

module procedure bin_midpoint
midpoint = self%bin_midpoint_(bin)
end procedure

module procedure bin_frequency
frequency = self%frequency_(bin)
end procedure

pure function normalize(x, x_min, x_max) result(x_normalized)
implicit none
real(rkind), intent(in) :: x(:,:,:,:), x_min, x_max
Expand All @@ -21,23 +41,23 @@ pure function normalize(x, x_min, x_max) result(x_normalized)
integer, allocatable :: in_bin(:)
integer i

histogram%variable_name = variable_name
histogram%unmapped_min = minval(v)
histogram%unmapped_max = maxval(v)
histogram%variable_name_ = variable_name
histogram%unmapped_min_ = minval(v)
histogram%unmapped_max_ = maxval(v)

allocate( histogram%frequency(num_bins))
allocate(histogram%bin_midpoint(num_bins))
allocate( histogram%frequency_(num_bins))
allocate(histogram%bin_midpoint_(num_bins))
allocate( in_bin(num_bins))

associate(v_min => (histogram%unmapped_min), v_max => (histogram%unmapped_max), cardinality => size(v))
associate(v_min => (histogram%unmapped_min_), v_max => (histogram%unmapped_max_), cardinality => size(v))
associate(v_mapped => normalize(v, v_min, v_max), dv => (v_mapped_max - v_mapped_min)/real(num_bins))
associate(v_bin_min => [(v_mapped_min + (i-1)*dv, i=1,num_bins)])
associate(smidgen => .0001*abs(dv)) ! use to make the high end of the bin range inclusive of the max value
associate(v_bin_max => [v_bin_min(2:), v_mapped_max + smidgen])
do concurrent(i = 1:num_bins)
in_bin(i) = count(v_mapped >= v_bin_min(i) .and. v_mapped < v_bin_max(i)) ! replace with Fortran 2023 reduction
histogram%frequency(i) = real(in_bin(i)) / real(cardinality)
histogram%bin_midpoint(i) = v_bin_min(i) + 0.5*dv
histogram%frequency_(i) = real(in_bin(i)) / real(cardinality)
histogram%bin_midpoint_(i) = v_bin_min(i) + 0.5*dv
end do
end associate
end associate
Expand Down

0 comments on commit c9fec55

Please sign in to comment.