-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #764 from Ivanou34/meshgrid
Addition of the `meshgrid` subroutine in `stdlib_math`
- Loading branch information
Showing
8 changed files
with
297 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
program example_meshgrid | ||
|
||
use stdlib_math, only: meshgrid, linspace, stdlib_meshgrid_ij | ||
use stdlib_kinds, only: sp | ||
|
||
implicit none | ||
|
||
integer, parameter :: nx = 3, ny = 2 | ||
real(sp) :: x(nx), y(ny), & | ||
xm_cart(ny, nx), ym_cart(ny, nx), & | ||
xm_mat(nx, ny), ym_mat(nx, ny) | ||
|
||
x = linspace(0_sp, 1_sp, nx) | ||
y = linspace(0_sp, 1_sp, ny) | ||
|
||
call meshgrid(x, y, xm_cart, ym_cart) | ||
print *, "xm_cart = " | ||
call print_2d_array(xm_cart) | ||
print *, "ym_cart = " | ||
call print_2d_array(ym_cart) | ||
|
||
call meshgrid(x, y, xm_mat, ym_mat, indexing=stdlib_meshgrid_ij) | ||
print *, "xm_mat = " | ||
call print_2d_array(xm_mat) | ||
print *, "ym_mat = " | ||
call print_2d_array(ym_mat) | ||
|
||
contains | ||
subroutine print_2d_array(array) | ||
real(sp), intent(in) :: array(:, :) | ||
integer :: i | ||
|
||
do i = 1, size(array, dim=1) | ||
print *, array(i, :) | ||
end do | ||
end subroutine | ||
end program example_meshgrid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#:include "common.fypp" | ||
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES | ||
#:set RANKS = range(1, MAXRANK + 1) | ||
|
||
#:def meshgrid_loop(indices) | ||
#:for j in reversed(indices) | ||
do i${j}$ = 1, size(x${j}$) | ||
#:endfor | ||
#:for j in indices | ||
xm${j}$(${"".join(f"i{j}," for j in indices).removesuffix(",")}$) = & | ||
x${j}$(i${j}$) | ||
#:endfor | ||
#:for j in indices | ||
end do | ||
#:endfor | ||
#:enddef | ||
|
||
submodule(stdlib_math) stdlib_math_meshgrid | ||
|
||
use stdlib_error, only: error_stop | ||
|
||
contains | ||
|
||
#:for k1, t1 in IR_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:if rank == 1 | ||
#:set XY_INDICES = [1] | ||
#:set IJ_INDICES = [1] | ||
#:else | ||
#:set XY_INDICES = [2, 1] + [j for j in range(3, rank + 1)] | ||
#:set IJ_INDICES = [1, 2] + [j for j in range(3, rank + 1)] | ||
#:endif | ||
#: set RName = rname("meshgrid", rank, t1, k1) | ||
module procedure ${RName}$ | ||
|
||
integer :: ${"".join(f"i{j}," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
|
||
select case (optval(indexing, stdlib_meshgrid_xy)) | ||
case (stdlib_meshgrid_xy) | ||
$:meshgrid_loop(XY_INDICES) | ||
case (stdlib_meshgrid_ij) | ||
$:meshgrid_loop(IJ_INDICES) | ||
case default | ||
call error_stop("ERROR (meshgrid): unexpected indexing.") | ||
end select | ||
end procedure | ||
#:endfor | ||
#:endfor | ||
|
||
end submodule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
set( | ||
fppFiles | ||
"test_stdlib_math.fypp" | ||
"test_meshgrid.fypp" | ||
) | ||
fypp_f90("${fyppFlags}" "${fppFiles}" outFiles) | ||
|
||
ADDTEST(stdlib_math) | ||
ADDTEST(linspace) | ||
ADDTEST(logspace) | ||
ADDTEST(meshgrid) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
! SPDX-Identifier: MIT | ||
|
||
#:include "common.fypp" | ||
#:set IR_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES | ||
#:set RANKS = range(1, MAXRANK + 1) | ||
#:set INDEXINGS = ["default", "xy", "ij"] | ||
|
||
#:def OPTIONAL_PART_IN_SIGNATURE(indexing) | ||
#:if indexing in ("xy", "ij") | ||
${f', stdlib_meshgrid_{indexing}'}$ | ||
#:endif | ||
#:enddef | ||
|
||
module test_meshgrid | ||
use testdrive, only : new_unittest, unittest_type, error_type, check | ||
use stdlib_math, only: meshgrid, stdlib_meshgrid_ij, stdlib_meshgrid_xy | ||
use stdlib_kinds, only: int8, int16, int32, int64, sp, dp, xdp, qp | ||
implicit none | ||
|
||
public :: collect_meshgrid | ||
|
||
contains | ||
|
||
!> Collect all exported unit tests | ||
subroutine collect_meshgrid(testsuite) | ||
!> Collection of tests | ||
type(unittest_type), allocatable, intent(out) :: testsuite(:) | ||
|
||
testsuite = [ & | ||
#:for k1, t1 in IR_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:for INDEXING in INDEXINGS | ||
#: set RName = rname(f"meshgrid_{INDEXING}", rank, t1, k1) | ||
new_unittest("${RName}$", test_${RName}$), & | ||
#:endfor | ||
#:endfor | ||
#:endfor | ||
new_unittest("dummy", test_dummy) & | ||
] | ||
|
||
end subroutine collect_meshgrid | ||
|
||
#:for k1, t1 in IR_KINDS_TYPES | ||
#:for rank in RANKS | ||
#:for INDEXING in INDEXINGS | ||
#:if rank == 1 | ||
#:set INDICES = [1] | ||
#:else | ||
#:if INDEXING in ("default", "xy") | ||
#:set INDICES = [2, 1] + [j for j in range(3, rank + 1)] | ||
#:elif INDEXING == "ij" | ||
#:set INDICES = [1, 2] + [j for j in range(3, rank + 1)] | ||
#:endif | ||
#:endif | ||
#:set RName = rname(f"meshgrid_{INDEXING}", rank, t1, k1) | ||
#:set GRIDSHAPE = "".join("length," for j in range(rank)).removesuffix(",") | ||
subroutine test_${RName}$(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
integer, parameter :: length = 3 | ||
${t1}$ :: ${"".join(f"x{j}(length)," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
${t1}$ :: ${"".join(f"xm{j}({GRIDSHAPE})," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
${t1}$ :: ${"".join(f"xm{j}_exact({GRIDSHAPE})," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
integer :: i | ||
integer :: ${"".join(f"i{j}," for j in range(1, rank + 1)).removesuffix(",")}$ | ||
${t1}$, parameter :: ZERO = 0 | ||
! valid test case | ||
#:for index in range(1, rank + 1) | ||
x${index}$ = [(i, i = length * ${index - 1}$ + 1, length * ${index}$)] | ||
#:endfor | ||
#:for j in range(1, rank + 1) | ||
xm${j}$_exact = reshape( & | ||
[${"".join("(" for dummy in range(rank)) + f"x{j}(i{j})" + "".join(f", i{index} = 1, size(x{index}))" for index in INDICES)}$], & | ||
shape=[${GRIDSHAPE}$] & | ||
) | ||
#:endfor | ||
call meshgrid( & | ||
${"".join(f"x{j}," for j in range(1, rank + 1))}$ & | ||
${"".join(f"xm{j}," for j in range(1, rank + 1)).removesuffix(",")}$ & | ||
${OPTIONAL_PART_IN_SIGNATURE(INDEXING)}$ ) | ||
#:for j in range(1, rank + 1) | ||
call check(error, maxval(abs(xm${j}$ - xm${j}$_exact)), ZERO) | ||
if (allocated(error)) return | ||
#:endfor | ||
end subroutine test_${RName}$ | ||
#:endfor | ||
#:endfor | ||
#:endfor | ||
|
||
subroutine test_dummy(error) | ||
!> Error handling | ||
type(error_type), allocatable, intent(out) :: error | ||
end subroutine | ||
|
||
end module test_meshgrid | ||
|
||
program tester | ||
use, intrinsic :: iso_fortran_env, only : error_unit | ||
use testdrive, only : run_testsuite, new_testsuite, testsuite_type | ||
use test_meshgrid, only : collect_meshgrid | ||
implicit none | ||
integer :: stat, is | ||
type(testsuite_type), allocatable :: testsuites(:) | ||
character(len=*), parameter :: fmt = '("#", *(1x, a))' | ||
|
||
stat = 0 | ||
|
||
testsuites = [ & | ||
new_testsuite("meshgrid", collect_meshgrid) & | ||
] | ||
|
||
do is = 1, size(testsuites) | ||
write(error_unit, fmt) "Testing:", testsuites(is)%name | ||
call run_testsuite(testsuites(is)%collect, error_unit, stat) | ||
end do | ||
|
||
if (stat > 0) then | ||
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!" | ||
error stop | ||
end if | ||
end program tester |