def test_from_dict_reversed_surface_bcs(): """Test the from_dict of Story objects with reversed Surface boundary conditions.""" pts_1 = (Point3D(0, 0, 2), Point3D(10, 0, 2), Point3D(10, 10, 2), Point3D(0, 10, 2)) pts_2 = (Point3D(10, 0, 3), Point3D(20, 0, 3), Point3D(20, 10, 3), Point3D(10, 10, 3)) room2d_1 = Room2D('Office1', Face3D(pts_1), 5) room2d_2 = Room2D('Office2', Face3D(pts_2), 3) story = Story('OfficeFloor', [room2d_1, room2d_2]) story.solve_room_2d_adjacency(0.01) story_dict_original = story.to_dict() # reverse the order of vertices in one of the rooms story_dict = story.to_dict() room2 = story_dict['room_2ds'][1] room2['floor_boundary'] = list(reversed(room2['floor_boundary'])) room2['boundary_conditions'] = list(reversed(room2['boundary_conditions'])) room1_bc = story_dict['room_2ds'][0]['boundary_conditions'][1] room1_bc['boundary_condition_objects'] = ('Office2..Face1', 'Office2') new_story = Story.from_dict(story_dict) assert new_story.to_dict() == story_dict_original # reverse the order of vertices in both of the rooms room1 = story_dict['room_2ds'][0] room1['floor_boundary'] = list(reversed(room1['floor_boundary'])) room1['boundary_conditions'] = list(reversed(room1['boundary_conditions'])) room2_bc = story_dict['room_2ds'][1]['boundary_conditions'][0] room2_bc['boundary_condition_objects'] = ('Office1..Face3', 'Office1') new_story = Story.from_dict(story_dict) assert new_story.to_dict() == story_dict_original
def test_from_dict(): """Test the Story from_dict method with energy properties.""" pts_1 = (Point3D(0, 0, 3), Point3D(0, 10, 3), Point3D(10, 10, 3), Point3D(10, 0, 3)) pts_2 = (Point3D(10, 0, 3), Point3D(10, 10, 3), Point3D(20, 10, 3), Point3D(20, 0, 3)) pts_3 = (Point3D(0, 10, 3), Point3D(0, 20, 3), Point3D(10, 20, 3), Point3D(10, 10, 3)) pts_4 = (Point3D(10, 10, 3), Point3D(10, 20, 3), Point3D(20, 20, 3), Point3D(20, 10, 3)) room2d_1 = Room2D('Office1', Face3D(pts_1), 3) room2d_2 = Room2D('Office2', Face3D(pts_2), 3) room2d_3 = Room2D('Office3', Face3D(pts_3), 3) room2d_4 = Room2D('Office4', Face3D(pts_4), 3) story = Story('OfficeFloor', [room2d_1, room2d_2, room2d_3, room2d_4]) story.solve_room_2d_adjacency(0.01) story.set_outdoor_window_parameters(SimpleWindowRatio(0.4)) mass_set = ConstructionSet('Thermal Mass Construction Set') story.properties.energy.construction_set = mass_set sd = story.to_dict() new_story = Story.from_dict(sd) assert new_story.properties.energy.construction_set.identifier == \ 'Thermal Mass Construction Set' assert new_story.to_dict() == sd
def test_to_from_dict(): """Test the to/from dict of Story objects.""" pts_1 = (Point3D(0, 0, 2), Point3D(10, 0, 2), Point3D(10, 10, 2), Point3D(0, 10, 2)) pts_2 = (Point3D(10, 0, 3), Point3D(20, 0, 3), Point3D(20, 10, 3), Point3D(10, 10, 3)) room2d_1 = Room2D('Office1', Face3D(pts_1), 5) room2d_2 = Room2D('Office2', Face3D(pts_2), 3) story = Story('OfficeFloor', [room2d_1, room2d_2]) story.solve_room_2d_adjacency(0.01) story.set_outdoor_window_parameters(SimpleWindowRatio(0.4)) story.set_outdoor_shading_parameters(Overhang(1)) story_dict = story.to_dict() new_story = Story.from_dict(story_dict) assert isinstance(new_story, Story) assert new_story.to_dict() == story_dict
def dict_to_object(dragonfly_dict, raise_exception=True): """Re-serialize a dictionary of almost any object within dragonfly. This includes any Model, Building, Story, Room2D, WindowParameter, ShadingParameter, and boundary conditions. Args: dragonfly_dict: A dictionary of any Dragonfly object. Note that this should be a non-abridged dictionary to be valid. raise_exception: Boolean to note whether an excpetion should be raised if the object is not identified as a part of dragonfly. Default: True. Returns: A Python object derived from the input dragonfly_dict. """ try: # get the type key from the dictionary obj_type = dragonfly_dict['type'] except KeyError: raise ValueError('Dragonfly dictionary lacks required "type" key.') if obj_type == 'Model': return Model.from_dict(dragonfly_dict) elif obj_type == 'Building': return Building.from_dict(dragonfly_dict) elif obj_type == 'Story': return Story.from_dict(dragonfly_dict) elif obj_type == 'Room2D': return Room2D.from_dict(dragonfly_dict) elif obj_type == 'ContextShade': return ContextShade.from_dict(dragonfly_dict) elif hasattr(dfw, obj_type): win_class = getattr(dfw, obj_type) return win_class.from_dict(dragonfly_dict) elif hasattr(dfs, obj_type): shd_class = getattr(dfs, obj_type) return shd_class.from_dict(dragonfly_dict) elif hasattr(hbc, obj_type): bc_class = getattr(hbc, obj_type) return bc_class.from_dict(dragonfly_dict) elif raise_exception: raise ValueError( '{} is not a recognized dragonfly object'.format(obj_type))