structural-shapes 0.2.3

Common structural shapes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#![warn(clippy::all)]
#![warn(missing_docs)]
#![warn(clippy::missing_docs_in_private_items)]
#![doc = include_str!("../README.md")]

use num::{Float, NumCast};
use typenum::{P4, Z0};
use uom::si::{
    f64::{Area, Length, Volume},
    length::meter,
    {Quantity, ISQ, SI},
};
type SecondAreaMomentofInertia = Quantity<ISQ<P4, Z0, Z0, Z0, Z0, Z0, Z0>, SI<f64>, f64>;

/// A helper function supporting conversion of floating point numbers to meters
pub fn meters<T: Float>(l: T) -> Length {
    Length::new::<meter>(NumCast::from(l).expect("The input must be castable to a float."))
}

/// This enum contains different structural shapes
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub enum StructuralShape {
    /// This is a pipe with an outer_radius and a thickness
    Pipe {
        /// Outer radius of hte pipe
        outer_radius: Length,
        /// Thickness of the pipe wall
        thickness: Length,
        /// Coordinates of center of gravity
        center_of_gravity: (Length, Length),
    },
    /// This is an I-Beam, with a width, height, web thickness, and flange thickness
    IBeam {
        /// Width of the beam
        width: Length,
        /// Height of the beam
        height: Length,
        /// Thickness of the web
        web_thickness: Length,
        /// Thickness of the flange
        flange_thickness: Length,
        /// Coordinates of center of gravity
        center_of_gravity: (Length, Length),
    },
    /// This is a box beam with a width, height, and thickness
    BoxBeam {
        /// Width of the box beam
        width: Length,
        /// Height of the box beam
        height: Length,
        /// Thickness of the wall
        thickness: Length,
        /// Coordinates of center of gravity
        center_of_gravity: (Length, Length),
    },
    /// This is a rod with a radius only
    Rod {
        /// Radius of the road
        radius: Length,
        /// Coordinates of center of gravity
        center_of_gravity: (Length, Length),
    },
    /// This is a solid rectangular with width and height
    Rectangle {
        /// Width of the rectangle
        width: Length,
        /// Height of the rectangle
        height: Length,
        /// Coordinates of center of gravity
        center_of_gravity: (Length, Length),
    },
}

impl StructuralShape {
    /// Make a new rod without COG
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_rod(2.0);
    /// ```
    pub fn new_rod(radius: f64) -> StructuralShape {
        StructuralShape::Rod {
            radius: meters(radius),
            center_of_gravity: (meters(0.0), meters(0.0)),
        }
    }

    /// Make a new pipe without COG
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_pipe(2.0, 0.15);
    /// ```
    pub fn new_pipe(radius: f64, thickness: f64) -> StructuralShape {
        StructuralShape::Pipe {
            outer_radius: meters(radius),
            thickness: meters(thickness),
            center_of_gravity: (meters(0.0), meters(0.0)),
        }
    }

    /// Make a new rectangle without COG
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_rectangle(2.0, 2.0);
    /// ```
    pub fn new_rectangle(height: f64, width: f64) -> StructuralShape {
        StructuralShape::Rectangle {
            width: meters(width),
            height: meters(height),
            center_of_gravity: (meters(0.0), meters(0.0)),
        }
    }

    /// Make a new boxbeam without COG
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_boxbeam(2.0, 2.0, 0.15);
    /// ```
    pub fn new_boxbeam(height: f64, width: f64, thickness: f64) -> StructuralShape {
        StructuralShape::BoxBeam {
            width: meters(width),
            height: meters(height),
            thickness: meters(thickness),
            center_of_gravity: (meters(0.0), meters(0.0)),
        }
    }

    /// Make a new Ibeam without COG
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_ibeam(2.0, 2.0, 0.15, 0.15);
    /// ```
    pub fn new_ibeam(
        height: f64,
        width: f64,
        web_thickness: f64,
        flange_thickness: f64,
    ) -> StructuralShape {
        StructuralShape::IBeam {
            width: meters(width),
            height: meters(height),
            web_thickness: meters(web_thickness),
            center_of_gravity: (meters(0.0), meters(0.0)),
            flange_thickness: meters(flange_thickness),
        }
    }

    /// This function returns the moment of inertia of the structural shape around the x-axis
    /// ```
    /// # use structural_shapes::{StructuralShape};
    /// let shape = StructuralShape::new_rod(2.0);
    /// let moi = shape.moi_x();
    /// ```
    pub fn moi_x(&self) -> SecondAreaMomentofInertia {
        match *self {
            StructuralShape::Pipe {
                outer_radius,
                thickness,
                center_of_gravity,
            } => CompositeShape::new()
                .add(StructuralShape::Rod {
                    radius: outer_radius,
                    center_of_gravity,
                })
                .sub(StructuralShape::Rod {
                    radius: (outer_radius - thickness),
                    center_of_gravity,
                })
                .moi_x(),
            StructuralShape::IBeam {
                width,
                height,
                flange_thickness,
                web_thickness,
                center_of_gravity,
            } => composite_ibeam(
                width,
                height,
                web_thickness,
                flange_thickness,
                center_of_gravity,
            )
            .moi_y(),
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
                center_of_gravity,
            } => CompositeShape::new()
                .add(StructuralShape::Rectangle {
                    width,
                    height,
                    center_of_gravity,
                })
                .sub(StructuralShape::Rectangle {
                    width: (width - 2.0 * thickness),
                    height: (height - 2.0 * thickness),
                    center_of_gravity,
                })
                .moi_x(),
            StructuralShape::Rod {
                radius,
                center_of_gravity,
            } => {
                std::f64::consts::PI * radius * radius * radius * radius / 4.0
                    + self.area() * center_of_gravity.0 * center_of_gravity.0
            }
            StructuralShape::Rectangle {
                width,
                height,
                center_of_gravity,
            } => {
                width * height * height * height / 12.0
                    + self.area() * center_of_gravity.0 * center_of_gravity.0
            }
        }
        .into()
    }

    /// This function returns the moment of inertia of hte structural shape around the y-axis
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_rod(2.0);
    /// let area = shape.moi_y();
    /// ```
    pub fn moi_y(&self) -> SecondAreaMomentofInertia {
        match *self {
            StructuralShape::Pipe {
                outer_radius,
                thickness,
                center_of_gravity,
            } => StructuralShape::Pipe {
                outer_radius,
                thickness,
                center_of_gravity: swap(center_of_gravity),
            }
            .moi_x(),

            StructuralShape::IBeam {
                height,
                width,
                flange_thickness,
                web_thickness,
                center_of_gravity,
            } => composite_ibeam(
                width,
                height,
                web_thickness,
                flange_thickness,
                center_of_gravity,
            )
            .moi_y(),
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
                center_of_gravity,
            } => StructuralShape::BoxBeam {
                width: height,
                height: width,
                thickness,
                center_of_gravity: swap(center_of_gravity),
            }
            .moi_x(),
            StructuralShape::Rod {
                radius,
                center_of_gravity,
            } => {
                std::f64::consts::PI * radius * radius * radius * radius / 4.0
                    + self.area() * center_of_gravity.1 * center_of_gravity.1
            }
            StructuralShape::Rectangle {
                width,
                height,
                center_of_gravity,
            } => {
                width * height * height * height / 12.0
                    + self.area() * center_of_gravity.1 * center_of_gravity.1
            }
        }
    }

    /// This function returns the polar moment of inertia of the composite shape about the origin.
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_rod(2.0);
    /// let area = shape.polar_moi();
    /// ```
    pub fn polar_moi(&self) -> SecondAreaMomentofInertia {
        self.moi_x() + self.moi_y()
    }

    /// This function returns the cross-sectional area of the structural shape
    /// ```
    /// # use structural_shapes::StructuralShape;
    /// let shape = StructuralShape::new_rod(2.0);
    /// let area = shape.area();
    /// ```
    pub fn area(&self) -> Area {
        match *self {
            StructuralShape::Pipe {
                outer_radius,
                thickness,
                ..
            } => {
                std::f64::consts::PI
                    * (outer_radius * outer_radius
                        - (outer_radius - thickness) * (outer_radius - thickness))
            }
            StructuralShape::IBeam {
                width,
                height,
                web_thickness,
                flange_thickness,
                ..
            } => width * height - (height - 2.0 * flange_thickness) * (width - web_thickness),
            StructuralShape::BoxBeam {
                width,
                height,
                thickness,
                ..
            } => width * height - (width - 2.0 * thickness) * (height - 2.0 * thickness),
            StructuralShape::Rod { radius, .. } => std::f64::consts::PI * radius * radius,
            StructuralShape::Rectangle { width, height, .. } => width * height,
        }
    }

    /// A function to set the center of gravity of a shape
    /// ```
    /// # use structural_shapes::{StructuralShape};
    /// let shape = StructuralShape::new_rod(2.0).with_cog(0.0, 0.0);
    /// let moi = shape.moi_x();
    /// ```
    pub fn with_cog(&mut self, x: f64, y: f64) -> StructuralShape {
        self.set_cog((meters(x), meters(y)));
        self.clone()
    }

    /// A function to return the current center of gravity for a shape
    pub(crate) fn get_cog(&self) -> (Length, Length) {
        match *self {
            StructuralShape::Pipe {
                center_of_gravity, ..
            } => center_of_gravity,
            StructuralShape::IBeam {
                center_of_gravity, ..
            } => center_of_gravity,
            StructuralShape::BoxBeam {
                center_of_gravity, ..
            } => center_of_gravity,
            StructuralShape::Rod {
                center_of_gravity, ..
            } => center_of_gravity,
            StructuralShape::Rectangle {
                center_of_gravity, ..
            } => center_of_gravity,
        }
    }

    /// A function to set the current center of gravity for a shape
    pub(crate) fn set_cog(&mut self, cog: (Length, Length)) {
        match *self {
            StructuralShape::Pipe {
                ref mut center_of_gravity,
                ..
            } => {
                *center_of_gravity = cog;
            }
            StructuralShape::IBeam {
                ref mut center_of_gravity,
                ..
            } => {
                *center_of_gravity = cog;
            }
            StructuralShape::BoxBeam {
                ref mut center_of_gravity,
                ..
            } => {
                *center_of_gravity = cog;
            }
            StructuralShape::Rod {
                ref mut center_of_gravity,
                ..
            } => {
                *center_of_gravity = cog;
            }
            StructuralShape::Rectangle {
                ref mut center_of_gravity,
                ..
            } => {
                *center_of_gravity = cog;
            }
        };
    }
}

/// A composite composed of multiple individual shapes
/// ```
/// # use structural_shapes::*;
/// let x = CompositeShape::new()
///     .add(StructuralShape::new_rod(2.0).with_cog(2.0, 0.0))
///     .add(StructuralShape::new_rod(2.0).with_cog(-2.0, 0.0));
/// ```
#[derive(Clone, Debug)]
pub struct CompositeShape {
    /// Constituent shapes
    pub shapes: Vec<(i8, StructuralShape)>,
}

impl CompositeShape {
    /// This creates a new composite shape, identical to default
    pub fn new() -> Self {
        Self::default()
    }
    /// This function adds a new shape to the composite
    pub fn add(&mut self, new_shape: StructuralShape) -> Self {
        self.shapes.push((1, new_shape));
        self.clone()
    }
    /// This function subtracts a new shape to the composite
    pub fn sub(&mut self, new_shape: StructuralShape) -> Self {
        self.shapes.push((-1, new_shape));
        self.clone()
    }
    /// Calculate center of gravity and update COG of members
    pub fn calculate_cog(&self) -> (Length, Length) {
        let area = self.area();
        let area_times_cx: Volume = self
            .shapes
            .iter()
            .map(|x| {
                let center_of_gravity = x.1.get_cog();
                (x.0 as f64) * x.1.area() * center_of_gravity.0
            })
            .sum();
        let area_times_cy: Volume = self
            .shapes
            .iter()
            .map(|x| {
                let center_of_gravity = x.1.get_cog();
                (x.0 as f64) * x.1.area() * center_of_gravity.1
            })
            .sum();
        let cog_x = area_times_cx / area;
        let cog_y = area_times_cy / area;
        (cog_x, cog_y)
    }
    /// Shift structure to have cog at (0.0,0.0)
    pub fn update_cog(&mut self) {
        let (cog_x, cog_y) = self.calculate_cog();
        self.shapes.iter_mut().for_each(|x| {
            let (_, ref mut shape) = x;
            let (old_x, old_y) = shape.get_cog();
            shape.set_cog((old_x - cog_x, old_y - cog_y));
        });
    }

    /// This function returns the moment of inertia of the composite shape around the x-axis
    pub fn moi_x(&self) -> SecondAreaMomentofInertia {
        self.shapes.iter().map(|x| (x.0 as f64) * x.1.moi_x()).sum()
    }
    /// This function returns the moment of inertia of the composite shape around the y-axis
    pub fn moi_y(&self) -> SecondAreaMomentofInertia {
        self.shapes.iter().map(|x| (x.0 as f64) * x.1.moi_y()).sum()
    }
    /// This function returns the polar moment of inertia of the composite shape around the origin.
    pub fn polar_moi(&self) -> SecondAreaMomentofInertia {
        self.moi_x() + self.moi_y()
    }
    /// This function returns the area of the composite shape
    pub fn area(&self) -> Area {
        self.shapes.iter().map(|x| (x.0 as f64) * x.1.area()).sum()
    }
}

/// Implement default
impl Default for CompositeShape {
    fn default() -> Self {
        CompositeShape { shapes: vec![] }
    }
}

/// Function for swapping values
fn swap(pair: (Length, Length)) -> (Length, Length) {
    (pair.1, pair.0)
}

/// Create a composite I-beam from some initial parameters
fn composite_ibeam(
    width: Length,
    height: Length,
    web_thickness: Length,
    flange_thickness: Length,
    center_of_gravity: (Length, Length),
) -> CompositeShape {
    CompositeShape::new()
        .add(StructuralShape::Rectangle {
            width,
            height,
            center_of_gravity,
        })
        .sub(StructuralShape::Rectangle {
            width: ((width - web_thickness) / 2.0),
            height: (height - 2.0 * flange_thickness),
            center_of_gravity: (
                center_of_gravity.0 - ((width - web_thickness) / 4.0) - web_thickness / 2.0,
                center_of_gravity.1,
            ),
        })
        .sub(StructuralShape::Rectangle {
            width: ((width - web_thickness) / 2.0),
            height: (height - 2.0 * flange_thickness),
            center_of_gravity: (
                center_of_gravity.0 + ((width - web_thickness) / 4.0) + web_thickness / 2.0,
                center_of_gravity.1,
            ),
        })
}