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
//! This module provides the [Keyword](Keyword) enum to
//! classify lines according to what card type they belong to. The term
//! "Keyword" is from the FEM solver Pamcrash, but generally used among FEM
//! solvers.

/// An enum to denote the several types of cards a line might belong to. For now
/// carries only information equivalent to the keyword, not the subtypes, e.g.
/// CNTAC types 33 and 36 will both be denoted by type Cntac
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Keyword {
  // Node
  Node,
  Cnode,
  Mass,
  Nsmas,
  Nsmas2,
  // Element
  Solid,
  Hexa20,
  Pent15,
  Penta6,
  Tetr10,
  Tetr4,
  Bshel,
  Tshel,
  Shell,
  Shel6,
  Shel8,
  Membr,
  Beam,
  Sprgbm,
  Bar,
  Spring,
  Joint,
  Kjoin,
  Mtojnt,
  Sphel,
  Sphelo,
  Gap,
  Impma,
  // Link
  Elink,
  Llink,
  Slink,
  Plink,
  Tied,
  // Part 3D
  PartSolid,
  PartBshel,
  PartTetra,
  PartSphel,
  PartCos3d,
  // Part 2D
  PartTshel,
  PartShell,
  PartMembr,
  // Part 1D
  PartBar,
  PartBeam,
  PartSpring,
  PartSprgbm,
  PartMbspr,
  PartJoint,
  PartKjoin,
  PartMbkjn,
  PartMtojnt,
  PartTied,
  PartSlink,
  PartElink,
  PartLlink,
  PartPlink,
  PartGap,
  // Constraint
  Mtoco,
  Otmco,
  Rbody0,
  Rbody1,
  Rbody2,
  Rbody3,
  // Auxiliaries
  Group,
}

impl Keyword {
  /// Parse a string to determine if it starts with the keyword of a card.
  #[inline]
  pub fn parse<T: AsRef<str>>(s: &T) -> Option<Keyword> {
    use self::Keyword::*;

    let s = s.as_ref().as_bytes();
    let len = s.len();

    if len == 0 || len < 8 {
      None
    } else {
      let start = &s[0..8];

      match start {
        // Node
        b"NODE  / " => Some(Node),
        b"CNODE / " => Some(Cnode),
        b"MASS  / " => Some(Mass),
        b"NSMAS / " => Some(Nsmas),
        b"NSMAS2/ " => Some(Nsmas2),
        // Element
        b"SOLID / " => Some(Solid),
        b"HEXA20/ " => Some(Hexa20),
        b"PENT15/ " => Some(Pent15),
        b"PENTA6/ " => Some(Penta6),
        b"TETR10/ " => Some(Tetr10),
        b"TETR4 / " => Some(Tetr4),
        b"BSHEL / " => Some(Bshel),
        b"TSHEL / " => Some(Tshel),
        b"SHELL / " => Some(Shell),
        b"SHEL6 / " => Some(Shel6),
        b"SHEL8 / " => Some(Shel8),
        b"MEMBR / " => Some(Membr),
        b"BEAM  / " => Some(Beam),
        b"SPRGBM/ " => Some(Sprgbm),
        b"BAR   / " => Some(Bar),
        b"SPRING/ " => Some(Spring),
        b"JOINT / " => Some(Joint),
        b"KJOIN / " => Some(Kjoin),
        b"MTOJNT/ " => Some(Mtojnt),
        b"SPHEL / " => Some(Sphel),
        b"SPHELO/ " => Some(Sphelo),
        b"GAP   / " => Some(Gap),
        b"IMPMA / " => Some(Impma),
        // Link
        b"ELINK / " => Some(Elink),
        b"LLINK / " => Some(Llink),
        b"SLINK / " => Some(Slink),
        b"PLINK / " => Some(Plink),
        b"TIED  / " => Some(Tied),
        // Part
        b"PART  / " => {
          if len < 24 {
            None
          } else {
            let mut p = &s[16..24];

            if let Some(first) = p.iter().position(|c| c != &b' ') {
              if let Some(last) = p.iter().rposition(|c| c != &b' ') {
                p = &p[first..last + 1]
              } else {
                return None;
              }
            } else {
              return None;
            }

            match p {
              b"SOLID" => Some(PartSolid),
              b"BSHEL" => Some(PartBshel),
              b"TETRA" => Some(PartTetra),
              b"SPHEL" => Some(PartSphel),
              b"COS3D" => Some(PartCos3d),
              b"TSHEL" => Some(PartTshel),
              b"SHELL" => Some(PartShell),
              b"MEMBR" => Some(PartMembr),
              b"BAR" => Some(PartBar),
              b"BEAM" => Some(PartBeam),
              b"SPRING" => Some(PartSpring),
              b"SPRGBM" => Some(PartSprgbm),
              b"MBSPR" => Some(PartMbspr),
              b"JOINT" => Some(PartJoint),
              b"KJOIN" => Some(PartKjoin),
              b"MTOJNT" => Some(PartMtojnt),
              b"MBKJN" => Some(PartMbkjn),
              b"TIED" => Some(PartTied),
              b"SLINK" => Some(PartSlink),
              b"ELINK" => Some(PartElink),
              b"LLINK" => Some(PartLlink),
              b"PLINK" => Some(PartPlink),
              b"GAP" => Some(PartGap),
              _ => None,
            }
          }
        }
        // Constraint
        b"MTOCO / " => Some(Mtoco),
        b"OTMCO / " => Some(Otmco),
        b"RBODY / " => {
          if len < 32 {
            None
          } else {
            let p = &s[24..32];

            match p.iter().find(|&&x| x != b' ') {
              Some(b'0') => Some(Rbody0),
              Some(b'1') => Some(Rbody1),
              Some(b'2') => Some(Rbody2),
              Some(b'3') => Some(Rbody3),
              _ => None,
            }
          }
        }
        // Auxiliaries
        b"GROUP / " => Some(Group),
        _ => None,
      }
    }
  }
}