### ===================================================================================================================
###  Example Vibration Contour Plot Too1
### ===================================================================================================================

# Imports
from haskoning_atr_tools import *

### ===================================================================================================================
###  1. Inputs and create project
### ===================================================================================================================

# User specified inputs
inputs_excel = Path.cwd() / 'template_inputs.xlsx'

# Create the project
project = ATRProject(name='My Project', location='Building location', project_code='XXXX')

### ===================================================================================================================
###  2. Define the equipments
### ===================================================================================================================

# Create equipment from file
project.create_equipments_from_file(file_path=inputs_excel, sheet_name='equipments', activity_filter='continuous')

### ===================================================================================================================
###  3. Define the targets
### ===================================================================================================================

# Create targets from file
project.create_targets_from_file(file_path=inputs_excel, sheet_name='targets')

# Add the vibration limits from file
project.load_target_limits_from_file(file_path=inputs_excel, sheet_name='velocity limits', limit_type='velocity')

### ===================================================================================================================
###  4. Perform simulation
### ===================================================================================================================

# Create the mesh for the vibration simulation, set size and precision
project.generate_constrained_mesh(x_range=(-20, 120), y_range=(-20, 120), mesh_area=10)

# Set the material properties from file
materials = project.create_materials_from_file(file_path=inputs_excel, sheet_name='materials')

# Create the simulation tool
vibration_simulation = project.create_vibration_simulation(
    name='vibration_simulation_1', material=materials['sandstone'])

# Create the frequency spectra for the equipments and provide the samping rate precision
project.create_frequency_domain_data_from_equipments(sampling_rate=1)

# Execute the vibration analysis
vibration_simulation.calculate_level_per_node_per_source(amplitude_type='decibel')

### ===================================================================================================================
###  4. Result assessment
### ===================================================================================================================

project.plot_f_spectrum_at_targets(amplitude_type='decibel', plot_components=True, show=True)
project.plot_f_spectrum_against_limits_at_targets(amplitude_type='decibel', log_scale_y=True, show=True)
project.plot_f_spectrum_all_targets(amplitude_type='decibel', compliance_limits=True, show=True)

# Create contour plots at specific frequencies and the governing contour plot
n_levels = 10
figure_size = (15, 9)
contour_plot = project.plot_contour(
    figure_size=figure_size, vibration_simulation=vibration_simulation, amplitude_type='decibel', align_colour_bar=True,
    n_levels=n_levels, at_frequencies=[10, 20, 40, 60], min_limit=0, max_limit=100, show_mesh=True)
project.plot_governing_contour(
    figure_size=figure_size, vibration_simulation=vibration_simulation, amplitude_type='decibel', n_levels=n_levels,
    min_limit=0, max_limit=100)

### ===================================================================================================================
###  5. End of script
### ===================================================================================================================
