RatRod_rs/elements/
truss.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 Truss {
11 pub nodes : (Rc<Node>, Rc<Node>),
12 pub material : Rc<Material>,
13 pub section : Rc<Section>
14
15}
16
17impl Truss {
18 pub fn get_length(&self) -> f64{
19 let (node1, node2) = &self.nodes;
20 ((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()
21 }
22
23 pub fn get_matrix(&self, dimension : u8, mat_size : usize, i:usize, j:usize) -> CooMat{
24 match dimension {
25 1 => self.get_1_d_matrix(mat_size, i, j),
26 2 => self.get_2_d_matrix(mat_size,i, j),
27 3 => self.get_3_d_matrix(mat_size,i, j),
28 _ => panic!("Invalid dimension")
29 }
30 }
31
32 pub fn get_1_d_matrix(&self, mat_size : usize, i:usize, j:usize) -> CooMat {
33 let l = self.get_length();
34 let stiffness = self.material.e * self.section.s / l;
35 let mut matrix = CooMat::new(mat_size,mat_size);
36 matrix.add_value(i, i, stiffness);
37 matrix.add_value(j, i, -1. * stiffness);
38 matrix.add_value(i, j, -1. * stiffness);
39 matrix.add_value(j, j, stiffness);
40 matrix
41 }
42
43 pub fn get_2_d_matrix(&self, mat_size : usize, x:usize, y:usize) -> CooMat {
44 let l = self.get_length();
45 let stiffness = self.material.e * self.section.s / l;
46 let (node1, node2) = &self.nodes;
47 let c = [(node1.x - node2.x)/l, (node1.y - node2.y)/l];
48 let mut matrix = CooMat::new(mat_size, mat_size);
49 for i in 0..2 {
50 for j in 0..2 {
51 matrix.add_value(3*x + i, 3*x + j, stiffness * c[i] * c[j]);
52 matrix.add_value(i + 3*y, 3*x + j, -1. * stiffness * c[i] * c[j]);
53 matrix.add_value(3*x + i, j + 3*y, -1. * stiffness * c[i] * c[j]);
54 matrix.add_value(i + 3*y, j + 3*y, stiffness * c[i] * c[j]);
55 }
56 }
57
58 matrix
59 }
60
61 pub fn get_3_d_matrix(&self, mat_size : usize, x:usize, y:usize) -> CooMat {
62 let l = self.get_length();
63 let stiffness = self.material.e * self.section.s / l;
64 let (node1, node2) = &self.nodes;
65 let c = [(node1.x - node2.x)/l, (node1.y - node2.y)/l, (node1.z - node2.z)/l];
66 let mut matrix = CooMat::new(mat_size, mat_size);
67
68 for i in 0..3 {
69 for j in 0..3 {
70 matrix.add_value(3*x + i, 3*x + j, stiffness * c[i] * c[j]);
71 matrix.add_value(i + 3*y, 3*x + j, -1. * stiffness * c[i] * c[j]);
72 matrix.add_value(3*x + i, j + 3*y, -1. * stiffness * c[i] * c[j]);
73 matrix.add_value(i + 3*y, j + 3*y, stiffness * c[i] * c[j]);
74 }
75 }
76
77 matrix
78 }
79}