Skip to content

Commit

Permalink
fix(tuil): Add a function to parse individual DOE-2 objects
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswmackey authored and Chris Mackey committed Jun 7, 2024
1 parent d4a608f commit 65f8415
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
55 changes: 55 additions & 0 deletions honeybee_doe2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,61 @@ def generate_inp_string_list_format(u_name, command, keywords, values):
return inp_str


def parse_inp_string(inp_string):
"""Parse an INP string of a single DOE-2 object into a list of values.
Note that this method is only equipped to parse DOE-2 test strings
that originate from eQuest or from this package. It has not yet
been generalized to parse all formats of text formats that can
appear in a DOE-2 file.
Args:
inp_string: An INP string for a single DOE-2 object.
Returns:
A tuple with four elements.
- u_name: Text for the unique name of the object.
- command: Text for the type of instruction that the DOE-2 object executes.
- keywords: A list of text with the same length as the values that denote
the attributes of the DOE-2 object.
- values: A list of values with the same length as the keywords that describe
the values of the attributes for the object.
"""
inp_string = inp_string.strip()
inp_strings = inp_string.split('..')
assert len(inp_strings) == 2, 'Received more than one object in inp_string.'
inp_string = re.sub(r'\$.*\n', '\n', inp_strings[0]) # remove all comments
doe2_fields = [e_str.strip() for e_str in inp_string.split('=')]
u_name = doe2_fields.pop(0).replace('"', '')
split_field_1 = doe2_fields[0].split('\n')
command = split_field_1[0].strip()
keywords = [split_field_1[1].strip()]
values = []
for field in doe2_fields[1:]:
split_field = [f.strip() for f in field.split('\n')]
if len(split_field) == 1:
values.append(split_field[0])
elif len(split_field) == 2 and not split_field[0].endswith(','):
values.append(split_field[0])
keywords.append(split_field[1])
else:
v_lines, end_val = [], False
for row in split_field:
if row.endswith(',') or row.endswith('('):
v_lines.append(row)
elif not end_val:
v_lines.append(row)
end_val = True
else:
keywords.append(row)
values.append(' '.join(v_lines))
return u_name, command, keywords, values


def header_comment_minor(header_text):
"""Create a header given header_text, which can help organize the INP file contents.
"""
Expand Down
138 changes: 138 additions & 0 deletions tests/util_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
"""Test the utility functions."""
from honeybee_doe2.util import parse_inp_string

SCHEDULE_DAY_STR = """
"College HTGSETP SCH Wkdy" = DAY-SCHEDULE
TYPE = TEMPERATURE
HOURS = (1, 6)
VALUES = (60.0)
HOURS = (7, 8)
VALUES = (64.0, 68.0)
HOURS = (9, 21)
VALUES = (70.0)
HOURS = (22, 24)
VALUES = (60.0)
..
"""

SCHEDULE_DAY_PD_STR = """
"PRJ Heating SAT" = DAY-SCHEDULE-PD
TYPE = TEMPERATURE
VALUES = ( 65, &D, &D, &D, &D, &D, 66.5, 68, 70, &D, &D, &D, &D,
&D, &D, &D, 65 )
..
"""

SCHEDULE_WEEK_PD_STR = """
"College HTGSETP SCH Week 1" = WEEK-SCHEDULE-PD
TYPE = TEMPERATURE
DAY-SCHEDULES = (
"College HTGSETP SCH Wkdy", $ Monday,
"College HTGSETP SCH Wkdy", $ Tuesday,
"College HTGSETP SCH Wkdy", $ Wednesday,
"College HTGSETP SCH Wkdy", $ Thursday,
"College HTGSETP SCH Wkdy", $ Friday,
"College HTGSETP SCH Wknd", $ Saturday,
"College HTGSETP SCH Wknd", $ Sunday,
"College HTGSETP SCH Hol", $ Holiday,
"College HTGSETP SCH WntrDsn", $ Winter Design Day,
"College HTGSETP SCH SmrDsn", $ Summer Design Day,
)
..
"""

SCHEDULE_WEEK_PD_STR2 = """
"PRJ Heating Wk" = WEEK-SCHEDULE-PD
TYPE = TEMPERATURE
DAY-SCHEDULES = ( "PRJ Heating WD", &D, &D, &D, &D, "PRJ Heating SAT",
"PRJ Heating SUN, HOL" )
..
"""

SCHEDULE_YEAR_STR = """
"College HTGSETP SCH" = SCHEDULE
TYPE = TEMPERATURE
THRU DEC 31 = "College HTGSETP SCH Week 1"
..
"""

SCHEDULE_YEAR_PD_STR = """
"PRJ Heating Sch" = SCHEDULE-PD
TYPE = TEMPERATURE
MONTH = ( 12 )
DAY = ( 31 )
WEEK-SCHEDULES = ( "PRJ Heating Wk" )
..
"""


def test_parse_schedule_day():
"""Test the parsing of DAY-SCHEDULE."""
u_name, command, keywords, values = parse_inp_string(SCHEDULE_DAY_STR)

assert u_name == 'College HTGSETP SCH Wkdy'
assert command == 'DAY-SCHEDULE'
assert len(keywords) == 9
assert len(values) == 9
assert keywords[0] == 'TYPE'
assert values[0] == 'TEMPERATURE'


def test_parse_schedule_day_pd():
"""Test the parsing of DAY-SCHEDULE-PD."""
u_name, command, keywords, values = parse_inp_string(SCHEDULE_DAY_PD_STR)

assert u_name == 'PRJ Heating SAT'
assert command == 'DAY-SCHEDULE-PD'
assert len(keywords) == 2
assert len(values) == 2
assert keywords[0] == 'TYPE'
assert values[0] == 'TEMPERATURE'


def test_parse_schedule_week_pd():
"""Test the parsing of WEEK-SCHEDULE-PD."""
u_name, command, keywords, values = parse_inp_string(SCHEDULE_WEEK_PD_STR)

assert u_name == 'College HTGSETP SCH Week 1'
assert command == 'WEEK-SCHEDULE-PD'
assert len(keywords) == 2
assert len(values) == 2
week_tuple = eval(values[-1])
assert len(week_tuple) == 10


def test_parse_schedule_week_pd2():
"""Test the parsing of WEEK-SCHEDULE-PD."""
u_name, command, keywords, values = parse_inp_string(SCHEDULE_WEEK_PD_STR2)

assert u_name == 'PRJ Heating Wk'
assert command == 'WEEK-SCHEDULE-PD'
assert len(keywords) == 2
assert len(values) == 2
week_tuple = eval(values[-1].replace('&D', '"&D"'))
assert len(week_tuple) == 7


def test_parse_schedule_year():
"""Test the parsing of SCHEDULE."""
u_name, command, keywords, values = parse_inp_string(SCHEDULE_YEAR_STR)

assert u_name == 'College HTGSETP SCH'
assert command == 'SCHEDULE'
assert len(keywords) == 2
assert len(values) == 2
assert keywords[0] == 'TYPE'
assert values[0] == 'TEMPERATURE'


def test_parse_schedule_year_pd():
"""Test the parsing of SCHEDULE-PD."""
u_name, command, keywords, values = parse_inp_string(SCHEDULE_YEAR_PD_STR)

assert u_name == 'PRJ Heating Sch'
assert command == 'SCHEDULE-PD'
assert len(keywords) == 4
assert len(values) == 4
assert keywords[0] == 'TYPE'
assert values[0] == 'TEMPERATURE'

0 comments on commit 65f8415

Please sign in to comment.