1 // =============================================================================
2 // PROJECT CHRONO - http://projectchrono.org
3 //
4 // Copyright (c) 2014 projectchrono.org
5 // All rights reserved.
6 //
7 // Use of this source code is governed by a BSD-style license that can be found
8 // in the LICENSE file at the top level of the distribution and at
9 // http://projectchrono.org/license-chrono.txt.
10 //
11 // =============================================================================
12 // Authors: Alessandro Tasora, Radu Serban
13 // =============================================================================
14 
15 #ifndef CHVARIABLESSHAFT_H
16 #define CHVARIABLESSHAFT_H
17 
18 #include "chrono/solver/ChVariables.h"
19 
20 namespace chrono {
21 
22 class ChShaft;
23 
24 /// Specialized class for representing a 1-DOF item for a system, that is
25 /// a shaft, with inertia and associated variable (rotational speed)
26 
27 class ChApi ChVariablesShaft : public ChVariables {
28   private:
29     ChShaft* m_shaft;      ///< associated shaft element
30     double m_inertia;      ///< shaft inertia
31     double m_inv_inertia;  ///< inverse of shaft inertia value
32 
33   public:
ChVariablesShaft()34     ChVariablesShaft() : ChVariables(1), m_shaft(NULL), m_inertia(1), m_inv_inertia(1) {}
~ChVariablesShaft()35     virtual ~ChVariablesShaft() {}
36 
37     /// Assignment operator: copy from other object
38     ChVariablesShaft& operator=(const ChVariablesShaft& other);
39 
40     /// The number of scalar variables in the vector qb
41     /// (dof=degrees of freedom)
Get_ndof()42     virtual int Get_ndof() const override { return 1; }
43 
44     /// Get the inertia associated with rotation of the shaft
GetInertia()45     double GetInertia() const { return m_inertia; }
46 
47     /// Get the inverse of the inertia associated with rotation of the shaft
GetInvInertia()48     double GetInvInertia() const { return m_inv_inertia; }
49 
50     /// Set the inertia associated with rotation of the shaft
51     void SetInertia(double inertia);
52 
GetShaft()53     ChShaft* GetShaft() { return m_shaft; }
SetShaft(ChShaft * shaft)54     void SetShaft(ChShaft* shaft) { m_shaft = shaft; }
55 
56     /// Computes the product of the inverse mass matrix by a
57     /// vector, and set in result: result = [invMb]*vect
58     virtual void Compute_invMb_v(ChVectorRef result, ChVectorConstRef vect) const override;
59 
60     /// Computes the product of the inverse mass matrix by a
61     /// vector, and increment result: result += [invMb]*vect
62     virtual void Compute_inc_invMb_v(ChVectorRef result, ChVectorConstRef vect) const override;
63 
64     /// Computes the product of the mass matrix by a
65     /// vector, and set in result: result = [Mb]*vect
66     virtual void Compute_inc_Mb_v(ChVectorRef result, ChVectorConstRef vect) const override;
67 
68     /// Computes the product of the corresponding block in the system matrix (ie. the mass matrix) by 'vect', scale by
69     /// c_a, and add to 'result'.
70     /// NOTE: the 'vect' and 'result' vectors must already have the size of the total variables&constraints in the
71     /// system; the procedure will use the ChVariable offsets (that must be already updated) to know the indexes in
72     /// result and vect.
73     virtual void MultiplyAndAdd(ChVectorRef result, ChVectorConstRef vect, const double c_a) const override;
74 
75     /// Add the diagonal of the mass matrix scaled by c_a, to 'result'.
76     /// NOTE: the 'result' vector must already have the size of system unknowns, ie the size of the total variables &
77     /// constraints in the system; the procedure will use the ChVariable offset (that must be already updated) as index.
78     virtual void DiagonalAdd(ChVectorRef result, const double c_a) const override;
79 
80     /// Build the mass matrix (for these variables) scaled by c_a, storing
81     /// it in 'storage' sparse matrix, at given column/row offset.
82     /// Note, most iterative solvers don't need to know mass matrix explicitly.
83     /// Optimized: doesn't fill unneeded elements except mass.
84     virtual void Build_M(ChSparseMatrix& storage, int insrow, int inscol, const double c_a) override;
85 
86     /// Method to allow serialization of transient data to archives.
ArchiveOUT(ChArchiveOut & marchive)87     virtual void ArchiveOUT(ChArchiveOut& marchive) override {
88         // version number
89         marchive.VersionWrite<ChVariablesShaft>();
90         // serialize parent class
91         ChVariables::ArchiveOUT(marchive);
92         // serialize all member data:
93         marchive << CHNVP(m_inertia);
94     }
95 
96     /// Method to allow de-serialization of transient data from archives.
ArchiveIN(ChArchiveIn & marchive)97     virtual void ArchiveIN(ChArchiveIn& marchive) override {
98         // version number
99         /*int version =*/ marchive.VersionRead<ChVariablesShaft>();
100         // deserialize parent class
101         ChVariables::ArchiveIN(marchive);
102         // stream in all member data:
103         marchive >> CHNVP(m_inertia);
104         SetInertia(m_inertia);
105     }
106 };
107 
108 }  // end namespace chrono
109 
110 #endif
111