From 898306f82e2b666bb3a9f265d5dbeda4f8828336 Mon Sep 17 00:00:00 2001 From: Sharan Roongta Date: Wed, 30 Sep 2020 23:33:09 +0200 Subject: [PATCH 1/6] sanity check for key mismatch --- src/IO.f90 | 3 +++ src/YAML_types.f90 | 3 +++ 2 files changed, 6 insertions(+) diff --git a/src/IO.f90 b/src/IO.f90 index 0542e7a62..ee35cf1bc 100644 --- a/src/IO.f90 +++ b/src/IO.f90 @@ -498,6 +498,9 @@ subroutine IO_error(error_ID,el,ip,g,instance,ext_msg) msg = 'Abrupt end of file' case (708) msg = '--- expected after YAML file header' + case (710) + msg = 'key mismatch' + !------------------------------------------------------------------------------------------------- ! errors related to the grid solver diff --git a/src/YAML_types.f90 b/src/YAML_types.f90 index cc1c85efd..86acf05be 100644 --- a/src/YAML_types.f90 +++ b/src/YAML_types.f90 @@ -952,6 +952,9 @@ function tNode_get_byKey_asIndex(self,key) result(keyIndex) endif enddo + if(keyIndex == -1) call IO_error(710,ext_msg=key) + + end function tNode_get_byKey_asIndex From 85b96209e3a43aea264947a456f4517e5c3b00a0 Mon Sep 17 00:00:00 2001 From: Sharan Roongta Date: Thu, 1 Oct 2020 01:51:31 +0200 Subject: [PATCH 2/6] use existing error description --- src/IO.f90 | 3 --- src/YAML_types.f90 | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/src/IO.f90 b/src/IO.f90 index ee35cf1bc..0542e7a62 100644 --- a/src/IO.f90 +++ b/src/IO.f90 @@ -498,9 +498,6 @@ subroutine IO_error(error_ID,el,ip,g,instance,ext_msg) msg = 'Abrupt end of file' case (708) msg = '--- expected after YAML file header' - case (710) - msg = 'key mismatch' - !------------------------------------------------------------------------------------------------- ! errors related to the grid solver diff --git a/src/YAML_types.f90 b/src/YAML_types.f90 index 86acf05be..09a2d0592 100644 --- a/src/YAML_types.f90 +++ b/src/YAML_types.f90 @@ -952,7 +952,7 @@ function tNode_get_byKey_asIndex(self,key) result(keyIndex) endif enddo - if(keyIndex == -1) call IO_error(710,ext_msg=key) + if(keyIndex == -1) call IO_error(140,ext_msg=key) end function tNode_get_byKey_asIndex From 46973508a2c9763bcdef8c509da3abde1b5ae929 Mon Sep 17 00:00:00 2001 From: Sharan Roongta Date: Mon, 5 Oct 2020 18:53:05 +0200 Subject: [PATCH 3/6] allow multi line flow yaml --- src/YAML_parse.f90 | 125 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 113 insertions(+), 12 deletions(-) diff --git a/src/YAML_parse.f90 b/src/YAML_parse.f90 index cb5b726dc..2beab6fe8 100644 --- a/src/YAML_parse.f90 +++ b/src/YAML_parse.f90 @@ -222,11 +222,24 @@ logical function isKey(line) else isKey = IO_rmComment(line(len(IO_rmComment(line)):len(IO_rmComment(line)))) == ':' & .and. .not. isFlow(line) + isKey = isKey .and. index(IO_rmComment(line),':') == index(IO_rmComment(line),':',back =.true.) endif end function isKey +!-------------------------------------------------------------------------------------------------- +! @brief check whether a string is a list in flow style +!-------------------------------------------------------------------------------------------------- +logical function isFlowList(line) + + character(len=*), intent(in) :: line + + isFlowList = index(adjustl(line),'[') == 1 + +end function isFlowList + + !-------------------------------------------------------------------------------------------------- ! @brief skip empty lines ! @details update start position in the block by skipping empty lines if present. @@ -272,6 +285,57 @@ subroutine skip_file_header(blck,s_blck) end subroutine skip_file_header +!-------------------------------------------------------------------------------------------------- +!> @brief check if the flow line contains line break +!-------------------------------------------------------------------------------------------------- +logical function is_end(str,e_char) + + character(len=*), intent(in) :: str + character, intent(in) :: e_char !< end of list/dict ( '}' or ']') + integer :: N_sq, & !< number of open square brackets + N_cu, & !< number of open curly brackets + i + character(len=:), allocatable:: line + + is_end = .false. + N_sq = 0 + N_cu = 0 + if(e_char == ']') line = str(index(str(:),'[')+1:) + if(e_char == '}') line = str(index(str(:),'{')+1:) + + do i = 1, len_trim(line) + is_end = (N_sq==0 .and. N_cu==0 .and. scan(line(i:i),e_char) == 1) + N_sq = N_sq + merge(1,0,line(i:i) == '[') + N_cu = N_cu + merge(1,0,line(i:i) == '{') + N_sq = N_sq - merge(1,0,line(i:i) == ']') + N_cu = N_cu - merge(1,0,line(i:i) == '}') + enddo + + +end function is_end + +!-------------------------------------------------------------------------------------------------- +!> @brief return the flow YAML line without line break +!-------------------------------------------------------------------------------------------------- +subroutine line_break(blck,s_blck,e_char,flow_sgl) + + character(len=*), intent(in) :: blck !< YAML in mixed style + integer, intent(inout) :: s_blck + character, intent(in) :: e_char !< end of list/dict ( '}' or ']') + character(len=:), allocatable, intent(out) :: flow_sgl + logical :: line_end + + line_end =.false. + flow_sgl = '' + + do while(.not.line_end) + flow_sgl = flow_sgl//IO_rmComment(blck(s_blck:s_blck + index(blck(s_blck:),IO_EOL) - 2))//' ' + line_end = is_end(flow_sgl,e_char) + s_blck = s_blck + index(blck(s_blck:),IO_EOL) + enddo + +end subroutine line_break + !-------------------------------------------------------------------------------------------------- ! @brief reads a line of YAML block which is already in flow style @@ -402,7 +466,7 @@ recursive subroutine lst(blck,flow,s_blck,s_flow,offset) integer, intent(inout) :: s_blck, & !< start position in blck s_flow, & !< start position in flow offset !< stores leading '- ' in nested lists - character(len=:), allocatable :: line + character(len=:), allocatable :: line,flow_sgl integer :: e_blck,indent indent = indentDepth(blck(s_blck:),offset) @@ -437,8 +501,12 @@ recursive subroutine lst(blck,flow,s_blck,s_flow,offset) s_blck = e_blck +2 offset = 0 elseif(isFlow(line)) then - call line_isFlow(flow,s_flow,line) - s_blck = e_blck +2 + if(isFlowList(line)) then + call line_break(blck,s_blck,']',flow_sgl) + else + call line_break(blck,s_blck,'}',flow_sgl) + endif + call line_isFlow(flow,s_flow,flow_sgl) offset = 0 endif else ! list item in the same line @@ -448,8 +516,13 @@ recursive subroutine lst(blck,flow,s_blck,s_flow,offset) s_blck = e_blck +2 offset = 0 elseif(isFlow(line)) then - call line_isFlow(flow,s_flow,line) - s_blck = e_blck +2 + s_blck = s_blck + index(blck(s_blck:),'-') + if(isFlowList(line)) then + call line_break(blck,s_blck,']',flow_sgl) + else + call line_break(blck,s_blck,'}',flow_sgl) + endif + call line_isFlow(flow,s_flow,flow_sgl) offset = 0 else ! non scalar list item offset = offset + indentDepth(blck(s_blck:))+1 ! offset in spaces to be ignored @@ -486,8 +559,8 @@ recursive subroutine dct(blck,flow,s_blck,s_flow,offset) s_flow, & !< start position in flow offset - character(len=:), allocatable :: line - integer :: e_blck,indent + character(len=:), allocatable :: line,flow_sgl + integer :: e_blck,indent,col_pos logical :: previous_isKey previous_isKey = .false. @@ -521,12 +594,22 @@ recursive subroutine dct(blck,flow,s_blck,s_flow,offset) endif if(isKeyValue(line)) then - call keyValue_toFlow(flow,s_flow,line) + col_pos = index(line,':') + if(isFlow(line(col_pos+1:))) then + if(isFlowList(line(col_pos+1:))) then + call line_break(blck,s_blck,']',flow_sgl) + else + call line_break(blck,s_blck,'}',flow_sgl) + endif + call keyValue_toFlow(flow,s_flow,flow_sgl) + else + call keyValue_toFlow(flow,s_flow,line) + s_blck = e_blck + 2 + endif else call line_toFlow(flow,s_flow,line) + s_blck = e_blck + 2 endif - - s_blck = e_blck +2 end if if(isScalar(line) .or. isKeyValue(line)) then @@ -559,7 +642,7 @@ recursive subroutine decide(blck,flow,s_blck,s_flow,offset) s_flow, & !< start position in flow offset integer :: e_blck - character(len=:), allocatable :: line + character(len=:), allocatable :: line,flow_sgl if(s_blck <= len(blck)) then call skip_empty_lines(blck,s_blck) @@ -583,8 +666,12 @@ recursive subroutine decide(blck,flow,s_blck,s_flow,offset) flow(s_flow:s_flow) = '}' s_flow = s_flow + 1 elseif(isFlow(line)) then + if(isFlowList(line)) then + call line_break(blck,s_blck,']',flow_sgl) + else + call line_break(blck,s_blck,'}',flow_sgl) + endif call line_isFlow(flow,s_flow,line) - s_blck = e_blck +2 else line = line(indentDepth(line)+1:) call line_toFlow(flow,s_flow,line) @@ -723,6 +810,20 @@ subroutine selfTest if (.not. to_flow(flow_mixed_braces) == flow) error stop 'to_flow' end block basic_flow + multi_line_flow: block + character(len=*), parameter :: flow_multi = & + "%YAML 1.1"//IO_EOL//& + "---"//IO_EOL//& + "a: [b,"//IO_EOL//& + "c: "//IO_EOL//& + "d, e]"//IO_EOL + character(len=*), parameter :: flow = & + "{a: [b, {c: d}, e]}" + + if( .not. to_flow(flow_multi) == flow) error stop 'to_flow' + end block multi_line_flow + + basic_mixed: block character(len=*), parameter :: block_flow = & "%YAML 1.1"//IO_EOL//& From 6dff0396b6df5b8a376e6a0d11d29d05e2f258fb Mon Sep 17 00:00:00 2001 From: Sharan Roongta Date: Mon, 5 Oct 2020 19:13:40 +0200 Subject: [PATCH 4/6] [skip sc] more testing --- .../SpectralMethod/Polycrystal/material.yaml | 14 ++++++++++--- src/YAML_parse.f90 | 20 +++++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/examples/SpectralMethod/Polycrystal/material.yaml b/examples/SpectralMethod/Polycrystal/material.yaml index 16c6042a6..9da5a1358 100644 --- a/examples/SpectralMethod/Polycrystal/material.yaml +++ b/examples/SpectralMethod/Polycrystal/material.yaml @@ -1,6 +1,8 @@ +--- homogenization: SX: - mech: {type: none} + mech: {type: +none} microstructure: - constituents: - fraction: 1.0 @@ -9,7 +11,8 @@ microstructure: homogenization: SX - constituents: - fraction: 1.0 - orientation: [0.7936696712125002, -0.28765777461664166, -0.3436487135089419, 0.4113964260949434] + orientation: [0.7936696712125002, -0.28765777461664166, + -0.3436487135089419, 0.4113964260949434] phase: Aluminum homogenization: SX - constituents: @@ -19,7 +22,12 @@ microstructure: homogenization: SX - constituents: - fraction: 1.0 - orientation: [0.28645844315788244, -0.022571491243423537, -0.467933059311115, -0.8357456192708106] + orientation: [0.28645844315788244, + + + +-0.022571491243423537, + -0.467933059311115, -0.8357456192708106] phase: Aluminum homogenization: SX - constituents: diff --git a/src/YAML_parse.f90 b/src/YAML_parse.f90 index 2beab6fe8..d8d320ad1 100644 --- a/src/YAML_parse.f90 +++ b/src/YAML_parse.f90 @@ -810,19 +810,35 @@ subroutine selfTest if (.not. to_flow(flow_mixed_braces) == flow) error stop 'to_flow' end block basic_flow - multi_line_flow: block + multi_line_flow1: block character(len=*), parameter :: flow_multi = & "%YAML 1.1"//IO_EOL//& "---"//IO_EOL//& "a: [b,"//IO_EOL//& "c: "//IO_EOL//& "d, e]"//IO_EOL + character(len=*), parameter :: flow = & "{a: [b, {c: d}, e]}" if( .not. to_flow(flow_multi) == flow) error stop 'to_flow' - end block multi_line_flow + end block multi_line_flow1 + multi_line_flow2: block + character(len=*), parameter :: flow_multi = & + "%YAML 1.1"//IO_EOL//& + "---"//IO_EOL//& + "-"//IO_EOL//& + " a: {b:"//IO_EOL//& + "[c,"//IO_EOL//& + "d"//IO_EOL//& + "e, f]}"//IO_EOL + + character(len=*), parameter :: flow = & + "[{a: {b: [c, d e, f]}}]" + + if( .not. to_flow(flow_multi) == flow) error stop 'to_flow' + end block multi_line_flow2 basic_mixed: block character(len=*), parameter :: block_flow = & From 08ab4a0b888abdab71f79adfd442ea6d9f75af16 Mon Sep 17 00:00:00 2001 From: Sharan Roongta Date: Tue, 6 Oct 2020 18:09:53 +0200 Subject: [PATCH 5/6] better names --- src/YAML_parse.f90 | 54 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/YAML_parse.f90 b/src/YAML_parse.f90 index d8d320ad1..8197782b2 100644 --- a/src/YAML_parse.f90 +++ b/src/YAML_parse.f90 @@ -257,7 +257,6 @@ subroutine skip_empty_lines(blck,s_blck) if(empty) s_blck = s_blck + index(blck(s_blck:),IO_EOL) enddo - end subroutine skip_empty_lines @@ -285,10 +284,11 @@ subroutine skip_file_header(blck,s_blck) end subroutine skip_file_header + !-------------------------------------------------------------------------------------------------- -!> @brief check if the flow line contains line break +!> @brief check if a line in flow YAML starts and ends in the same line !-------------------------------------------------------------------------------------------------- -logical function is_end(str,e_char) +logical function flow_is_closed(str,e_char) character(len=*), intent(in) :: str character, intent(in) :: e_char !< end of list/dict ( '}' or ']') @@ -297,44 +297,44 @@ logical function is_end(str,e_char) i character(len=:), allocatable:: line - is_end = .false. + flow_is_closed = .false. N_sq = 0 N_cu = 0 if(e_char == ']') line = str(index(str(:),'[')+1:) if(e_char == '}') line = str(index(str(:),'{')+1:) do i = 1, len_trim(line) - is_end = (N_sq==0 .and. N_cu==0 .and. scan(line(i:i),e_char) == 1) + flow_is_closed = (N_sq==0 .and. N_cu==0 .and. scan(line(i:i),e_char) == 1) N_sq = N_sq + merge(1,0,line(i:i) == '[') N_cu = N_cu + merge(1,0,line(i:i) == '{') N_sq = N_sq - merge(1,0,line(i:i) == ']') N_cu = N_cu - merge(1,0,line(i:i) == '}') enddo +end function flow_is_closed -end function is_end !-------------------------------------------------------------------------------------------------- !> @brief return the flow YAML line without line break !-------------------------------------------------------------------------------------------------- -subroutine line_break(blck,s_blck,e_char,flow_sgl) +subroutine remove_line_break(blck,s_blck,e_char,flow_line) character(len=*), intent(in) :: blck !< YAML in mixed style integer, intent(inout) :: s_blck character, intent(in) :: e_char !< end of list/dict ( '}' or ']') - character(len=:), allocatable, intent(out) :: flow_sgl + character(len=:), allocatable, intent(out) :: flow_line logical :: line_end line_end =.false. - flow_sgl = '' + flow_line = '' do while(.not.line_end) - flow_sgl = flow_sgl//IO_rmComment(blck(s_blck:s_blck + index(blck(s_blck:),IO_EOL) - 2))//' ' - line_end = is_end(flow_sgl,e_char) - s_blck = s_blck + index(blck(s_blck:),IO_EOL) + flow_line = flow_line//IO_rmComment(blck(s_blck:s_blck + index(blck(s_blck:),IO_EOL) - 2))//' ' + line_end = flow_is_closed(flow_line,e_char) + s_blck = s_blck + index(blck(s_blck:),IO_EOL) enddo -end subroutine line_break +end subroutine remove_line_break !-------------------------------------------------------------------------------------------------- @@ -466,7 +466,7 @@ recursive subroutine lst(blck,flow,s_blck,s_flow,offset) integer, intent(inout) :: s_blck, & !< start position in blck s_flow, & !< start position in flow offset !< stores leading '- ' in nested lists - character(len=:), allocatable :: line,flow_sgl + character(len=:), allocatable :: line,flow_line integer :: e_blck,indent indent = indentDepth(blck(s_blck:),offset) @@ -502,11 +502,11 @@ recursive subroutine lst(blck,flow,s_blck,s_flow,offset) offset = 0 elseif(isFlow(line)) then if(isFlowList(line)) then - call line_break(blck,s_blck,']',flow_sgl) + call remove_line_break(blck,s_blck,']',flow_line) else - call line_break(blck,s_blck,'}',flow_sgl) + call remove_line_break(blck,s_blck,'}',flow_line) endif - call line_isFlow(flow,s_flow,flow_sgl) + call line_isFlow(flow,s_flow,flow_line) offset = 0 endif else ! list item in the same line @@ -518,11 +518,11 @@ recursive subroutine lst(blck,flow,s_blck,s_flow,offset) elseif(isFlow(line)) then s_blck = s_blck + index(blck(s_blck:),'-') if(isFlowList(line)) then - call line_break(blck,s_blck,']',flow_sgl) + call remove_line_break(blck,s_blck,']',flow_line) else - call line_break(blck,s_blck,'}',flow_sgl) + call remove_line_break(blck,s_blck,'}',flow_line) endif - call line_isFlow(flow,s_flow,flow_sgl) + call line_isFlow(flow,s_flow,flow_line) offset = 0 else ! non scalar list item offset = offset + indentDepth(blck(s_blck:))+1 ! offset in spaces to be ignored @@ -559,7 +559,7 @@ recursive subroutine dct(blck,flow,s_blck,s_flow,offset) s_flow, & !< start position in flow offset - character(len=:), allocatable :: line,flow_sgl + character(len=:), allocatable :: line,flow_line integer :: e_blck,indent,col_pos logical :: previous_isKey @@ -597,11 +597,11 @@ recursive subroutine dct(blck,flow,s_blck,s_flow,offset) col_pos = index(line,':') if(isFlow(line(col_pos+1:))) then if(isFlowList(line(col_pos+1:))) then - call line_break(blck,s_blck,']',flow_sgl) + call remove_line_break(blck,s_blck,']',flow_line) else - call line_break(blck,s_blck,'}',flow_sgl) + call remove_line_break(blck,s_blck,'}',flow_line) endif - call keyValue_toFlow(flow,s_flow,flow_sgl) + call keyValue_toFlow(flow,s_flow,flow_line) else call keyValue_toFlow(flow,s_flow,line) s_blck = e_blck + 2 @@ -642,7 +642,7 @@ recursive subroutine decide(blck,flow,s_blck,s_flow,offset) s_flow, & !< start position in flow offset integer :: e_blck - character(len=:), allocatable :: line,flow_sgl + character(len=:), allocatable :: line,flow_line if(s_blck <= len(blck)) then call skip_empty_lines(blck,s_blck) @@ -667,9 +667,9 @@ recursive subroutine decide(blck,flow,s_blck,s_flow,offset) s_flow = s_flow + 1 elseif(isFlow(line)) then if(isFlowList(line)) then - call line_break(blck,s_blck,']',flow_sgl) + call remove_line_break(blck,s_blck,']',flow_line) else - call line_break(blck,s_blck,'}',flow_sgl) + call remove_line_break(blck,s_blck,'}',flow_line) endif call line_isFlow(flow,s_flow,line) else From 2a23b5eaa9cc070497086e4a94e77b97477f204a Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 7 Oct 2020 09:37:48 +0200 Subject: [PATCH 6/6] simplified --- src/YAML_parse.f90 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/YAML_parse.f90 b/src/YAML_parse.f90 index 8197782b2..8e20e0c29 100644 --- a/src/YAML_parse.f90 +++ b/src/YAML_parse.f90 @@ -220,9 +220,9 @@ logical function isKey(line) if(len(IO_rmComment(line)) == 0) then isKey = .false. else - isKey = IO_rmComment(line(len(IO_rmComment(line)):len(IO_rmComment(line)))) == ':' & - .and. .not. isFlow(line) - isKey = isKey .and. index(IO_rmComment(line),':') == index(IO_rmComment(line),':',back =.true.) + isKey = index(IO_rmComment(line),':',back=.false.) == len(IO_rmComment(line)) .and. & + index(IO_rmComment(line),':',back=.true.) == len(IO_rmComment(line)) .and. & + .not. isFlow(line) endif end function isKey