concept field in category fortran
appears as: field, Field, fields, The Field

This is an excerpt from Manning's book Modern Fortran: Building efficient parallel applications.
In math or physics, a field represents an assignment of values of some quantity (such as water height or velocity) to points in space.
Listing C.2. mod_field.f90, the module that defines the
Fieldderived typelanguage="fortran" linenumbering="unnumbered">module mod_field use iso_fortran_env, only: int32, real32 use mod_diff, only: diffx_real => diffx, diffy_real => diffy use mod_io, only: write_field use mod_parallel, only: tile_indices, tile_neighbors_2d implicit none private public :: Field, diffx, diffy type :: Field #1 character(:), allocatable :: name integer(int32) :: lb(2), ub(2) integer(int32) :: dims(2) integer(int32) :: neighbors(4) integer(int32) :: edge_size real(real32), allocatable :: data(:,:) contains #2 procedure, private, pass(self) :: assign_field, assign_real_scalar procedure, private, pass(self) :: field_add_field, field_add_real procedure, private, pass(self) :: field_sub_field, field_sub_real procedure, private, pass(self) :: field_mul_array, field_mul_real, & field_mul_field procedure, private, pass(self) :: field_div_real procedure, public, pass(self) :: gather procedure, public, pass(self) :: init_gaussian procedure, public, pass(self) :: sync_edges procedure, public, pass(self) :: write generic :: assignment(=) => assign_field, assign_real_scalar generic :: operator(+) => field_add_field, field_add_real generic :: operator(-) => field_sub_field, field_sub_real generic :: operator(*) => field_mul_array, field_mul_real, & field_mul_field generic :: operator(/) => field_div_real end type Field interface Field #3 module procedure :: field_constructor #3 end interface Field #3 contains type(Field) function field_constructor(name, dims) result(self) character(*), intent(in) :: name integer(int32), intent(in) :: dims(2) integer(int32) :: edge_size, indices(4) self % name = name self % dims = dims indices = tile_indices(dims) self % lb = indices([1, 3]) self % ub = indices([2, 4]) allocate(self % data(self % lb(1)-1:self % ub(1)+1,& self % lb(2)-1:self % ub(2)+1)) self % data = 0 self % neighbors = tile_neighbors_2d(periodic=.true.) self % edge_size = max(self % ub(1)-self % lb(1)+1,& self % ub(2)-self % lb(2)+1) call co_max(self % edge_size) end function field_constructor subroutine assign_field(self, f) #4 class(Field), intent(in out) :: self class(Field), intent(in) :: f call from_field(self, f) call self % sync_edges() end subroutine assign_field pure subroutine assign_real_scalar(self, a) #4 class(Field), intent(in out) :: self real(real32), intent(in) :: a self % data = a end subroutine assign_real_scalar pure function diffx(input_field) #5 class(Field), intent(in) :: input_field real(real32), allocatable :: diffx(:,:) diffx = diffx_real(input_field % data) end function diffx pure function diffy(input_field) #5 class(Field), intent(in) :: input_field real(real32), allocatable :: diffy(:,:) diffy = diffy_real(input_field % data) end function diffy pure subroutine from_field(target, source) type(Field), intent(in out) :: target type(Field), intent(in) :: source target % name = source % name target % lb = source % lb target % ub = source % ub target % dims = source % dims target % neighbors = source % neighbors target % edge_size = source % edge_size target % data = source % data end subroutine from_field function gather(self, image) #6 class(Field), intent(in) :: self integer(int32), intent(in) :: image real(real32), allocatable :: gather_coarray(:,:)[:] real(real32) :: gather(self % dims(1), self % dims(2)) allocate(gather_coarray(self % dims(1), self % dims(2))[*]) associate(is => self % lb(1), ie => self % ub(1),& js => self % lb(2), je => self % ub(2)) gather_coarray(is:ie, js:je)[image] = self % data(is:ie, js:je) sync all if (this_image() == image) gather = gather_coarray end associate deallocate(gather_coarray) end function gather subroutine init_gaussian(self, decay, ic, jc) #7 class(Field), intent(in out) :: self real(real32), intent(in) :: decay integer(int32), intent(in) :: ic, jc integer(int32) :: i, j do concurrent(i = self % lb(1):self % ub(1),& j = self % lb(2):self % ub(2)) self % data(i, j) = exp(-decay * ((i - ic)**2 + (j - jc)**2)) end do call self % sync_edges() end subroutine init_gaussian pure type(Field) & #8 function field_add_field(self, f) result(res) #8 class(Field), intent(in) :: self, f call from_field(res, self) res % data = self % data + f % data end function field_add_field pure type(Field) & #8 function field_add_real(self, x) result(res) #8 class(Field), intent(in) :: self real(real32), intent(in) :: x(:,:) call from_field(res, self) res % data = self % data + x end function field_add_real pure type(Field) & #8 function field_div_real(self, x) result(res) #8 class(Field), intent(in) :: self real(real32), intent(in) :: x call from_field(res, self) res % data = self % data / x end function field_div_real pure type(Field) & #8 function field_mul_array(self, x) result(res) #8 class(Field), intent(in) :: self real(real32), intent(in) :: x(:,:) call from_field(res, self) res % data = self % data * x end function field_mul_array pure type(Field) & #8 function field_mul_real(self, x) result(res) #8 class(Field), intent(in) :: self real(real32), intent(in) :: x call from_field(res, self) res % data = self % data * x end function field_mul_real pure type(Field) & #8 function field_mul_field(self, f) result(res) #8 class(Field), intent(in) :: self, f call from_field(res, self) res % data = self % data * f % data end function field_mul_field pure type(Field) & #8 function field_sub_real(self, x) result(res) #8 class(Field), intent(in) :: self real(real32), intent(in) :: x(:,:) call from_field(res, self) res % data = self % data - x end function field_sub_real pure type(Field) & #8 function field_sub_field(self, f) result(res) #8 class(Field), intent(in) :: self, f call from_field(res, self) res % data = self % data - f % data end function field_sub_field subroutine sync_edges(self) #9 class(Field), intent(in out) :: self real(real32), allocatable, save :: edge(:,:)[:] associate(is => self % lb(1), ie => self % ub(1),& js => self % lb(2), je => self % ub(2),& neighbors => self % neighbors) if (.not. allocated(edge)) & allocate(edge(self % edge_size, 4)[*]) sync images(set(neighbors)) edge(1:je-js+1,1)[neighbors(1)] = self % data(is,js:je) edge(1:je-js+1,2)[neighbors(2)] = self % data(ie,js:je) edge(1:ie-is+1,3)[neighbors(3)] = self % data(is:ie,js) edge(1:ie-is+1,4)[neighbors(4)] = self % data(is:ie,je) sync images(set(neighbors)) self % data(is-1,js:je) = edge(1:je-js+1,2) self % data(ie+1,js:je) = edge(1:je-js+1,1) self % data(is:ie,js-1) = edge(1:ie-is+1,4) self % data(is:ie,je+1) = edge(1:ie-is+1,3) end associate end subroutine sync_edges subroutine write(self, n) #10 class(Field), intent(in) :: self integer(int32), intent(in) :: n real(real32), allocatable :: gather(:,:) gather = self % gather(1) if (this_image() == 1) call write_field(gather, self % name, n) end subroutine write pure recursive function set(a) result(res) #11 integer, intent(in) :: a(:) integer, allocatable :: res(:) if (size(a) > 1) then res = [a(1), set(pack(a(2:), .not. a(2:) == a(1)))] else res = a end if end function set end module mod_field