def damage_scenario_traffic_plot( c: Config, response_types: List[ResponseType], damage_scenario: DamageScenario, titles: List[str], saves: List[str], times: int = 3, ): """Save a contour plot of a scenarios scenario under normal traffic.""" # Grid of points where to record fem. grid_points = [ Point(x=x, y=0, z=z) for x, z in itertools.product( np.linspace(c.bridge.x_min, c.bridge.x_max, 4), np.linspace(c.bridge.z_min, c.bridge.z_max, 4), ) ] c, sim_params = damage_scenario.use( c=c, sim_params=SimParams(response_types=response_types)) # Generate a plot for each response type. for t, response_type, title, save in zip(range(times), response_types, titles, saves): time_index = -1 + abs(t) response_array = responses_to_traffic_array( c=c, traffic_array=load_normal_traffic_array(c, mins=1)[0], response_type=response_type, bridge_scenario=damage_scenario, points=grid_points, sim_runner=OSRunner, ) print(f"grid.shape = {np.array(grid).shape}") mean_response_array = np.mean(response_array, axis=0).T print(f"response_array.shape = {np.array(response_array).shape}") print( f"mean_response_array.shape = {np.array(mean_response_array).shape}" ) top_view_bridge(c.bridge, abutments=True, piers=True) responses = Responses.from_responses( response_type=response_type, responses=[(response_array[time_index][p], point) for p, point in enumerate(grid_points)], ) plot_contour_deck(c=c, responses=responses, center_norm=True, levels=100) plt.title(title) plt.save(save) plt.close()
def uls_contour_plot(c: Config, x_i: int, z_i: int, response_type: ResponseType): wheel_xs = c.bridge.wheel_track_xs(c) wheel_x = wheel_xs[x_i] wheel_zs = c.bridge.wheel_track_zs(c) wheel_z = wheel_zs[z_i] print_i(f"Wheel (x, z) = ({wheel_x}, {wheel_z})") plt.landscape() plt.subplot(2, 1, 1) healthy = list( ILMatrix.load_wheel_track( c=c, response_type=response_type, fem_runner=OSRunner(c), load_z_frac=c.bridge.z_frac(wheel_z), run_only=False, indices=[x_i], ))[0].resize() top_view_bridge(bridge=c.bridge, compass=False, abutments=True, piers=True) plot_contour_deck(c=c, responses=healthy, sci_format=True, decimals=6) plt.title("Healthy") c = transverse_crack().use(c)[0] cracked = list( ILMatrix.load_wheel_track( c=c, response_type=response_type, fem_runner=OSRunner(c), load_z_frac=c.bridge.z_frac(wheel_z), run_only=False, indices=[x_i], ))[0].resize() plt.subplot(2, 1, 2) top_view_bridge(bridge=c.bridge, compass=False, abutments=True, piers=True) plot_contour_deck(c=c, responses=cracked, sci_format=True, decimals=6) plt.title("Cracked") plt.tight_layout() plt.savefig( c.get_image_path( "verification", safe_str( f"uls-contour-x-{wheel_x}-z-{wheel_z}-{response_type.name()}") + ".pdf", ))
def build_with_refinement(refinement_radii): sim_params = SimParams( response_types=[response_type], ploads=[pload], refinement_radii=refinement_radii, ) # Build and save the model file. if build: build_model_3d( c=min_config, expt_params=ExptParams([sim_params]), os_runner=OSRunner(min_config), ) # Load and plot fem. if plot: sim_responses = load_fem_responses( c=min_config, sim_runner=OSRunner(min_config), response_type=response_type, sim_params=sim_params, run=True, ) for scatter in [True, False]: top_view_bridge(min_config.bridge, abutments=True, piers=True, lanes=True) plot_contour_deck( c=min_config, responses=sim_responses, scatter=scatter, levels=100, ) plt.title(f"{refinement_radii}") plt.savefig( min_config.get_image_path( "debugging", safe_str( f"{response_type.name()}-{refinement_radii}-scatter-{scatter}" ) + ".pdf", )) plt.close()
def top_view_plot(c: Config, max_time: int, skip: int, damage_scenario): response_type = ResponseType.YTranslation # Create the traffic. traffic_scenario = normal_traffic(c=c, lam=5, min_d=2) traffic_sequence, traffic, traffic_array = load_traffic( c=c, traffic_scenario=traffic_scenario, max_time=max_time, ) assert len(traffic) == traffic_array.shape[0] # Points on the deck to collect fem. deck_points = [ Point(x=x, y=0, z=z) for x in np.linspace( c.bridge.x_min, c.bridge.x_max, num=int(c.bridge.length * 2)) for z in np.linspace( c.bridge.z_min, c.bridge.z_max, num=int(c.bridge.width * 2)) # for x in np.linspace(c.bridge.x_min, c.bridge.x_max, num=30) # for z in np.linspace(c.bridge.z_min, c.bridge.z_max, num=10) ] point = Point(x=21, y=0, z=-8.4) # Point to plot deck_points.append(point) # Traffic array to fem array. responses_array = responses_to_traffic_array( c=c, traffic_array=traffic_array, damage_scenario=damage_scenario, points=deck_points, response_type=response_type, ) # Temperature effect July 1st. temps_2019 = temperature.load("holly-springs") temps_2019["temp"] = temperature.resize(temps_2019["temp"]) effect_2019 = temperature.effect( c=c, response_type=response_type, points=deck_points, temps=temps_2019["temp"], solar=temps_2019["solar"], len_per_hour=60, ).T # The effect is ordered by time series and then by points. (104910, 301) assert len(effect_2019) == len(temps_2019) july_2019_i, july_2019_j = temperature.from_to_indices( temps_2019, datetime.fromisoformat(f"2019-10-01T00:00"), datetime.fromisoformat(f"2019-10-01T23:59"), ) temp_effect = [] for i in range(len(deck_points)): temp_effect.append( temperature.apply( # Effect for July 1st, for the current point.. effect=effect_2019.T[i][july_2019_i:july_2019_j], # ..for the length of the time series. responses=responses_array, )) temp_effect = np.array(temp_effect) plt.subplot(2, 1, 1) plt.plot(effect_2019.T[-1]) plt.subplot(2, 1, 2) plt.plot(temp_effect[-1]) plt.show() # Determine response due to pier settlement. pd_response_at_point = 0 if isinstance(damage_scenario, PierDispDamage): pd_expt = list( DCMatrix.load(c=c, response_type=response_type, fem_runner=OSRunner(c))) for pier_displacement in damage_scenario.pier_disps: pd_sim_responses = pd_expt[pier_displacement.pier] pd_response_at_point += pd_sim_responses.at_deck( point, interp=False) * (pier_displacement.displacement / c.pd_unit_disp) # Resize fem if applicable to response type. resize_f, units = resize_units(response_type.units()) if resize_f is not None: responses_array = resize_f(responses_array) temp_effect = resize_f(temp_effect.T).T print(np.mean(temp_effect[-1])) pd_response_at_point = resize_f(pd_response_at_point) responses_w_temp = responses_array + temp_effect.T # Determine levels of the colourbar. amin, amax = np.amin(responses_array), np.amax(responses_array) # amin, amax = min(amin, -amax), max(-amin, amax) levels = np.linspace(amin, amax, 25) # All vehicles, for colour reference. all_vehicles = flatten(traffic, Vehicle) # Iterate through each time index and plot results. warmed_up_at = traffic_sequence[0][0].time_left_bridge(c.bridge) # Plot for each time step. for t_ind in range(len(responses_array))[::skip]: plt.landscape() # Plot the bridge top view. plt.subplot2grid((3, 1), (0, 0), rowspan=2) top_view_bridge(c.bridge, compass=False, lane_fill=False, piers=True) top_view_vehicles( bridge=c.bridge, mv_vehicles=flatten(traffic[t_ind], Vehicle), time=warmed_up_at + t_ind * c.sensor_hz, all_vehicles=all_vehicles, ) responses = Responses( response_type=response_type, responses=[(responses_array[t_ind][p_ind], deck_points[p_ind]) for p_ind in range(len(deck_points))], units=units, ) plot_contour_deck(c=c, responses=responses, levels=levels, mm_legend=False) plt.scatter( [point.x], [point.z], label=f"Sensor in bottom plot", marker="o", color="red", zorder=10, ) plt.legend(loc="upper right") plt.title( f"{response_type.name()} after {np.around(t_ind * c.sensor_hz, 4)} seconds" ) # Plot the fem at a point. plt.subplot2grid((3, 1), (2, 0)) time = t_ind * c.sensor_hz plt.axvline(x=time, color="black", label=f"Current time = {np.around(time, 4)} s") plt.plot( np.arange(len(responses_array)) * c.sensor_hz, responses_w_temp.T[-1], color="red", label="Total effect", ) if isinstance(damage_scenario, PierDispDamage): plt.plot( np.arange(len(responses_array)) * c.sensor_hz, np.ones(temp_effect[-1].shape) * pd_response_at_point, color="green", label="Pier settlement effect", ) plt.plot( np.arange(len(responses_array)) * c.sensor_hz, temp_effect[-1], color="blue", label="Temperature effect", ) plt.ylabel(f"{response_type.name()} ({responses.units})") plt.xlabel("Time (s)") plt.title(f"{response_type.name()} at sensor in top plot") plt.legend(loc="upper right", framealpha=1) # Finally save the image. name = f"{damage_scenario.name}-{response_type.name()}-{t_ind}" plt.tight_layout() plt.savefig(c.get_image_path("classify/top-view", f"{name}.pdf")) plt.savefig(c.get_image_path("classify/top-view/png", f"{name}.png")) plt.close()
def wagen_1_contour_plot( c: Config, x: int, crack_x: float, response_type: ResponseType, scatter: bool, run: bool, length: float, outline: bool, wheels: bool, temp: bool, ): original_c = c LOADS = False temp_bottom, temp_top = [17, 25] time = wagen1.time_at(x=x, bridge=c.bridge) def plot_wheels(): if wheels: wagen1.plot_wheels(c=c, time=time, label="Truck 1 wheels", zorder=100) center = c.bridge.x_max / 2 min_x, max_x = center - 20, center + 20 min_z, max_z = c.bridge.z_min, c.bridge.z_max def zoom_in(): plt.ylim(min_z, max_z) plt.xlim(min_x, max_x) loads = wagen1.to_wheel_track_loads(c=c, time=time, flat=True) crack_f = lambda: transverse_crack(length=length, at_x=crack_x) c = healthy_damage_w_transverse_crack_nodes(crack_f).use(original_c)[0] deck_shells = get_bridge_shells(c.bridge)[0] healthy_responses = load_fem_responses( c=c, sim_params=SimParams(ploads=loads), response_type=response_type, sim_runner=OSRunner(c), run=run, ).at_shells(deck_shells) # Convert fem to one per shell. if response_type in [ResponseType.Strain, ResponseType.StrainZZB]: # Resize by E-6 from microstrain to strain to match temperature units. healthy_responses = healthy_responses.resize() before_temp = healthy_responses.at_deck(Point(x=51, z=-8.4), interp=False) if temp: healthy_deck_points = healthy_responses.deck_points() # Point of fem. temp_effect = temperature.effect( c=c, response_type=response_type, points=healthy_deck_points, temps_bt=([temp_bottom], [temp_top]), ).T[0] # Temperature effect at existing response points. healthy_responses = healthy_responses.add(temp_effect, healthy_deck_points) after_temp = healthy_responses.at_deck(Point(x=51, z=-8.4), interp=False) print_i(f"Healthy, before/after = {before_temp}, {after_temp}") if response_type in [ResponseType.Strain, ResponseType.StrainZZB]: healthy_responses = healthy_responses.map(lambda x: x * 1e6) else: healthy_responses = healthy_responses.resize() # Responses in cracked scenario. c = crack_f().use(original_c)[0] crack_responses = load_fem_responses( c=c, sim_params=SimParams(ploads=loads), response_type=response_type, sim_runner=OSRunner(c), run=run, ).at_shells(deck_shells) if response_type in [ResponseType.Strain, ResponseType.StrainZZB]: # Resize by E-6 from microstrain to strain to match temperature units. crack_responses = crack_responses.resize() before_temp = crack_responses.at_deck(Point(x=51, z=-8.4), interp=False) if temp: crack_deck_points = crack_responses.deck_points() # Point of fem. temp_effect = temperature.effect( c=c, response_type=response_type, points=healthy_deck_points, temps_bt=([temp_bottom], [temp_top]), ).T[0] # Temperature effect at existing response points. crack_responses = crack_responses.add(temp_effect, healthy_deck_points) after_temp = crack_responses.at_deck(Point(x=51, z=-8.4), interp=False) print_i(f"Crack, before/after = {before_temp}, {after_temp}") if response_type in [ResponseType.Strain, ResponseType.StrainZZB]: crack_responses = crack_responses.map(lambda x: x * 1e6) else: crack_responses = crack_responses.resize() # Limit to points in crack zone. without_cm = 35 print(f"Avoid {without_cm} cm around crack zone") _without_crack_zone = crack_f().without(c.bridge, without_cm / 100) without_crack_zone = lambda p: not _without_crack_zone(p) if response_type in [ResponseType.Strain, ResponseType.StrainZZB]: healthy_responses = healthy_responses.without(without_crack_zone) crack_responses = crack_responses.without(without_crack_zone) # Norm calculation. vmin = min(healthy_responses.values()) vmax = max(healthy_responses.values()) vmin = min(vmin, min(crack_responses.values())) vmax = max(vmax, max(crack_responses.values())) norm = mpl.colors.Normalize(vmin=vmin, vmax=vmax) print(f"Norm min/max = {vmin}, {vmax}") plt.portrait() plt.subplot(3, 1, 1) plot_contour_deck( c=c, responses=healthy_responses, ploads=loads if LOADS else [], scatter=scatter, norm=norm, decimals=2, ) c_x_start, c_z_start, c_x_end, c_z_end = list( map(round_m, crack_f().crack_area(c.bridge))) def plot_outline(label="Crack zone"): if outline: plt.gca().add_patch( mpl.patches.Rectangle( (c_x_start, c_z_start), c_x_end - c_x_start, c_z_end - c_z_start, fill=not scatter, edgecolor="black", facecolor="white", alpha=1, label=label, )) top_view_bridge(bridge=c.bridge, compass=False, abutments=True, piers=True) plot_outline(label="Responses not considered") plot_wheels() zoom_in() def legend(): plt.legend( loc="upper right", borderpad=0.2, labelspacing=0.2, borderaxespad=0, handletextpad=0.2, columnspacing=0.2, ) legend() plt.title(f"Healthy bridge") plt.xlabel("") plt.tick_params(bottom=False, labelbottom=False) plt.subplot(3, 1, 2) plot_contour_deck( c=c, responses=crack_responses, ploads=loads if LOADS else [], scatter=scatter, norm=norm, decimals=2, ) top_view_bridge(bridge=c.bridge, compass=False, abutments=True, piers=True) plot_outline() plot_wheels() zoom_in() legend() plt.title(f"Cracked bridge") plt.xlabel("") plt.tick_params(bottom=False, labelbottom=False) plt.subplot(3, 1, 3) responses = [] for x in healthy_responses.deck_xs: for z in healthy_responses.zs[x][0]: responses.append(( bridge_sim.sim.responses.responses[0][x][0][z] - crack_responses.at_deck(Point(x=x, z=z), interp=False), Point(x=x, z=z), )) # try: # fem.append(( # healthy_responses.fem[0][x][0][z] # - crack_responses.fem[0][x][0][z], # Point(x=x, z=z) # )) # except KeyError: # pass # diff_responses = responses = Responses( response_type=response_type, responses=responses, units=healthy_responses.units, ) plot_contour_deck( c=c, responses=diff_responses, ploads=loads if LOADS else [], cmap=mpl.cm.get_cmap("PiYG"), scatter=scatter, decimals=2, ) print("********") print("********") print("********") grid_x, grid_z = 600, 200 grid_points = list( filter( lambda p: not without_crack_zone(p), [ Point(x=x, y=0, z=z) for x in np.linspace(c.bridge.x_min, c.bridge.x_max, grid_x) for z in np.linspace(c.bridge.z_min, c.bridge.z_max, grid_z) ], )) print(f"Amount grid points = {len(grid_points)}") grid_x_len = c.bridge.length / grid_x grid_z_len = c.bridge.width / grid_z grid_area = grid_x_len * grid_z_len print(f"Grid area = {grid_area}") print("Interpolating diff fem") interp_diff_responses = diff_responses.at_decks(grid_points) count_interp = len(interp_diff_responses) interp_diff_responses = interp_diff_responses[~np. isnan(interp_diff_responses)] print( f"Removed {count_interp - len(interp_diff_responses)} of {count_interp} fem, remaining = {len(interp_diff_responses)}" ) print("Finished interpolating diff fem") count_min, count_max = 0, 0 d_min, d_max = min(diff_responses.values()), max(diff_responses.values()) print(f"diff min, max = {d_min}, {d_max}") d_min08, d_max08 = d_min * 0.8, d_max * 0.8 for interp_r in interp_diff_responses: if interp_r < d_min08: count_min += 1 if interp_r > d_max08: count_max += 1 print(f"Count = {count_min}, {count_max}") save_path = original_c.get_image_path( "verification", safe_str( f"truck1-contour-x-{x}{crack_x}{length}-{response_type.name()}-{temp}" ), ) with open(save_path + ".txt", "w") as f: f.write(f"{count_min}, {count_max}\n") f.write(f"{count_min * grid_area}, {count_max * grid_area}") print(f"Wrote results to {save_path}.txt") top_view_bridge(bridge=c.bridge, compass=False, abutments=True, piers=True) plot_outline() plot_wheels() zoom_in() legend() temp_str = f"\nT_bot = {temp_bottom} °C, T_top = {temp_top} °C" if temp else "" plt.title(f"Difference of healthy & cracked bridge") rt_name = (f"Microstrain {response_type.ss_direction()}" if response_type in [ResponseType.Strain, ResponseType.StrainZZB ] else response_type.name()) plt.suptitle(f"{rt_name}: Truck 1 on healthy & cracked bridge{temp_str}") plt.tight_layout(rect=[0, 0.03, 1, 0.93 if temp else 0.95]) plt.savefig(save_path + ".pdf")
def stress_strength_plot(c: Config, top: bool): """Plot the difference of tensile strength and stress under load.""" original_c = c plt.portrait() response_type = ResponseType.StrainT if top else ResponseType.Strain settlement = 3 temp_bottom, temp_top = 21, 30 deck_points = [ Point(x=x, y=0, z=z) for x in np.linspace( # c.bridge.x_min, c.bridge.x_max, num=10 c.bridge.x_min, c.bridge.x_max, num=int(c.bridge.length * 3), ) for z in np.linspace( # c.bridge.z_min, c.bridge.z_max, num=10 c.bridge.z_min, c.bridge.z_max, num=int(c.bridge.width * 3), ) ] # Pier settlement. plt.subplot(3, 1, 1) c, sim_params = pier_disp_damage([(9, settlement / 1000)]).use(original_c) responses = (load_fem_responses( c=c, sim_runner=OSRunner(c), response_type=response_type, sim_params=sim_params, ).resize().to_stress(c.bridge)) top_view_bridge(bridge=c.bridge, compass=False, abutments=True, piers=True) plot_contour_deck(c=c, responses=responses, decimals=2) plt.legend(loc="upper right", borderaxespad=0) plt.title(f"{settlement} mm pier settlement") print("Calculated stress from pier settlement") # Temperature effect. plt.subplot(3, 1, 2) c = original_c print(f"deck_points.shape = {np.array(deck_points).shape}") temp_effect = temperature.effect( c=c, response_type=response_type, points=deck_points, temps_bt=([temp_bottom], [temp_top]), ).T[0] print(f"temp_effect.shape = {np.array(temp_effect).shape}") responses = (Responses( response_type=response_type, responses=[(temp_effect[p_ind], deck_points[p_ind]) for p_ind in range(len(deck_points)) if not np.isnan(temp_effect[p_ind])], ).without(remove=without.edges(c=c, radius=2)).to_stress(c.bridge)) top_view_bridge(c.bridge, compass=False, abutments=True, piers=True) plot_contour_deck(c=c, responses=responses, decimals=2) plt.legend(loc="upper right", borderaxespad=0) plt.title(f"T_bot, T_top = {temp_bottom}°C, {temp_top}°C") # plt.title(f"{top_str} stress\nbottom, top = {temp_bottom}, {temp_top}") print("Calculated stress from temperature") # Cracked concrete. plt.subplot(3, 1, 3) time = wagen1.time_at(x=52, bridge=c.bridge) print(f"wagen1.total_kn() = {wagen1.kn}") wagen1.kn = 400 loads = wagen1.to_wheel_track_loads(c=c, time=time, flat=True) c, sim_params = transverse_crack().use(original_c) c, sim_params = HealthyDamage().use(original_c) sim_params.ploads = loads responses = (load_fem_responses( c=c, sim_runner=OSRunner(c), response_type=response_type, sim_params=sim_params, ).resize().to_stress(c.bridge)) top_view_bridge(bridge=c.bridge, compass=False, abutments=True, piers=True) plot_contour_deck(c=c, responses=responses, decimals=2) plt.legend(loc="upper right", borderaxespad=0) # plt.title(f"Top stress: cracked concrete\nunder a {int(wagen1.kn)} kN vehicles") plt.title(f"{int(wagen1.total_kn())} kN vehicle") plt.suptitle(f"Stress {response_type.ss_direction()} for 3 scenarios") equal_lims("x", 3, 1) plt.tight_layout(rect=[0, 0.03, 1, 0.95]) plt.savefig( original_c.get_image_path( "validation", f"stress-strength-{response_type.name()}.pdf")) plt.close()