@@ -130,19 +130,47 @@ def build_simulated_inputs(model_dir):
130130
131131 # 3. List of component and damage states ids associated with the damage
132132 comp_ds_list = pd .read_csv (os .path .join (model_dir , 'comp_ds_list.csv' ))
133-
133+
134134 # 4. List of component and damage states in the performance model
135135 comp_population = pd .read_csv (os .path .join (model_dir , 'comp_population.csv' ))
136136 comp_header = list (comp_population .columns )
137- comp_list = np .array (comp_header [2 :len (comp_header )])
138- comp_list = np .char .replace (np .array (comp_list ),'_' ,'.' )
137+
138+ # component IDs from comp_population
139+ comp_list = np .array (comp_header [2 :])
140+ comp_list = np .char .replace (comp_list , '_' , '.' )
139141 comp_list = comp_list .tolist ()
140- # Remove suffixes from repated entries
142+
143+ # Remove suffixes from repeated entries
141144 for i in range (len (comp_list )):
142145 if len (comp_list [i ]) > 10 :
143- comp_list [i ]= comp_list [i ][0 :10 ]
146+ comp_list [i ] = comp_list [i ][0 :10 ]
147+
148+ # Default: Skip components that do not have attributes
149+ valid_fragility_ids = set (component_attributes ['fragility_id' ].astype (str ))
150+ missing_comp_ids = sorted (set ([cid for cid in comp_list if cid not in valid_fragility_ids ]))
151+
152+ if missing_comp_ids :
153+ print (
154+ "Warning: skipping components with missing component attributes: "
155+ + ", " .join (missing_comp_ids )
156+ )
157+
158+ # Keep only valid components
159+ valid_mask = [cid in valid_fragility_ids for cid in comp_list ]
160+ valid_cols = comp_header [:2 ] + [col for col , keep in zip (comp_header [2 :], valid_mask ) if keep ]
161+
162+ comp_population = comp_population .loc [:, valid_cols ].copy ()
163+ comp_list = [cid for cid in comp_list if cid in valid_fragility_ids ]
164+
165+ # We also need to filter simulated_damage
166+ comp_ds_keep_mask = comp_ds_list ['comp_id' ].isin (comp_list ).to_numpy ()
167+
168+ comp_ds_list = comp_ds_list [comp_ds_keep_mask ].copy ()
169+ comp_ds_list = comp_ds_list .reset_index (drop = True )
170+
144171 building_model ['comps' ] = {'comp_list' : comp_list } #FZ# Component list has been added to building model dictionary.
145172
173+
146174 # Go through each story and assign component populations
147175 drs = np .unique (np .array (comp_population ['dir' ]))
148176
@@ -216,11 +244,38 @@ def build_simulated_inputs(model_dir):
216244 functionality ['utilities' ]['gas' ].append (0 )
217245
218246
219- # 3. Simulated component damage per tenant unit for each realization of the monte carlo simulation
220247 # 3. Simulated component damage per tenant unit for each realization of the monte carlo simulation
221248 with open (os .path .join (model_dir , 'simulated_damage.json' ), 'r' ) as f :
222249 sim_damage = json .load (f )
250+
223251
252+ # Filter simulated damage arrays to match filtered comp_ds_list
253+ if not np .all (comp_ds_keep_mask ):
254+ tenant_keys = [
255+ 'repair_cost' , 'num_comps' , 'qnt_damaged' ,
256+ 'qnt_damaged_side_1' , 'qnt_damaged_side_2' ,
257+ 'qnt_damaged_side_3' , 'qnt_damaged_side_4' ,
258+ 'worker_days'
259+ ]
260+ story_keys = ['qnt_damaged_dir_1' , 'qnt_damaged_dir_2' , 'qnt_damaged_dir_3' ]
261+
262+ if 'tenant_units' in sim_damage :
263+ for tu in range (len (sim_damage ['tenant_units' ])):
264+ for key in tenant_keys :
265+ if key in sim_damage ['tenant_units' ][tu ]:
266+ arr = np .array (sim_damage ['tenant_units' ][tu ][key ])
267+ if arr .ndim == 2 :
268+ sim_damage ['tenant_units' ][tu ][key ] = arr [:, comp_ds_keep_mask ].tolist ()
269+ elif arr .ndim == 1 :
270+ sim_damage ['tenant_units' ][tu ][key ] = arr [comp_ds_keep_mask ].tolist ()
271+
272+ if 'story' in sim_damage :
273+ for s in range (len (sim_damage ['story' ])):
274+ for key in story_keys :
275+ if key in sim_damage ['story' ][s ]:
276+ arr = np .array (sim_damage ['story' ][s ][key ])
277+ sim_damage ['story' ][s ][key ] = arr [:, comp_ds_keep_mask ].tolist ()
278+
224279 # Write in individual dictionaries part of larger 'damage' dictionary
225280 damage = {'story' : {}, 'tenant_units' : {}}
226281
0 commit comments