### Install openTorsion with pip or clone the repository Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/installation.rst.txt Install openTorsion via pip, or clone the repository with git if git is installed. Cloning creates a folder named opentorsion containing the source code. ```bash pip install opentorsion ``` ```bash git clone https://github.com/Aalto-Arotor/openTorsion.git ``` -------------------------------- ### Install openTorsion with pip Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/installation.html Installs the openTorsion package using the Python package installer pip. Requires Python to be installed on the system. ```bash pip install opentorsion ``` -------------------------------- ### Create shaft, disk, and gear elements and assemble them Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/modeling.rst.txt This example creates a complete OpenTorsion model: four shaft elements defined by stiffness, one hollow shaft defined by dimensions and material properties, six disk elements, and a gear pair. The elements are grouped into lists and passed to the Assembly constructor. Finally, the assembly is visualized using the Plots class. ```python import opentorsion as ot # Creating 4 shaft elements using stiffness values # Syntax: ot.Shaft(node 1, node 2, Length [mm], outer diameter [mm], stiffness [Nm/rad]) shaft1 = ot.Shaft(0, 1, L=None, odl=None, k=25e+6) shaft2 = ot.Shaft(1, 2, L=None, odl=None, k=25e+6) shaft3 = ot.Shaft(2, 3, L=None, odl=None, k=25e+6) shaft4 = ot.Shaft(3, 4, L=None, odl=None, k=25e+6) # Creating a hollow shaft using dimension and material parameter values # Syntax: ot.Shaft(node 1, node 2, Length [mm], outer diameter [mm], inner diameter [mm], shear modulus [Pa], density [kg/m^3]) shaft5 = ot.Shaft(5, 6, L=400, odl=80, idl=40, G=79e9, rho=7850) # Creating 5 disk elements # Syntax: ot.Disk(node, inertia [kgm^2]) disk1 = ot.Disk(0, I=100) disk2 = ot.Disk(1, I=10) disk3 = ot.Disk(2, I=50) disk4 = ot.Disk(3, I=10) disk5 = ot.Disk(4, I=80) disk6 = ot.Disk(6, I=80) # Creating a gear element # Gears consist of a parent gear and one or more child gear # Syntax: ot.Gear(node, inertia [kgm^2], gear radius [mm], parent) gear1 = ot.Gear(4, I=2, R=50, parent=None) # parent gear gear2 = ot.Gear(5, I=5, R=150, parent=gear1) # child gear # Adding the elements to lists corresponding to an element type shafts = [shaft1, shaft2, shaft3, shaft4, shaft5] disks = [disk1, disk2, disk3, disk4, disk5, disk6] gears = [gear1, gear2] # Syntax: ot.Assembly(shaft_elements, disk_elements, gear_elements) assembly = ot.Assembly(shaft_elements=shafts, disk_elements=disks, gear_elements=gears) # Assembly can be visualized using openTorsion plotting tools. plot_tools = ot.Plots(assembly) # initialize plot tools plot_tools.plot_assembly() ``` -------------------------------- ### Install OpenTorsion and import dependencies Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/transient_simulation.ipynb Installs the opentorsion package and imports numpy, opentorsion, and matplotlib.pyplot for use in the notebook. ```python %pip install opentorsion import numpy as np import opentorsion as ot import matplotlib.pyplot as plt ``` ```text Collecting opentorsion Downloading opentorsion-0.2.5-py3-none-any.whl (20 kB) Requirement already satisfied: matplotlib in /usr/local/lib/python3.10/dist-packages (from opentorsion) (3.7.1) Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from opentorsion) (1.25.2) Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from opentorsion) (1.11.4) Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (1.2.1) Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (0.12.1) Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (4.51.0) Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (1.4.5) Requirement already satisfied: packaging>=20.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (24.0) Requirement already satisfied: pillow>=6.2.0 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (9.4.0) Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (3.1.2) Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.10/dist-packages (from matplotlib->opentorsion) (2.8.2) Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.10/dist-packages (from python-dateutil>=2.7->matplotlib->opentorsion) (1.16.0) Installing collected packages: opentorsion Successfully installed opentorsion-0.2.5 ``` -------------------------------- ### Transient response simulation with OpenTorsion Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/transient_response.rst.txt This example creates an assembly of shafts and disks, defines an impulse excitation, and simulates the system's response using a discrete-time state-space model. The resulting torque at each shaft is plotted. ```python import numpy as np import matplotlib.pyplot as plt from scipy.signal import dlsim import opentorsion as ot # An example assembly # Creating 4 shaft elements using stiffness values # Syntax: ot.Shaft(node 1, node 2, Length [mm], outer diameter [mm], stiffness [Nm/rad]) shaft1 = ot.Shaft(0, 1, L=None, odl=None, k=25e+6) shaft2 = ot.Shaft(1, 2, L=None, odl=None, k=25e+6) shaft3 = ot.Shaft(2, 3, L=None, odl=None, k=25e+6) shaft4 = ot.Shaft(3, 4, L=None, odl=None, k=25e+6) # Creating 5 disk elements # Syntax: ot.Disk(node, inertia [kgm^2]) disk1 = ot.Disk(0, I=100) disk2 = ot.Disk(1, I=10) disk3 = ot.Disk(2, I=50) disk4 = ot.Disk(3, I=10) disk5 = ot.Disk(4, I=80) # Adding the elements to lists corresponding to an element type shafts = [shaft1, shaft2, shaft3, shaft4] disks = [disk1, disk2, disk3, disk4, disk5] # Syntax: ot.Assembly(shaft_elements, disk_elements) assembly = ot.Assembly(shaft_elements=shafts, disk_elements=disks) # Defining and impulse excitation dt = 0.002 t = np.arange(0, 0.500, dt) impulse = np.zeros((len(t), assembly.dofs)) ramp = np.arange(0, 2000, int(2000/8)) impulse[22:30,0] = ramp impulse[30,0] = 2000 impulse[31:39,0] = ramp[::-1] plt.plot(t, impulse[:,0], c='black') plt.title("Impulse excitation") plt.xlabel("Times (s)") plt.ylabel("Torque (Nm)") plt.show() # Discrete-time LTI state-space model A, B = assembly.state_space() Ad, Bd = assembly.continuous_2_discrete(A, B, dt) C = np.eye(A.shape[1]) D = np.zeros((C.shape[0], B.shape[1])) sys = (Ad, Bd, C, D, dt) # scipy.signal.dlsim used for time-step simulation tout, yout, xout = dlsim(sys, impulse, t) # simulation result is nodal rotational angles and speeds angles, speeds = np.split(yout, 2, axis=1) # initiate 4 subplots for the 4 shafts fig, axes = plt.subplots(4, 1, figsize=(8, 8)) # Shaft stiffness values are used to calculate the torque from the angle differences shaft_stiffness = [25e+6, 25e+6, 25e+6, 25e+6] # Loop over the 4 shafts to plot the response for each of them for n in range(4): # Plot the shaft response in newton meters axes[n].plot(t, shaft_stiffness[n]*(angles[:,(n+1)]-angles[:,n]), c='red') axes[n].set_title(f'Torque at shaft {n+1}') axes[n].set_xlabel('Time (s)') axes[n].set_ylabel('Torque (Nm)') plt.tight_layout() plt.show() ``` -------------------------------- ### Import NumPy and opentorsion Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/doctrees/nbsphinx/notebooks/modal_analysis_and_forced_response.ipynb Imports the NumPy library and the opentorsion package, which are required for the modal analysis and forced response example. ```python import numpy as np import opentorsion as ot ``` -------------------------------- ### Clone openTorsion repository with git Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/installation.html Clones the openTorsion repository from GitHub using git. Requires git to be installed. Creates a folder named opentorsion containing the source code. ```bash git clone https://github.com/Aalto-Arotor/openTorsion.git ``` -------------------------------- ### Full forced response simulation Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/doctrees/nbsphinx/notebooks/modal_analysis_and_forced response.ipynb Main simulation: creates a 3-mass, 2-shaft assembly with given stiffness and inertia values. Performs modal analysis to get eigenfrequencies (printed twice each), plots eigenmodes, Campbell diagram, assembly. Then loops over rotational speeds 0.1 to 25 rad/s at 5000 steps, computing vibratory torque per shaft per excitation, summing contributions, and plotting the total shaft torque. Uses C = assembly.C_modal(M, K, xi=0.02) for damping, throws an exception if not all steps complete. ```python def forced_response(): """ First a model of a windmill is created as a system of three lumped masses connected by two shafts. The assembly is given harmonic excitation as input. Finally, the system response is calculated and plotted. """ # Parameters of the mechanical model k1 = 3.67e8 # Nm/rad k2 = 5.496e9 # Nm/rad J1 = 1e7 # kgm^2 J2 = 5770 # kgm^2 J3 = 97030 # kgm^2 # Creating assembly shafts, disks = [], [] disks.append(ot.Disk(0, J1)) shafts.append(ot.Shaft(0, 1, None, None, k=k1, I=0)) disks.append(ot.Disk(1, J2)) shafts.append(ot.Shaft(1, 2, None, None, k=k2, I=0)) disks.append(ot.Disk(2, J3)) assembly = ot.Assembly(shafts, disk_elements=disks) M, K = assembly.M, assembly.K # Mass and stiffness matrices C = assembly.C_modal(M, K, xi=0.02) # Damping matrix # Modal analysis A, B = assembly.state_matrix(C) omegas_undamped, omegas_damped, damping_ratios = assembly.modal_analysis() # Print eigenfrequencies. # The list contains each eigenfrequency twice: e.g. eigenfrequencies = [1st, 1st, 2nd, 2nd, 3rd, 3rd, ...] print("Eigenfrequencies: ", omegas_undamped.round(3)) # Initiate plotting tools calling Plots(assembly) plot_tools = ot.Plots(assembly) # Plot eigenmodes, input number of eigenmodes plot_tools.plot_assembly() plot_tools.plot_eigenmodes(modes=3) plot_tools.plot_campbell(frequency_range_rpm=[0, 300], num_modes=2) # Steady-state forced response analysis VT_element1 = [] VT_element2 = [] # The excitation depends on the rotational speed of the system. # Here the response is calculated at each rotational speed. # The responses at each rotational speed are summed to get the total response. for rpm in np.linspace(0.1, 25, 5000): omegas, amplitudes = get_windmill_excitation(rpm) excitations = np.zeros((M.shape[0], omegas.shape[0]), dtype="complex128") excitations[2] = amplitudes # Excitation acts on the generator side T_vib = assembly.vibratory_torque(excitations, omegas, k_shafts=np.array([k1, k2]), C=C) VT_element1.append(np.sum(np.abs(T_vib[0]))) VT_element2.append(np.sum(np.abs(T_vib[1]))) T_e = np.array( [np.array(VT_element1), np.array(VT_element2)] ) # Total response (shaft torque) plot_tools = ot.Plots(assembly) plot_tools.torque_response_plot(np.linspace(0.1, 25, 5000), T_e, show_plot=True) return ``` -------------------------------- ### Modal analysis and forced response of a windmill with OpenTorsion Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/modal_analysis_and_forced_response.html Defines and runs a forced_response() function that builds a three-mass windmill model with OpenTorsion, performs modal analysis, and computes the steady-state forced response over a range of rotational speeds. The function prints the undamped eigenfrequencies (each appears twice) and produces plots of the assembly, eigenmodes, Campbell diagram, and torque response. The output shows the eigenfrequencies for the example: 0, 0, 58.34, 58.34, 1034.115, 1034.115 rad/s. ```python def forced_response(): """ First a model of a windmill is created as a system of three lumped masses connected by two shafts. The assembly is given harmonic excitation as input. Finally, the system response is calculated and plotted. """ # Parameters of the mechanical model k1 = 3.67e8 # Nm/rad k2 = 5.496e9 # Nm/rad J1 = 1e7 # kgm^2 J2 = 5770 # kgm^2 J3 = 97030 # kgm^2 # Creating assembly shafts, disks = [], [] disks.append(ot.Disk(0, J1)) shafts.append(ot.Shaft(0, 1, None, None, k=k1, I=0)) disks.append(ot.Disk(1, J2)) shafts.append(ot.Shaft(1, 2, None, None, k=k2, I=0)) disks.append(ot.Disk(2, J3)) assembly = ot.Assembly(shafts, disk_elements=disks) M, K = assembly.M, assembly.K # Mass and stiffness matrices C = assembly.C_modal(M, K, xi=0.02) # Damping matrix # Modal analysis A, B = assembly.state_matrix(C) omegas_undamped, omegas_damped, damping_ratios = assembly.modal_analysis() # Print eigenfrequencies. # The list contains each eigenfrequency twice: e.g. eigenfrequencies = [1st, 1st, 2nd, 2nd, 3rd, 3rd, ...] print("Eigenfrequencies: ", omegas_undamped.round(3)) # Initiate plotting tools calling Plots(assembly) plot_tools = ot.Plots(assembly) # Plot eigenmodes, input number of eigenmodes plot_tools.plot_assembly() plot_tools.plot_eigenmodes(modes=3) plot_tools.plot_campbell(frequency_range_rpm=[0, 300], num_modes=2) # Steady-state forced response analysis VT_element1 = [] VT_element2 = [] # The excitation depends on the rotational speed of the system. # Here the response is calculated at each rotational speed. # The responses at each rotational speed are summed to get the total response. for rpm in np.linspace(0.1, 25, 5000): omegas, amplitudes = get_windmill_excitation(rpm) excitations = np.zeros((M.shape[0], omegas.shape[0]), dtype="complex128") excitations[2] = amplitudes # Excitation acts on the generator side T_vib = assembly.vibratory_torque(excitations, omegas, k_shafts=np.array([k1, k2]), C=C) VT_element1.append(np.sum(np.abs(T_vib[0]))) VT_element2.append(np.sum(np.abs(T_vib[1]))) T_e = np.array( [np.array(VT_element1), np.array(VT_element2)] ) # Total response (shaft torque) plot_tools = ot.Plots(assembly) plot_tools.torque_response_plot(np.linspace(0.1, 25, 5000), T_e, show_plot=True) return ``` ```python forced_response() ``` ```text Eigenfrequencies: [ 0. 0. 58.34 58.34 1034.115 1034.115] ``` -------------------------------- ### Set up controller parameters and instantiate PI Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/transient_simulation.ipynb Defines controller gains, target velocity, torque limit, and instantiates the PI controller with a time step (ts). ```python ## This class creates a discrete PI controller for a system. ## Attributes ## ---------- ## K ## Proportional gain ## Ki ## Integral gain ## dt ## Time step ## setpoint ## Desired system output ## limit ## Maximum output limit for integrator and controller ``` ```python class PI(): def __init__(self, Kp, Ki, dt, setpoint, limit): self.Kp = Kp self.Ki = Ki self.dt = dt self.setpoint = setpoint self.limit = limit self.integral_error = 0 def next_step(self, x): error = self.setpoint - x self.integral_error += error*self.Ki*self.dt out = self.integral_error + error*self.Kp if self.integral_error > self.limit: self.integral_error = self.limit if out > self.limit: return self.limit return out ``` -------------------------------- ### Steady-state forced response calculation and plotting with OpenTorsion Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/forced_response.rst.txt Builds a 4-shaft, 5-disk assembly, defines a rotational-speed-dependent excitation at node 0, computes the steady-state response with assembly.ss_response, and plots the torque at each shaft by summing harmonic components. Requires numpy, matplotlib, and opentorsion. ```python import numpy as np import matplotlib.pyplot as plt import opentorsion as ot # An example assembly # Creating 4 shaft elements using stiffness values # Syntax: ot.Shaft(node 1, node 2, Length [mm], outer diameter [mm], stiffness [Nm/rad]) shaft1 = ot.Shaft(0, 1, L=None, odl=None, k=25e+6) shaft2 = ot.Shaft(1, 2, L=None, odl=None, k=25e+6) shaft3 = ot.Shaft(2, 3, L=None, odl=None, k=25e+6) shaft4 = ot.Shaft(3, 4, L=None, odl=None, k=25e+6) # Creating 5 disk elements # Syntax: ot.Disk(node, inertia [kgm^2]) disk1 = ot.Disk(0, I=100) disk2 = ot.Disk(1, I=10) disk3 = ot.Disk(2, I=50) disk4 = ot.Disk(3, I=10) disk5 = ot.Disk(4, I=80) # Adding the elements to lists corresponding to an element type shafts = [shaft1, shaft2, shaft3, shaft4] disks = [disk1, disk2, disk3, disk4, disk5] # Syntax: ot.Assembly(shaft_elements, disk_elements) assembly = ot.Assembly(shaft_elements=shafts, disk_elements=disks) harmonics = np.array([1, 2, 3, 4]) # excitation harmonics, multiples of rotational frequency amplitudes = [200, 50, 5, 2] # excitation amplitudes, corresponding to harmonics # defining an excitation matrix: a rotational speed dependent excitation is applied to node 0 # rows correspond to assembly nodes, columns correspond to excitation frequencies excitation = np.zeros([assembly.dofs, len(amplitudes)]) excitation[0] = amplitudes w = 3600*(2*np.pi)/60 # base rotational frequency t = np.linspace(0, (2*np.pi)/w, 200) # time, used for plotting omegas = w*harmonics # array of excitation frequencies # steady-state response q_res, w_res = assembly.ss_response(excitation, omegas) # angle difference between two consecutive nodes q_difference = (q_res.T[:, 1:] - q_res.T[:, :-1]).T # initiate 4 subplots for the 4 shafts fig, axes = plt.subplots(4, 1, figsize=(8, 8)) # Shaft stiffness values are used to calculate the torque from the angle differences shaft_stiffness = [25e+6, 25e+6, 25e+6, 25e+6] # Loop over the 4 shafts to plot the response for each of them for n in range(4): shaft_response = q_difference[n] sum_wave = np.zeros_like(t) # Loop over the harmonic components and cumulate the result for i, (response_component, harmonic) in enumerate(zip(shaft_response, harmonics)): # Get the waveform of each response component this_wave = np.real(response_component*np.exp(1.0j*harmonic*w*t)) # Cumulate the sum wave sum_wave += this_wave # Plot the individual component in newton meters axes[n].plot(t, this_wave*shaft_stiffness[n], '--', c='gray') # Plot the sum excitation signal in newton meters axes[n].plot(t, sum_wave*shaft_stiffness[n], c='red') axes[n].set_title(f'Torque at shaft {n+1}') axes[n].set_xlabel('Time (s)') axes[n].set_ylabel('Torque (Nm)') plt.tight_layout() plt.show() ``` -------------------------------- ### Plot vibratory torque response for ICE example Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/ICE_example.html Defines a function to plot vibratory torque for two shafts (between flywheel and 6th cylinder, and between crankshaft pulley and gear train) across engine speeds, returning the torque arrays. The second block builds the assembly, computes vibratory torque for each rpm, calls plot_results, and then uses openTorsion's Plots class to generate the same torque response plots. ```python def plot_results(rpms, vibratory_torque): ''' Plots the vibratory torque in wanted shafts for each considered engine speed. Parameters ---------- rpms: ndarray All considered engine speeds in rpm vibratory_torque: ndarray Matrix containing maximum vibratory torque at each rotation speed for each shaft. Each row correspond to an engine speed and each column to a shaft. Returns ------- shaft_8: ndarray Array containing maximum vibratory torque at all considered engine speeds for shaft between flywheel and 6th cylinder shaft_1: ndarray Array containing maximum vibratory torque at all considered engine speeds for shaft between crankshaft pulley and gear train ''' shaft_8 = [shaft[7] for shaft in vibratory_torque] plt.plot(rpms, shaft_8, c='red', label='calculated') plt.xlabel('Engine speed (rpm)') plt.ylabel('Vibratory torque (Nm)') plt.title('Torque between the flywheel and the 6th cylinder (Fig.31)') plt.figure() shaft_1 = [shaft[0] for shaft in vibratory_torque] plt.plot(rpms, shaft_1, c='red', label='calculated') plt.xlabel('Engine speed (rpm)') plt.ylabel('Vibratory torque (Nm)') plt.title('Torque between the crankshaft pulley and the gear train (Fig.32)') plt.figure() return shaft_8, shaft_1 ``` ```python assembly = crankshaft_assembly() rpms = np.arange(1000, 2575, 25) # Vibratory torque for every shaft and every considered engine speed vibratory_torque = [] for rpm in rpms: vibratory_torque.append(calculate_response(assembly, rpm)) shaft_8, shaft_1 = plot_results(rpms, vibratory_torque) # Same plots but using openTorsions Plots class plots = opentorsion.Plots(assembly) plots.torque_response_plot(rpms, [np.array(shaft_8), np.array(shaft_1)], True) ``` -------------------------------- ### Assembly() Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Creates an Assembly object that assembles the multi-degree of freedom system matrices and provides functions for modal analysis and response analysis. ```APIDOC ## Assembly() ### Description Creates an Assembly object that assembles the multi-degree of freedom system matrices and provides functions for modal analysis and response analysis. ### Constructor `Assembly(shaft_elements, disk_elements=None, gear_elements=None)` ### Parameters - **shaft_elements** (list) - Required - List containing the shaft elements - **disk_elements** (list) - Optional - List containing the disk elements - **gear_elements** (list) - Optional - List containing the gear elements ### Attributes - **shaft_elements** (list) - List containing the shaft elements - **disk_elements** (list) - List containing the disk elements - **gear_elements** (list) - List containing the gear elements ``` -------------------------------- ### Create PI controller with gains and limit Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/transient_simulation.html Creates a PI controller instance with proportional gain 3, integral gain 3, target velocity 200 RPM, and torque limit 20 Nm. The time step ts is assumed to be defined elsewhere; the comment notes the expected argument order. ```python """ Creating controller """ # Parameters Kp = 3 Ki = 3 target = 200 # RPM limit = 20 # Torque (Nm) # Syntax is: ot.PI(Proportional gain, Integral gain, Time step [s], Target velocity [RPM], Limit [Nm]) controller = PI(Kp, Ki, ts, target, limit) ``` -------------------------------- ### assemble_K() Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Assembles the stiffness matrix. ```APIDOC ## assemble_K() ### Description Assembles the stiffness matrix. ### Returns - **ndarray** - The stiffness matrix ``` -------------------------------- ### Import opentorsion and numpy Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/doctrees/nbsphinx/notebooks/modal_analysis_and_forced response.ipynb Import the required libraries for using the OpenTorsion API and numerical operations. ```python import numpy as np import opentorsion as ot ``` -------------------------------- ### E() Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Assembles the gear constraint matrix. ```APIDOC ## E() ### Description Assembles the gear constraint matrix. ### Returns - **ndarray** - The gear constraint matrix ``` -------------------------------- ### Import OpenTorsion and plotting libraries Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/doctrees/nbsphinx/notebooks/ICE_example.ipynb Imports matplotlib, numpy, scipy, and the OpenTorsion modules (Shaft, Disk, Assembly) required for the torsional vibration analysis. ```python import matplotlib.pyplot as plt import numpy as np import scipy import opentorsion from opentorsion import Shaft, Disk, Assembly ``` -------------------------------- ### Discretize state and input matrices with continuous_2_discrete() Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/transient_simulation.ipynb Discretizes the continuous state and input matrices using the continuous_2_discrete method with a time step of 0.001 seconds, producing the discrete matrices Ad and Bd. ```python """ Discretization of the state and input matrices """ ts = 0.001 # Time step # Syntax is: self.continuous_2_discrete(state matrix, input matrix, time step) Ad, Bd = drivetrain.continuous_2_discrete(A, B, ts=0.001) ``` -------------------------------- ### Simulation loop with state-space update Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/notebooks/transient_simulation.ipynb.txt Initializes the state vector, input vector, time iterations, and RPM conversion factor, then runs a simulation loop that updates the state using the discrete-time state-space matrices Ad and Bd, applies the controller and excitation inputs, and records the states and RPM values. ```python x0 = np.zeros(2 * drivetrain.M.shape[0]) # Initial state ``` ```python u0 = np.zeros(drivetrain.M.shape[0]) # Input vector ``` ```python iterations = np.linspace(0, t_end, int(t_end/0.001)) # Iterations based on simulation time and time step ``` ```python rpm = 60 / (2 * np.pi) # Conversion from rad/s to RPM ``` ```python states_step = [] ``` ```python rpms = [] ``` ```python # Calculating all the states ``` ```python for i in iterations: ``` ```python u0[0] = controller.next_step(x0[drivetrain.M.shape[0]] * rpm) ``` ```python u0[-1] = excitations.step_next(i) ``` ```python x0 = Ad @ x0 + Bd @ u0 ``` ```python states_step.append(x0) ``` ```python rpms.append(x0[drivetrain.M.shape[0]] * rpm) ``` ```python states_step = np.array(states_step) ``` ```python rpms = np.array(rpms) ``` -------------------------------- ### Create model assembly with shafts, disks, and gears Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/transient_simulation.ipynb Defines model parameters and creates shaft, disk, and gear elements. Nodes are connected via shafts or gears; here nodes 2 and 3 are connected via gear elements with a gear ratio of 8. The assembly is created with the ot.Assembly class. ```python """ Creating a model assembly """ # Model parameters I1, k1, c1 = 0.5, 5000, 10 I2, k2, c2 = 0.1, 500, 0.8 I3, k3, c3, d3 = 0.5, 500, 5, 0.2 I4, d4 = 0.1, 0.2 I5, d5 = 1, 5 # Number of teeth in gear elements z1, z2 = 10, 80 # Creating shaft elements # Syntax is: ot.Shaft(node 1, node 2, Length [mm], outer diameter [mm], stiffness [Nm/rad], damping) shaft1 = ot.Shaft(0, 1, L=None, odl=None, k=k1, c=c1) shaft2 = ot.Shaft(1, 2, L=None, odl=None, k=k2, c=c2) shaft3 = ot.Shaft(3, 4, L=None, odl=None, k=k3, c=c3) shafts = [shaft1, shaft2, shaft3] # Creating disk elements # Syntax is: ot.Disk(node, Inertia [kgm^2], damping) disk1 = ot.Disk(0, I=I1) disk2 = ot.Disk(1, I=I2) disk3 = ot.Disk(2, I=I3, c=d3) disk4 = ot.Disk(3, I=I4, c=d4) disk5 = ot.Disk(4, I=I5, c=d5) disks = [disk1, disk2, disk3, disk4, disk5] # Creating gear elements with a gear ratio of 80 / 10 = 8 # Syntax is: ot.Gear(node, Inertia [kgm^2], radius/teeth, parent) gear1 = ot.Gear(2, 0, z1) gear2 = ot.Gear(3, 0, z2, parent=gear1) gears = [gear1, gear2] # Creating an assembly of the elements drivetrain = ot.Assembly(shafts, disks, gear_elements=gears) ``` -------------------------------- ### Calculate state and input matrices with state_space() Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/transient_simulation.ipynb Calls the state_space method of the Assembly class to obtain the continuous-time state matrix A and input matrix B for the drivetrain model. ```python """ Calculating the state and input matrices """ A, B = drivetrain.state_space() ``` -------------------------------- ### Visualize openTorsion assembly with ot.Plots.plot_assembly Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/notebooks/transient_simulation.ipynb.txt Visualizes the drivetrain assembly using the Plots class and its plot_assembly method. Requires an existing ot.Assembly object named drivetrain. ```python # Create a plot_tool object for visualizations plot_tools = ot.Plots(drivetrain) # The assembly can be visualized using the plot_assembly method plot_tools.plot_assembly() ``` -------------------------------- ### assemble_C() Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Assembles the damping matrix. ```APIDOC ## assemble_C() ### Description Assembles the damping matrix. ### Returns - **ndarray** - The damping matrix assembled with component specific damping coefficients ``` -------------------------------- ### state_space(C=None) Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Provides the state-space representation of the system. ```APIDOC ## state_space(C=None) ### Description Returns the state space matrices of the second order system. ### Method Function call ### Parameters - **C** (ndarray, optional) - Optional - Damping matrix, if not given, uses the default damping matrix ### Returns - **A_sys** (ndarray) - State matrix A - **B_sys** (ndarray) - Input matrix B ``` -------------------------------- ### state_matrix(C=None) Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Constructs the state and input matrices for eigenvalue analysis. ```APIDOC ## state_matrix(C=None) ### Description Assembles the state matrices for eigenvalue calculation. ### Method Function call ### Parameters - **C** (ndarray, optional) - Optional - Damping matrix ### Returns - **ndarray** - The state matrix - **ndarray** - The input matrix ``` -------------------------------- ### Run steady-state analysis and plot with openTorsion Plots Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/doctrees/nbsphinx/notebooks/ICE_example.ipynb Runs the steady-state analysis over engine speeds from 1000 to 2550 rpm in 25 rpm steps. It builds the assembly, computes the vibratory torque for each speed, and then plots the results for shafts 8 and 1. Finally, it uses the openTorsion Plots class to generate the same torque response plot. ```python assembly = crankshaft_assembly() rpms = np.arange(1000, 2575, 25) # Vibratory torque for every shaft and every considered engine speed vibratory_torque = [] for rpm in rpms: vibratory_torque.append(calculate_response(assembly, rpm)) shaft_8, shaft_1 = plot_results(rpms, vibratory_torque) # Same plots but using openTorsions Plots class plots = opentorsion.Plots(assembly) plots.torque_response_plot(rpms, [np.array(shaft_8), np.array(shaft_1)], True) ``` -------------------------------- ### state_matrix(C=None) Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Assembles the state matrices for eigenvalue calculation. ```APIDOC ## state_matrix(C=None) ### Description Assembles the state matrices for eigenvalue calculation. ### Parameters - **C** - Optional - Damping matrix ### Returns - State matrix ``` -------------------------------- ### assemble_M() Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Assembles the mass matrix. ```APIDOC ## assemble_M() ### Description Assembles the mass matrix. ### Returns - **ndarray** - The mass matrix ``` -------------------------------- ### state_space(C=None) Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Returns the state space matrices of the second order system. ```APIDOC ## state_space(C=None) ### Description State space matrices of the second order system. ### Parameters - **C** - Optional - Damping matrix ### Returns - State space matrices ``` -------------------------------- ### crankshaft_assembly: build openTorsion crankshaft model Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/notebooks/ICE_example.html Builds an openTorsion Assembly representing a crankshaft finite element model based on the reference in the docstring. Uses predefined inertia (J2..J10), stiffness (k2..k9), and absolute damping values. Returns the assembly instance. ```python def crankshaft_assembly(): ''' A shaft-line Finite Element Model of a crankshaft based on model presented in https://doi.org/10.1243/14644193JMBD126 Fig.4. Returns ------- assembly: openTorsion Assembly class instance The created opentorsion assembly ''' J2 = 0.0170 J3 = 0.0090 J4 = 0.0467 J5 = 0.0327 J6 = 0.0467 J7 = 0.0467 J8 = 0.0327 J9 = 0.0487 J10 = 2.0750 k2 = 1.106e6 k3 = 1.631e6 k4 = 1.253e6 k5 = 1.253e6 k6 = 1.678e6 k7 = 1.253e6 k8 = 1.253e6 k9 = 1.976e6 c_a = 2 # absolute damping shafts, disks = [], [] disks.append(Disk(0, J2)) shafts.append(Shaft(0, 1, None, None, k=k2, I=0)) disks.append(Disk(1, J3)) shafts.append(Shaft(1, 2, None, None, k=k3, I=0)) disks.append(Disk(2, J4, c=c_a)) # cylinder 1 shafts.append(Shaft(2, 3, None, None, k=k4, I=0)) disks.append(Disk(3, J5, c=c_a)) shafts.append(Shaft(3, 4, None, None, k=k5, I=0)) disks.append(Disk(4, J6, c=c_a)) shafts.append(Shaft(4, 5, None, None, k=k6, I=0)) disks.append(Disk(5, J7, c=c_a)) shafts.append(Shaft(5, 6, None, None, k=k7, I=0)) disks.append(Disk(6, J8, c=c_a)) shafts.append(Shaft(6, 7, None, None, k=k8, I=0)) disks.append(Disk(7, J9, c=c_a)) #cylinder 6 shafts.append(Shaft(7, 8, None, None, k=k9, I=0)) disks.append(Disk(8, J10)) # flywheel assembly = Assembly(shafts, disk_elements=disks) return assembly ``` -------------------------------- ### Build global damping matrix C from assembly elements Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/notebooks/ICE_example.ipynb.txt Constructs the global damping matrix by summing element damping matrices for shaft and gear elements. If gear elements exist, applies a transformation matrix to the assembled matrix. ```python C[element.node, element.node] += element.C() if assembly.gear_elements is not None: for element in assembly.gear_elements: C[element.node, element.node] += element.C() # Build transformation matrix E = assembly.E() transform = assembly.T(E) # Calculate transformed mass matrix C = np.dot(np.dot(transform.T, C), transform) return C ``` -------------------------------- ### opentorsion.excitation.TransientExcitations Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/opentorsion.html Class for creating transient excitations (step and impulse) with methods to calculate the next excitation value at each time step. ```APIDOC ## opentorsion.excitation.TransientExcitations ### Description Creates transient excitations (step and impulse) for time-domain analysis. The class provides methods to calculate the next excitation value at each time step. ### Class `opentorsion.excitation.TransientExcitations(ts, t_excite, magnitude)` ### Parameters - **ts** (float) - Time step size - **t_excite** (float) - Time instance for applying the excitation - **magnitude** (float) - Excitation magnitude ### Methods #### impulse_next(t) ##### Description Calculates the next impulse excitation. ##### Parameters - **t** (float) - Current time step ##### Returns - **float** - Torque magnitude of the next excitation #### step_next(t) ##### Description Calculates the next step excitation. ##### Parameters - **t** (float) - Current time step ##### Returns - **float** - Torque magnitude of the next step excitation ``` -------------------------------- ### Perform modal analysis and plot eigenmodes and Campbell diagram with OpenTorsion Source: https://github.com/aalto-arotor/opentorsion/blob/main/docs/_sources/modal_analysis.rst.txt Build a torsional assembly with four shaft elements and five disks, then run modal analysis and generate eigenmode and Campbell plots. The eigenfrequency list contains each frequency twice; the Campbell plot is configured with a 0-5000 rpm range, three modes, harmonics 1-3, and an operating speed of 3600 rpm. ```python import opentorsion as ot # An example assembly # Creating 4 shaft elements using stiffness values # Syntax: ot.Shaft(node 1, node 2, Length [mm], outer diameter [mm], stiffness [Nm/rad]) shaft1 = ot.Shaft(0, 1, L=None, odl=None, k=25e+6) shaft2 = ot.Shaft(1, 2, L=None, odl=None, k=25e+6) shaft3 = ot.Shaft(2, 3, L=None, odl=None, k=25e+6) shaft4 = ot.Shaft(3, 4, L=None, odl=None, k=25e+6) # Creating 5 disk elements # Syntax: ot.Disk(node, inertia [kgm^2]) disk1 = ot.Disk(0, I=100) disk2 = ot.Disk(1, I=10) disk3 = ot.Disk(2, I=50) disk4 = ot.Disk(3, I=10) disk5 = ot.Disk(4, I=80) # Adding the elements to lists corresponding to an element type shafts = [shaft1, shaft2, shaft3, shaft4] disks = [disk1, disk2, disk3, disk4, disk5] # Syntax: ot.Assembly(shaft_elements, disk_elements) assembly = ot.Assembly(shaft_elements=shafts, disk_elements=disks) # initialize OpenTorsion plotting tools plot_tools = ot.Plots(assembly) # Calculation of the system's eigenfrequencies omegas_undamped, omegas_damped, damping_ratios = assembly.modal_analysis() # Print eigenfrequencies. # The list contains each eigenfrequency twice, i.e., eigenfrequencies = [1st, 1st, 2nd, 2nd, 3rd, 3rd, ...] print("Eigenfrequencies [rad/s]: ", omegas_undamped.round(3)) # Plot eigenmodes, takes as parameter the number of eigenmodes to be plotted plot_tools.plot_eigenmodes(modes=3) # Campbell plot takes as parameter # - the rotational frequency range [rpm] # - number of eigenfrequencies to be plotted # - number of harmonics to be plotted # - operating speed range plot_tools.plot_campbell( frequency_range_rpm=[0, 5000], num_modes=3, harmonics=[1, 2, 3], operating_speeds_rpm=[3600] ) ```