From 00671f943bfe41e77f303238909e14b056dcfbad Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 13 Apr 2022 23:45:14 +0200 Subject: [PATCH 1/9] easier to understand --- src/YAML_types.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/YAML_types.f90 b/src/YAML_types.f90 index d7ca71cbb..c4dcc0a83 100644 --- a/src/YAML_types.f90 +++ b/src/YAML_types.f90 @@ -1348,8 +1348,8 @@ subroutine tList_append(self,node) type(tItem), pointer :: item if (.not. associated(self%first)) then - allocate(self%first) - item => self%first + allocate(item) + self%first => item else item => self%first do while (associated(item%next)) From ca0df3389a96c7d867c0e884128662a3ee9a4c37 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 14 Apr 2022 00:06:54 +0200 Subject: [PATCH 2/9] avoid long pointer transversal --- src/YAML_types.f90 | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/YAML_types.f90 b/src/YAML_types.f90 index c4dcc0a83..b32651905 100644 --- a/src/YAML_types.f90 +++ b/src/YAML_types.f90 @@ -119,7 +119,8 @@ module YAML_types type, extends(tNode), public :: tList - class(tItem), pointer :: first => NULL() + class(tItem), pointer :: first => NULL(), & + last => NULL() contains procedure :: asFormattedString => tList_asFormattedString @@ -1350,13 +1351,11 @@ subroutine tList_append(self,node) if (.not. associated(self%first)) then allocate(item) self%first => item + self%last => item else - item => self%first - do while (associated(item%next)) - item => item%next - enddo - allocate(item%next) - item => item%next + allocate(self%last%next) + item => self%last%next + self%last => item end if item%node => node From 563d566a3f699af85a8fe558ebf7c724d4fd4756 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 14 Apr 2022 01:06:47 +0200 Subject: [PATCH 3/9] made two loops faster the most annoying one is still slow. --- src/YAML_types.f90 | 2 +- src/material.f90 | 50 ++++++++++++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/YAML_types.f90 b/src/YAML_types.f90 index b32651905..857b24bd0 100644 --- a/src/YAML_types.f90 +++ b/src/YAML_types.f90 @@ -145,7 +145,7 @@ module YAML_types end type tDict - type :: tItem + type, public :: tItem character(len=:), allocatable :: key class(tNode), pointer :: node => NULL() class(tItem), pointer :: next => NULL() diff --git a/src/material.f90 b/src/material.f90 index 3ff280b0d..4d6bc361d 100644 --- a/src/material.f90 +++ b/src/material.f90 @@ -91,6 +91,7 @@ subroutine parse() homogenizations, & homogenization + class(tItem), pointer :: item integer, dimension(:), allocatable :: & counterPhase, & counterHomogenization @@ -102,6 +103,7 @@ subroutine parse() co, ce, & ma + materials => config_material%get('material') phases => config_material%get('phase') homogenizations => config_material%get('homogenization') @@ -169,17 +171,23 @@ subroutine parse() allocate(material_O_0(materials%length)) allocate(material_F_i_0(materials%length)) - do ma = 1, materials%length - material => materials%get(ma) - constituents => material%get('constituents') - allocate(material_O_0(ma)%data(constituents%length)) - allocate(material_F_i_0(ma)%data(1:3,1:3,constituents%length)) - do co = 1, constituents%length - constituent => constituents%get(co) - call material_O_0(ma)%data(co)%fromQuaternion(constituent%get_as1dFloat('O',requiredSize=4)) - material_F_i_0(ma)%data(1:3,1:3,co) = constituent%get_as2dFloat('F_i',defaultVal=math_I3,requiredShape=[3,3]) - enddo - enddo + ! manual iteration for performance + select type(materials) + class is(tList) + item => materials%first + do ma = 1, materials%length + material => item%node + constituents => material%get('constituents') + allocate(material_O_0(ma)%data(constituents%length)) + allocate(material_F_i_0(ma)%data(1:3,1:3,constituents%length)) + do co = 1, constituents%length + constituent => constituents%get(co) + call material_O_0(ma)%data(co)%fromQuaternion(constituent%get_as1dFloat('O',requiredSize=4)) + material_F_i_0(ma)%data(1:3,1:3,co) = constituent%get_as2dFloat('F_i',defaultVal=math_I3,requiredShape=[3,3]) + end do + item => item%next + end do + end select end subroutine parse @@ -195,17 +203,25 @@ subroutine sanityCheck(materials,homogenizations) class(tNode), pointer :: material, & homogenization, & constituents + class(tItem), pointer :: item integer :: m + if (maxval(discretization_materialAt) > materials%length) & call IO_error(155,ext_msg='More materials requested than found in material.yaml') - do m = 1, materials%length - material => materials%get(m) - constituents => material%get('constituents') - homogenization => homogenizations%get(material%get_asString('homogenization')) - if (constituents%length /= homogenization%get_asInt('N_constituents')) call IO_error(148) - end do + ! manual iteration for performance + select type(materials) + class is(tList) + item => materials%first + do m = 1, materials%length + material => materials%get(m) + constituents => material%get('constituents') + homogenization => homogenizations%get(material%get_asString('homogenization')) + if (constituents%length /= homogenization%get_asInt('N_constituents')) call IO_error(148) + item => item%next + end do + end select end subroutine sanityCheck From 3309360e4bf96ee32d156b825098b8e573cbea67 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 14 Apr 2022 07:19:56 +0200 Subject: [PATCH 4/9] avoit out of bounds access --- src/YAML_parse.f90 | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/YAML_parse.f90 b/src/YAML_parse.f90 index e589758c9..8013089e1 100644 --- a/src/YAML_parse.f90 +++ b/src/YAML_parse.f90 @@ -168,8 +168,11 @@ logical function quotedString(line) character(len=*), intent(in) :: line + quotedString = .false. + if (len(line) == 0) return + if (scan(line(:1),IO_QUOTES) == 1) then quotedString = .true. if(line(len(line):len(line)) /= line(:1)) call IO_error(710,ext_msg=line) From b56176d670d22616ec512e7c0c11326849f8aaf1 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 14 Apr 2022 12:55:11 +0200 Subject: [PATCH 5/9] all YAML related initialization is constant (linear in number of points) --- src/material.f90 | 125 +++++++++++++++++++++-------------------------- 1 file changed, 56 insertions(+), 69 deletions(-) diff --git a/src/material.f90 b/src/material.f90 index 4d6bc361d..40d178540 100644 --- a/src/material.f90 +++ b/src/material.f90 @@ -94,7 +94,10 @@ subroutine parse() class(tItem), pointer :: item integer, dimension(:), allocatable :: & counterPhase, & - counterHomogenization + counterHomogenization, & + temp_ho + integer, dimension(:,:), allocatable :: temp_ph + real(pReal), dimension(:,:), allocatable :: temp_v real(pReal) :: v integer :: & @@ -108,8 +111,6 @@ subroutine parse() phases => config_material%get('phase') homogenizations => config_material%get('homogenization') - call sanityCheck(materials, homogenizations) - #if defined (__GFORTRAN__) material_name_phase = getKeys(phases) material_name_homogenization = getKeys(homogenizations) @@ -136,10 +137,56 @@ subroutine parse() allocate(material_v(homogenization_maxNconstituents,discretization_Ncells),source=0.0_pReal) - do el = 1, discretization_Nelems - material => materials%get(discretization_materialAt(el)) + allocate(material_O_0(materials%length)) + allocate(material_F_i_0(materials%length)) + + select type(materials) + + class is(tList) + + if (maxval(discretization_materialAt) > materials%length) & + call IO_error(155,ext_msg='More materials requested than found in material.yaml') + + allocate(temp_ho(materials%length)) + allocate(temp_ph(materials%length,homogenization_maxNconstituents),source=-1) + allocate(temp_v(materials%length,homogenization_maxNconstituents),source=0.0_pReal) + + item => materials%first + do ma = 1, materials%length + material => item%node + temp_ho(ma) = homogenizations%getIndex(material%get_asString('homogenization')) + constituents => material%get('constituents') + + homogenization => homogenizations%get(temp_ho(ma)) + if (constituents%length /= homogenization%get_asInt('N_constituents')) call IO_error(148) + + allocate(material_O_0(ma)%data(constituents%length)) + allocate(material_F_i_0(ma)%data(1:3,1:3,constituents%length)) + + do co = 1, constituents%length + constituent => constituents%get(co) + temp_v(ma,co) = constituent%get_asFloat('v') + temp_ph(ma,co) = phases%getIndex(constituent%get_asString('phase')) + + call material_O_0(ma)%data(co)%fromQuaternion(constituent%get_as1dFloat('O',requiredSize=4)) + material_F_i_0(ma)%data(1:3,1:3,co) = constituent%get_as2dFloat('F_i',defaultVal=math_I3,requiredShape=[3,3]) + + end do + if (dNeq(sum(temp_v(ma,:)),1.0_pReal,1.e-9_pReal)) call IO_error(153,ext_msg='constituent') + + item => item%next + + end do + + end select + + + ! build mappings + do el = 1, discretization_Nelems + + ma = discretization_materialAt(el) + ho = temp_ho(ma) - ho = homogenizations%getIndex(material%get_asString('homogenization')) do ip = 1, discretization_nIPs ce = (el-1)*discretization_nIPs + ip material_homogenizationID(ce) = ho @@ -147,13 +194,11 @@ subroutine parse() material_homogenizationEntry(ce) = counterHomogenization(ho) end do - constituents => material%get('constituents') - do co = 1, constituents%length - constituent => constituents%get(co) + do co = 1, size(temp_ph(ma,:)>0) - v = constituent%get_asFloat('v') + v = temp_v(ma,co) + ph = temp_ph(ma,co) - ph = phases%getIndex(constituent%get_asString('phase')) do ip = 1, discretization_nIPs ce = (el-1)*discretization_nIPs + ip material_phaseID(co,ce) = ph @@ -163,68 +208,10 @@ subroutine parse() end do end do - if (dNeq(sum(material_v(1:constituents%length,ce)),1.0_pReal,1.e-9_pReal)) & - call IO_error(153,ext_msg='constituent') - end do - allocate(material_O_0(materials%length)) - allocate(material_F_i_0(materials%length)) - - ! manual iteration for performance - select type(materials) - class is(tList) - item => materials%first - do ma = 1, materials%length - material => item%node - constituents => material%get('constituents') - allocate(material_O_0(ma)%data(constituents%length)) - allocate(material_F_i_0(ma)%data(1:3,1:3,constituents%length)) - do co = 1, constituents%length - constituent => constituents%get(co) - call material_O_0(ma)%data(co)%fromQuaternion(constituent%get_as1dFloat('O',requiredSize=4)) - material_F_i_0(ma)%data(1:3,1:3,co) = constituent%get_as2dFloat('F_i',defaultVal=math_I3,requiredShape=[3,3]) - end do - item => item%next - end do - end select - end subroutine parse - -!-------------------------------------------------------------------------------------------------- -!> @brief Check if material.yaml is consistent and contains sufficient # of materials -!-------------------------------------------------------------------------------------------------- -subroutine sanityCheck(materials,homogenizations) - - class(tNode), intent(in) :: materials, & - homogenizations - - class(tNode), pointer :: material, & - homogenization, & - constituents - class(tItem), pointer :: item - integer :: m - - - if (maxval(discretization_materialAt) > materials%length) & - call IO_error(155,ext_msg='More materials requested than found in material.yaml') - - ! manual iteration for performance - select type(materials) - class is(tList) - item => materials%first - do m = 1, materials%length - material => materials%get(m) - constituents => material%get('constituents') - homogenization => homogenizations%get(material%get_asString('homogenization')) - if (constituents%length /= homogenization%get_asInt('N_constituents')) call IO_error(148) - item => item%next - end do - end select - -end subroutine sanityCheck - #if defined (__GFORTRAN__) !-------------------------------------------------------------------------------------------------- !> @brief %keys() is broken on gfortran From dbf5ee84cb997febff4a10949c1bfa352e751188 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 14 Apr 2022 20:14:08 +0200 Subject: [PATCH 6/9] better readable --- src/material.f90 | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/material.f90 b/src/material.f90 index 40d178540..7264738e5 100644 --- a/src/material.f90 +++ b/src/material.f90 @@ -111,6 +111,10 @@ subroutine parse() phases => config_material%get('phase') homogenizations => config_material%get('homogenization') + + if (maxval(discretization_materialAt) > materials%length) & + call IO_error(155,ext_msg='More materials requested than found in material.yaml') + #if defined (__GFORTRAN__) material_name_phase = getKeys(phases) material_name_homogenization = getKeys(homogenizations) @@ -126,31 +130,20 @@ subroutine parse() end do homogenization_maxNconstituents = maxval(homogenization_Nconstituents) - allocate(counterPhase(phases%length),source=0) - allocate(counterHomogenization(homogenizations%length),source=0) - - allocate(material_homogenizationID(discretization_Ncells),source=0) - allocate(material_homogenizationEntry(discretization_Ncells),source=0) - - allocate(material_phaseID(homogenization_maxNconstituents,discretization_Ncells),source=0) - allocate(material_phaseEntry(homogenization_maxNconstituents,discretization_Ncells),source=0) - allocate(material_v(homogenization_maxNconstituents,discretization_Ncells),source=0.0_pReal) allocate(material_O_0(materials%length)) allocate(material_F_i_0(materials%length)) + allocate(temp_ho(materials%length)) + allocate(temp_ph(materials%length,homogenization_maxNconstituents),source=-1) + allocate(temp_v(materials%length,homogenization_maxNconstituents),source=0.0_pReal) + + ! parse YAML structure select type(materials) class is(tList) - if (maxval(discretization_materialAt) > materials%length) & - call IO_error(155,ext_msg='More materials requested than found in material.yaml') - - allocate(temp_ho(materials%length)) - allocate(temp_ph(materials%length,homogenization_maxNconstituents),source=-1) - allocate(temp_v(materials%length,homogenization_maxNconstituents),source=0.0_pReal) - item => materials%first do ma = 1, materials%length material => item%node @@ -175,12 +168,21 @@ subroutine parse() if (dNeq(sum(temp_v(ma,:)),1.0_pReal,1.e-9_pReal)) call IO_error(153,ext_msg='constituent') item => item%next - end do end select + allocate(counterPhase(phases%length),source=0) + allocate(counterHomogenization(homogenizations%length),source=0) + + allocate(material_homogenizationID(discretization_Ncells),source=0) + allocate(material_homogenizationEntry(discretization_Ncells),source=0) + + allocate(material_phaseID(homogenization_maxNconstituents,discretization_Ncells),source=0) + allocate(material_phaseEntry(homogenization_maxNconstituents,discretization_Ncells),source=0) + + ! build mappings do el = 1, discretization_Nelems @@ -212,6 +214,7 @@ subroutine parse() end subroutine parse + #if defined (__GFORTRAN__) !-------------------------------------------------------------------------------------------------- !> @brief %keys() is broken on gfortran From 3cc3229792c82ef496f5bf3c376452a61652584e Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 16 Apr 2022 16:19:33 +0200 Subject: [PATCH 7/9] string new line which is added by libfyaml --- src/YAML_parse.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/YAML_parse.f90 b/src/YAML_parse.f90 index 8013089e1..bfce06bc9 100644 --- a/src/YAML_parse.f90 +++ b/src/YAML_parse.f90 @@ -83,7 +83,7 @@ recursive function parse_flow(YAML_flow) result(node) s, & ! start position of dictionary or list d ! position of key: value separator (':') - flow_string = trim(adjustl(YAML_flow(:))) + flow_string = trim(adjustl(YAML_flow)) if (len_trim(flow_string) == 0) then node => emptyDict return @@ -201,7 +201,7 @@ function to_flow(mixed) result(flow) block character(len=strlen,kind=c_char), pointer :: s call c_f_pointer(str_ptr,s) - flow = s + flow = s(:len(s)-1) end block call free_C(str_ptr) From 954a336d92c278227525ee174e25d196f7a328b2 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sat, 16 Apr 2022 16:24:29 +0200 Subject: [PATCH 8/9] Fortran side does not support tags (i.e. 'type hints') --- src/C_routines.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/C_routines.c b/src/C_routines.c index 3a1db91a0..a25fde688 100644 --- a/src/C_routines.c +++ b/src/C_routines.c @@ -88,7 +88,7 @@ void inflate_c(const uLong *s_deflated, const uLong *s_inflated, const Byte defl #ifdef FYAML void to_flow_c(char **flow, int* length_flow, const char *mixed){ struct fy_document *fyd = NULL; - enum fy_emitter_cfg_flags emit_flags = FYECF_MODE_FLOW_ONELINE | FYECF_STRIP_LABELS | FYECF_STRIP_DOC; + enum fy_emitter_cfg_flags emit_flags = FYECF_MODE_FLOW_ONELINE | FYECF_STRIP_LABELS | FYECF_STRIP_TAGS |FYECF_STRIP_DOC; fyd = fy_document_build_from_string(NULL, mixed, -1); if (!fyd) { From 4a93dbec117cbb0092ddc8f33e1a5cbe00097d2b Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 19 Apr 2022 13:11:40 -0400 Subject: [PATCH 9/9] more descriptive variable names --- src/material.f90 | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/material.f90 b/src/material.f90 index 7264738e5..2258c40a1 100644 --- a/src/material.f90 +++ b/src/material.f90 @@ -95,9 +95,9 @@ subroutine parse() integer, dimension(:), allocatable :: & counterPhase, & counterHomogenization, & - temp_ho - integer, dimension(:,:), allocatable :: temp_ph - real(pReal), dimension(:,:), allocatable :: temp_v + ho_of + integer, dimension(:,:), allocatable :: ph_of + real(pReal), dimension(:,:), allocatable :: v_of real(pReal) :: v integer :: & @@ -135,9 +135,9 @@ subroutine parse() allocate(material_O_0(materials%length)) allocate(material_F_i_0(materials%length)) - allocate(temp_ho(materials%length)) - allocate(temp_ph(materials%length,homogenization_maxNconstituents),source=-1) - allocate(temp_v(materials%length,homogenization_maxNconstituents),source=0.0_pReal) + allocate(ho_of(materials%length)) + allocate(ph_of(materials%length,homogenization_maxNconstituents),source=-1) + allocate( v_of(materials%length,homogenization_maxNconstituents),source=0.0_pReal) ! parse YAML structure select type(materials) @@ -147,10 +147,10 @@ subroutine parse() item => materials%first do ma = 1, materials%length material => item%node - temp_ho(ma) = homogenizations%getIndex(material%get_asString('homogenization')) + ho_of(ma) = homogenizations%getIndex(material%get_asString('homogenization')) constituents => material%get('constituents') - homogenization => homogenizations%get(temp_ho(ma)) + homogenization => homogenizations%get(ho_of(ma)) if (constituents%length /= homogenization%get_asInt('N_constituents')) call IO_error(148) allocate(material_O_0(ma)%data(constituents%length)) @@ -158,14 +158,14 @@ subroutine parse() do co = 1, constituents%length constituent => constituents%get(co) - temp_v(ma,co) = constituent%get_asFloat('v') - temp_ph(ma,co) = phases%getIndex(constituent%get_asString('phase')) + v_of(ma,co) = constituent%get_asFloat('v') + ph_of(ma,co) = phases%getIndex(constituent%get_asString('phase')) call material_O_0(ma)%data(co)%fromQuaternion(constituent%get_as1dFloat('O',requiredSize=4)) material_F_i_0(ma)%data(1:3,1:3,co) = constituent%get_as2dFloat('F_i',defaultVal=math_I3,requiredShape=[3,3]) end do - if (dNeq(sum(temp_v(ma,:)),1.0_pReal,1.e-9_pReal)) call IO_error(153,ext_msg='constituent') + if (dNeq(sum(v_of(ma,:)),1.0_pReal,1.e-9_pReal)) call IO_error(153,ext_msg='constituent') item => item%next end do @@ -187,7 +187,7 @@ subroutine parse() do el = 1, discretization_Nelems ma = discretization_materialAt(el) - ho = temp_ho(ma) + ho = ho_of(ma) do ip = 1, discretization_nIPs ce = (el-1)*discretization_nIPs + ip @@ -196,10 +196,10 @@ subroutine parse() material_homogenizationEntry(ce) = counterHomogenization(ho) end do - do co = 1, size(temp_ph(ma,:)>0) + do co = 1, size(ph_of(ma,:)>0) - v = temp_v(ma,co) - ph = temp_ph(ma,co) + v = v_of(ma,co) + ph = ph_of(ma,co) do ip = 1, discretization_nIPs ce = (el-1)*discretization_nIPs + ip