RatRod_rs/elements/
beam.rs1use sparse_matrix::matrix::coo_mat::CooMat;
2
3use std::rc::Rc;
4
5use crate::{node::Node, material::Material, section::Section};
6
7use serde::{Serialize, Deserialize};
8
9#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
10pub struct Beam{
11 pub nodes : (Rc<Node>, Rc<Node>),
12 pub material : Rc<Material>,
13 pub section : Rc<Section>
14}
15
16impl Beam{
17 pub fn get_length(&self) -> f64{
18 let (node1, node2) = &self.nodes;
19 ((node1.x - node2.x)*(node1.x - node2.x) + (node1.y - node2.y)*(node1.y - node2.y) + (node1.z - node2.z)*(node1.z - node2.z)).sqrt()
20 }
21
22 pub fn get_matrix(&self, dimension : u8, mat_size : usize, i:usize, j:usize) -> CooMat{
23 match dimension {
24 2 => self.get_2_d_matrix(mat_size,i, j),
25 3 => self.get_3_d_matrix(mat_size,i, j),
26 _ => panic!("Invalid dimension")
27 }
28 }
29
30 pub fn get_2_d_matrix(&self, mat_size : usize, x:usize, y:usize) -> CooMat {
31 let l = self.get_length();
32 let truss_stiffness = self.material.e * self.section.s / l;
33 let beam_stiffness = self.material.e * self.section.i / l / l / l;
34 let (node1, node2) = &self.nodes;
35 let mut matrix = CooMat::new(mat_size, mat_size);
36 matrix.add_value(3*x , 3*x , truss_stiffness);
37 matrix.add_value(3*y , 3*x , -1. * truss_stiffness);
38 matrix.add_value(3*x , 3*y , -1. * truss_stiffness);
39 matrix.add_value(3*y , 3*y , truss_stiffness);
40 matrix.add_value(3*x + 1, 3*x + 1, 12. * beam_stiffness);
41 matrix.add_value(3*x + 2, 3*x + 1, 6. * l * beam_stiffness);
42 matrix.add_value(3*x + 1, 3*x + 2, 6. * l * beam_stiffness);
43 matrix.add_value(3*x + 2, 3*x + 2, 4. * l * l * beam_stiffness);
44 matrix.add_value(3*y + 1, 3*x + 1, -12. * beam_stiffness);
45 matrix.add_value(3*y + 2, 3*x + 1, 6. * l * beam_stiffness);
46 matrix.add_value(3*y + 1, 3*x + 2, -6. * l * beam_stiffness);
47 matrix.add_value(3*y + 2, 3*x + 2, 2. * l * l * beam_stiffness);
48 matrix.add_value(3*x + 1, 3*y + 1, -12. * beam_stiffness);
49 matrix.add_value(3*x + 2, 3*y + 1, -6. * l * beam_stiffness);
50 matrix.add_value(3*x + 1, 3*y + 2, 6. * l * beam_stiffness);
51 matrix.add_value(3*x + 2, 3*y + 2, 2. * l * l * beam_stiffness);
52 matrix.add_value(3*y + 1, 3*y + 1, 12. * beam_stiffness);
53 matrix.add_value(3*y + 2, 3*y + 1, -6. * l * beam_stiffness);
54 matrix.add_value(3*y + 1, 3*y + 2, -6. * l * beam_stiffness);
55 matrix.add_value(3*y + 2, 3*y + 2, 4. * l * l * beam_stiffness);
56 let mut rotation = CooMat::identity(mat_size);
57 let c = (node1.x - node2.x).abs()/l;
58 let s = (node1.y - node2.y).abs()/l;
59 rotation.add_value(3*x , 3*x , c);
60 rotation.add_value(3*x + 1, 3*x , -s);
61 rotation.add_value(3*x , 3*x + 1, s);
62 rotation.add_value(3*x + 1, 3*x + 1, c);
63 rotation.add_value(3*y , 3*y , c);
64 rotation.add_value(3*y + 1, 3*y , -s);
65 rotation.add_value(3*y , 3*y + 1, s);
66 rotation.add_value(3*y + 1, 3*y + 1, c);
67
68 matrix = &(rotation.transposed()) * &(&matrix * &rotation);
69
70 matrix
71 }
72
73 pub fn get_3_d_matrix(&self, mat_size : usize, x:usize, y:usize) -> CooMat {
74 unimplemented!();
75 }
76}