read in 2d float arrays in yaml

test added too
This commit is contained in:
Sharan Roongta 2021-03-02 16:14:33 +01:00
parent f3a2c49b39
commit 357fd81be4
1 changed files with 67 additions and 27 deletions

View File

@ -73,7 +73,7 @@ module YAML_types
procedure :: & procedure :: &
contains => tNode_contains contains => tNode_contains
procedure :: & procedure :: &
get_table_asFloats => tNode_get_byKey_as2dFloats get_table_asFloats => tNode_get_byKey_as2dFloats !SR: Name needs to change
generic :: & generic :: &
get => tNode_get_byIndex, & get => tNode_get_byIndex, &
@ -129,7 +129,9 @@ module YAML_types
procedure :: asFormattedString => tList_asFormattedString procedure :: asFormattedString => tList_asFormattedString
procedure :: append => tList_append procedure :: append => tList_append
procedure :: & procedure :: &
asFloats => tList_asFloats asFloats => tList_asFloats
procedure :: &
as2dFloats => tList_as2dFloats
procedure :: & procedure :: &
asInts => tList_asInts asInts => tList_asInts
procedure :: & procedure :: &
@ -203,9 +205,11 @@ end subroutine YAML_types_init
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
subroutine selfTest subroutine selfTest
class(tNode), pointer :: s1,s2 class(tNode), pointer :: s1,s2,s3,s4
allocate(tScalar::s1) allocate(tScalar::s1)
allocate(tScalar::s2) allocate(tScalar::s2)
allocate(tScalar::s3)
allocate(tScalar::s4)
select type(s1) select type(s1)
class is(tScalar) class is(tScalar)
s1 = '1' s1 = '1'
@ -217,7 +221,9 @@ subroutine selfTest
end select end select
block block
class(tNode), pointer :: l1, l2, n class(tNode), pointer :: l1, l2, l3, n
real(pReal), allocatable, dimension(:,:) :: x
select type(s1) select type(s1)
class is(tScalar) class is(tScalar)
s1 = '2' s1 = '2'
@ -228,6 +234,17 @@ subroutine selfTest
s2 = '3' s2 = '3'
endselect endselect
select type(s3)
class is(tScalar)
s3 = '4'
endselect
select type(s4)
class is(tScalar)
s4 = '5'
endselect
allocate(tList::l1) allocate(tList::l1)
select type(l1) select type(l1)
class is(tList) class is(tList)
@ -240,12 +257,24 @@ subroutine selfTest
if(dNeq(n%get_asFloat(2),3.0_pReal)) error stop 'byIndex_asFloat' if(dNeq(n%get_asFloat(2),3.0_pReal)) error stop 'byIndex_asFloat'
endselect endselect
allocate(tList::l3)
select type(l3)
class is(tList)
call l3%append(s3)
call l3%append(s4)
endselect
allocate(tList::l2) allocate(tList::l2)
select type(l2) select type(l2)
class is(tList) class is(tList)
call l2%append(l1) call l2%append(l1)
if(any(l2%get_asInts(1) /= [2,3])) error stop 'byIndex_asInts' if(any(l2%get_asInts(1) /= [2,3])) error stop 'byIndex_asInts'
if(any(dNeq(l2%get_asFloats(1),[2.0_pReal,3.0_pReal]))) error stop 'byIndex_asFloats' if(any(dNeq(l2%get_asFloats(1),[2.0_pReal,3.0_pReal]))) error stop 'byIndex_asFloats'
call l2%append(l3)
x = l2%as2dFloats()
if(x(2,1)/= 4.0_pReal) error stop 'byKey_as2dFloats'
if(any(dNeq(pack(l2%as2dFloats(),.true.),&
[2.0_pReal,4.0_pReal,3.0_pReal,5.0_pReal]))) error stop 'byKey_as2dFloats'
n => l2 n => l2
end select end select
deallocate(n) deallocate(n)
@ -826,40 +855,28 @@ end function tNode_get_byKey_asFloats
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief Access by key and convert to float array !> @brief Access by key and convert to 2D float array
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
function tNode_get_byKey_as2dFloats(self,k) result(nodeAs2dFloats) function tNode_get_byKey_as2dFloats(self,k,defaultVal) result(nodeAs2dFloats)
class(tNode), intent(in), target :: self class(tNode), intent(in), target :: self
character(len=*), intent(in) :: k character(len=*), intent(in) :: k
real(pReal), intent(in), dimension(:,:), optional :: defaultVal
real(pReal), dimension(:,:), allocatable :: nodeAs2dFloats real(pReal), dimension(:,:), allocatable :: nodeAs2dFloats
class(tNode), pointer :: node,node_ class(tNode), pointer :: node
type(tList), pointer :: row_list,column_list type(tList), pointer :: rows
integer :: i,j
if(self%contains(k)) then if(self%contains(k)) then
node => self%get(k) node => self%get(k)
row_list => node%asList() rows => node%asList()
node_ => row_list%get(1) nodeAs2dFloats = rows%as2dFloats()
column_list => node_%asList() elseif(present(defaultVal)) then
allocate(nodeAs2dFloats(row_list%length,column_list%length),source=0.0_pReal) nodeAs2dFloats = defaultVal
else else
call IO_error(143,ext_msg=k) call IO_error(143,ext_msg=k)
endif endif
node => self%get(k)
row_list => node%asList()
do i=1,row_list%length
node_ => row_list%get(i)
column_list => node_%asList()
do j=1,column_list%length
nodeAs2dFloats(i,j) = column_list%get_asFloat(j)
enddo
enddo
end function tNode_get_byKey_as2dFloats end function tNode_get_byKey_as2dFloats
@ -1151,6 +1168,29 @@ function tList_asFloats(self)
end function tList_asFloats end function tList_asFloats
!--------------------------------------------------------------------------------------------------
!> @brief Convert to 2D float array
!--------------------------------------------------------------------------------------------------
function tList_as2dFloats(self)
class(tList), intent(in), target :: self
real(pReal), dimension(:,:), allocatable :: tList_as2dFloats
integer :: i
class(tNode), pointer :: row
type(tList), pointer :: row_data
row => self%get(1) !SR: some interface called 'shape' may be used?
row_data => row%asList()
allocate(tList_as2dFloats(row%length,row_data%length),source=0.0_pReal)
do i=1,self%length
tList_as2dFloats(i,:) = self%get_asFloats(i)
enddo
end function tList_as2dFloats
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------
!> @brief Convert to int array !> @brief Convert to int array
!-------------------------------------------------------------------------------------------------- !--------------------------------------------------------------------------------------------------