### ===================================================================================================================
### Create figure of the response spectrum graph
### ===================================================================================================================
# Copyright ©VIIA 2024
### ===================================================================================================================
### 1. Import modules
### ===================================================================================================================
# General imports
from __future__ import annotations
from pathlib import Path
from typing import TYPE_CHECKING, Optional
import math
# References for functions and classes in the rhdhv_fem package
from rhdhv_fem.fem_config import Config
from rhdhv_fem.general import NPRResponseSpectrum
# References for functions and classes in the viiaPackage
if TYPE_CHECKING:
from viiapackage.viiaStatus import ViiaProject
from viiapackage.viiaSettings import ViiaSettings
# Import module matplotlib
import matplotlib
from matplotlib import pyplot as plt
# Switch to a non-interactive backend to properly import matplotlib.pyplot
matplotlib.use(Config.MPL_NONINTERACTIVE(notify=False))
### ===================================================================================================================
### 2. Function to create figure of the response spectrum graph
### ===================================================================================================================
[docs]def viia_create_response_spectrum_graph(
project: ViiaProject, response_spectrum: NPRResponseSpectrum,
title: str = 'Horizontal Response Spectrum', save_folder: Optional[Path] = None, q_factor: float = 1,
ductile: bool = False) -> Path:
"""
This function creates a picture with the requested response spectrum graph.
Input:
- project (obj): Project object containing collections and of fem objects and project variables.
- response_spectrum (obj): Object reference of the ResponseSpectrumNPR instance to be used in the plot.
- title (str): Title that will be shown on top of the plot. Default value 'Horizontal Response Spectrum'.
- save_folder (Path): Specify the folder location where the plot should be created. Default value is None,
creating the plot in the default images folder defined for the project.
- q_factor (float): Behaviour factor for ductility. Default value 1.
- ductile: Option to calculate the response spectrum acceleration considering ductility, default set to
False.
Output:
- The function collects the data and creates the requested response spectrum graph plot.
- Returns the file reference of the created png-image in the requested folder.
"""
# Collect the data from the spectrum
periods = response_spectrum.periods(n=40, last_period=4)
accelerations = response_spectrum.accelerations(periods=periods)
# Set the folder
if save_folder is None:
save_folder = project.workfolder_location / ViiaSettings.DEFAULT_IMAGES_FOLDER
# Initiate plot
plt.close()
# Apply VIIA graph style
style_file = Path(project.viia_settings.project_specific_package_location) / 'viiaGraph.mplstyle'
plt.style.use(style_file.as_posix())
# Plot the values
fig, ax = plt.subplots(figsize=(15, 5))
ax.plot(periods, accelerations, alpha=0.7, label='Horizontal NPR-data')
# Set custom axis limits
ax.set_ylim(0, math.ceil(max(accelerations) / 0.05) * 0.05)
# Set the axes labels
ax.set_xlabel('Period [s]')
ax.set_ylabel('Pseudo-spectral accel. (S_a) [g]')
fig.suptitle(title)
# Add a legend
ax.legend(markerscale=4)
# Set the location where to save the graphs
file = save_folder / f'Response_spectrum.png'
# Create the graphs
fig.savefig(file, format='png')
return file
### ===================================================================================================================
### 3. End of script
### ===================================================================================================================