File size: 12,119 Bytes
91ba325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046eb45
91ba325
046eb45
91ba325
 
046eb45
91ba325
 
046eb45
91ba325
046eb45
 
 
 
91ba325
046eb45
 
 
 
 
 
 
91ba325
046eb45
 
 
91ba325
 
046eb45
 
 
 
91ba325
046eb45
 
 
 
 
 
 
91ba325
046eb45
 
 
 
91ba325
046eb45
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""
Data validation module for HVAC Load Calculator.
This module provides validation functions for user inputs.
"""

import streamlit as st
import pandas as pd
import numpy as np
from typing import Dict, List, Any, Optional, Tuple, Callable
import json
import os


class DataValidation:
    """Class for data validation functionality."""
    
    @staticmethod
    def validate_building_info(building_info: Dict[str, Any]) -> Tuple[bool, List[str]]:
        """
        Validate building information inputs.
        
        Args:
            building_info: Dictionary with building information
            
        Returns:
            Tuple containing validation result (True if valid) and list of validation messages
        """
        is_valid = True
        messages = []
        
        # Check required fields
        required_fields = [
            ("project_name", "Project Name"),
            ("building_name", "Building Name"),
            ("location", "Location"),
            ("climate_zone", "Climate Zone"),
            ("building_type", "Building Type")
        ]
        
        for field, display_name in required_fields:
            if field not in building_info or not building_info[field]:
                is_valid = False
                messages.append(f"{display_name} is required.")
        
        # Check numeric fields
        numeric_fields = [
            ("floor_area", "Floor Area", 0, None),
            ("num_floors", "Number of Floors", 1, None),
            ("floor_height", "Floor Height", 2.0, 10.0),
            ("occupancy", "Occupancy", 0, None)
        ]
        
        for field, display_name, min_val, max_val in numeric_fields:
            if field in building_info:
                try:
                    value = float(building_info[field])
                    if min_val is not None and value < min_val:
                        is_valid = False
                        messages.append(f"{display_name} must be at least {min_val}.")
                    if max_val is not None and value > max_val:
                        is_valid = False
                        messages.append(f"{display_name} must be at most {max_val}.")
                except (ValueError, TypeError):
                    is_valid = False
                    messages.append(f"{display_name} must be a number.")
        
        # Check design conditions
        if "design_conditions" in building_info:
            design_conditions = building_info["design_conditions"]
            
            # Check summer conditions
            summer_fields = [
                ("summer_outdoor_db", "Summer Outdoor Dry-Bulb", -10.0, 50.0),
                ("summer_outdoor_wb", "Summer Outdoor Wet-Bulb", -10.0, 40.0),
                ("summer_indoor_db", "Summer Indoor Dry-Bulb", 18.0, 30.0),
                ("summer_indoor_rh", "Summer Indoor RH", 30.0, 70.0)
            ]
            
            for field, display_name, min_val, max_val in summer_fields:
                if field in design_conditions:
                    try:
                        value = float(design_conditions[field])
                        if min_val is not None and value < min_val:
                            is_valid = False
                            messages.append(f"{display_name} must be at least {min_val}.")
                        if max_val is not None and value > max_val:
                            is_valid = False
                            messages.append(f"{display_name} must be at most {max_val}.")
                    except (ValueError, TypeError):
                        is_valid = False
                        messages.append(f"{display_name} must be a number.")
            
            # Check winter conditions
            winter_fields = [
                ("winter_outdoor_db", "Winter Outdoor Dry-Bulb", -40.0, 20.0),
                ("winter_outdoor_rh", "Winter Outdoor RH", 0.0, 100.0),
                ("winter_indoor_db", "Winter Indoor Dry-Bulb", 18.0, 25.0),
                ("winter_indoor_rh", "Winter Indoor RH", 20.0, 60.0)
            ]
            
            for field, display_name, min_val, max_val in winter_fields:
                if field in design_conditions:
                    try:
                        value = float(design_conditions[field])
                        if min_val is not None and value < min_val:
                            is_valid = False
                            messages.append(f"{display_name} must be at least {min_val}.")
                        if max_val is not None and value > max_val:
                            is_valid = False
                            messages.append(f"{display_name} must be at most {max_val}.")
                    except (ValueError, TypeError):
                        is_valid = False
                        messages.append(f"{display_name} must be a number.")
            
            # Check that wet-bulb is less than dry-bulb
            if "summer_outdoor_db" in design_conditions and "summer_outdoor_wb" in design_conditions:
                try:
                    db = float(design_conditions["summer_outdoor_db"])
                    wb = float(design_conditions["summer_outdoor_wb"])
                    if wb > db:
                        is_valid = False
                        messages.append("Summer Outdoor Wet-Bulb temperature must be less than or equal to Dry-Bulb temperature.")
                except (ValueError, TypeError):
                    pass  # Already handled above
        
        return is_valid, messages
    
    @staticmethod
    def validate_components(components: Dict[str, List[Any]]) -> Tuple[bool, List[str]]:
        """
        Validate building components.
        
        Args:
            components: Dictionary with building components
            
        Returns:
            Tuple containing validation result (True if valid) and list of validation messages
        """
        is_valid = True
        messages = []
        
        # Check if any components exist
        if not any(components.values()):
            is_valid = False
            messages.append("At least one building component (wall, roof, floor, window, or door) is required.")
        
        # Check wall components
        for i, wall in enumerate(components.get("walls", [])):
            # Check required fields
            if not wall.name:
                is_valid = False
                messages.append(f"Wall #{i+1}: Name is required.")
            
            # Check numeric fields
            if wall.area <= 0:
                is_valid = False
                messages.append(f"Wall #{i+1}: Area must be greater than zero.")
            
            if wall.u_value <= 0:
                is_valid = False
                messages.append(f"Wall #{i+1}: U-value must be greater than zero.")
        
        # Check roof components
        for i, roof in enumerate(components.get("roofs", [])):
            # Check required fields
            if not roof.name:
                is_valid = False
                messages.append(f"Roof #{i+1}: Name is required.")
            
            # Check numeric fields
            if roof.area <= 0:
                is_valid = False
                messages.append(f"Roof #{i+1}: Area must be greater than zero.")
            
            if roof.u_value <= 0:
                is_valid = False
                messages.append(f"Roof #{i+1}: U-value must be greater than zero.")
        
        # Check floor components
        for i, floor in enumerate(components.get("floors", [])):
            # Check required fields
            if not floor.name:
                is_valid = False
                messages.append(f"Floor #{i+1}: Name is required.")
            
            # Check numeric fields
            if floor.area <= 0:
                is_valid = False
                messages.append(f"Floor #{i+1}: Area must be greater than zero.")
            
            if floor.u_value <= 0:
                is_valid = False
                messages.append(f"Floor #{i+1}: U-value must be greater than zero.")
        
        # Check window components
        for i, window in enumerate(components.get("windows", [])):
            # Check required fields
            if not window.name:
                is_valid = False
                messages.append(f"Window #{i+1}: Name is required.")
            
            # Check numeric fields
            if window.area <= 0:
                is_valid = False
                messages.append(f"Window #{i+1}: Area must be greater than zero.")
            
            if window.u_value <= 0:
                is_valid = False
                messages.append(f"Window #{i+1}: U-value must be greater than zero.")
            
            if window.shgc <= 0 or window.shgc > 1:
                is_valid = False
                messages.append(f"Window #{i+1}: SHGC must be between 0 and 1.")
        
        # Check door components
        for i, door in enumerate(components.get("doors", [])):
            # Check required fields
            if not door.name:
                is_valid = False
                messages.append(f"Door #{i+1}: Name is required.")
            
            # Check numeric fields
            if door.area <= 0:
                is_valid = False
                messages.append(f"Door #{i+1}: Area must be greater than zero.")
            
            if door.u_value <= 0:
                is_valid = False
                messages.append(f"Door #{i+1}: U-value must be greater than zero.")
        
        # Check for minimum requirements
        if not components.get("walls", []):
            messages.append("Warning: No walls defined. At least one wall is recommended.")
        
        if not components.get("roofs", []):
            messages.append("Warning: No roofs defined. At least one roof is recommended.")
        
        if not components.get("floors", []):
            messages.append("Warning: No floors defined. At least one floor is recommended.")
        
        return is_valid, messages
    
    def validate_calculation_inputs(self, session_state: Dict[str, Any]) -> bool:
        """
        Validate inputs required for HVAC load calculations.
        
        Args:
            session_state: Streamlit session state containing all inputs
            
        Returns:
            Boolean indicating whether inputs are valid for calculations
        """
        # Check if building information exists
        if not session_state.get("building_info"):
            st.error("Building information is required for calculations.")
            return False
        
        # Validate building information
        building_info_valid, building_info_messages = self.validate_building_info(session_state["building_info"])
        if not building_info_valid:
            st.error("Building information is incomplete or invalid:")
            for message in building_info_messages:
                st.error(f"- {message}")
            return False
        
        # Check if design conditions exist
        if "design_conditions" not in session_state["building_info"]:
            st.error("Design conditions are required for calculations.")
            return False
        
        # Check if components exist
        if not session_state.get("components"):
            st.error("Building components are required for calculations.")
            return False
        
        # Validate components
        components_valid, components_messages = self.validate_components(session_state["components"])
        if not components_valid:
            st.error("Building components are incomplete or invalid:")
            for message in components_messages:
                st.error(f"- {message}")
            return False
        
        # Check if at least one component exists
        if not any(session_state["components"].values()):
            st.error("At least one building component (wall, roof, floor, window, or door) is required.")
            return False
        
        # All validations passed
        return True