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
// Chemfiles, a modern library for chemistry file reading and writing
// Copyright (C) 2015-2017 Guillaume Fraux
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
use std::ops::Drop;

use chemfiles_sys::*;
use errors::{check, Error};
use Result;

/// Available unit cell shapes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CellShape {
    /// Orthorhombic cell, with the three angles equals to 90°.
    Orthorhombic,
    /// Triclinic cell, with any values for the angles.
    Triclinic,
    /// Infinite cell, to use when there is no cell.
    Infinite,
}

impl From<chfl_cell_shape_t> for CellShape {
    fn from(celltype: chfl_cell_shape_t) -> CellShape {
        match celltype {
            chfl_cell_shape_t::CHFL_CELL_ORTHORHOMBIC => CellShape::Orthorhombic,
            chfl_cell_shape_t::CHFL_CELL_TRICLINIC => CellShape::Triclinic,
            chfl_cell_shape_t::CHFL_CELL_INFINITE => CellShape::Infinite,
        }
    }
}

impl From<CellShape> for chfl_cell_shape_t {
    fn from(celltype: CellShape) -> chfl_cell_shape_t {
        match celltype {
            CellShape::Orthorhombic => chfl_cell_shape_t::CHFL_CELL_ORTHORHOMBIC,
            CellShape::Triclinic => chfl_cell_shape_t::CHFL_CELL_TRICLINIC,
            CellShape::Infinite => chfl_cell_shape_t::CHFL_CELL_INFINITE,
        }
    }
}

/// An `UnitCell` represent the box containing the atoms, and its periodicity.
///
/// An unit cell is fully represented by three lengths (a, b, c); and three
/// angles (alpha, beta, gamma). The angles are stored in degrees, and the
/// lengths in Angstroms.
///
/// A cell also has a matricial representation, by projecting the three base
/// vector into an orthonormal base. We choose to represent such matrix as an
/// upper triangular matrix:
///
/// ```text
/// | a_x   b_x   c_x |
/// |  0    b_y   c_y |
/// |  0     0    c_z |
/// ```
pub struct UnitCell {
    handle: *const CHFL_CELL
}

impl Clone for UnitCell {
    fn clone(&self) -> UnitCell {
        unsafe {
            let new_handle = chfl_cell_copy(self.as_ptr());
            UnitCell::from_ptr(new_handle).expect(
                "Out of memory when copying an UnitCell"
            )
        }
    }
}

impl UnitCell {
    /// Create an `UnitCell` from a C pointer.
    ///
    /// This function is unsafe because no validity check is made on the pointer,
    /// except for it being non-null.
    #[inline]
    #[doc(hidden)]
    pub unsafe fn from_ptr(ptr: *const CHFL_CELL) -> Result<UnitCell> {
        if ptr.is_null() {
            Err(Error::null_ptr())
        } else {
            Ok(UnitCell{handle: ptr})
        }
    }

    /// Get the underlying C pointer as a const pointer.
    #[inline]
    #[doc(hidden)]
    pub fn as_ptr(&self) -> *const CHFL_CELL {
        self.handle
    }

    /// Get the underlying C pointer as a mutable pointer.
    #[inline]
    #[doc(hidden)]
    pub fn as_mut_ptr(&mut self) -> *mut CHFL_CELL {
        self.handle as *mut CHFL_CELL
    }

    /// Create an `Orthorhombic` `UnitCell` from the three lengths, in Angstroms.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::{UnitCell, CellShape};
    /// let cell = UnitCell::new(30.0, 30.0, 23.0).unwrap();
    ///
    /// assert_eq!(cell.lengths(), Ok((30.0, 30.0, 23.0)));
    /// assert_eq!(cell.angles(), Ok((90.0, 90.0, 90.0)));
    /// assert_eq!(cell.shape(), Ok(CellShape::Orthorhombic));
    /// ```
    pub fn new(a: f64, b: f64, c: f64) -> Result<UnitCell> {
        let lengths = [a, b, c];
        unsafe {
            let handle = chfl_cell(lengths.as_ptr());
            UnitCell::from_ptr(handle)
        }
    }

    /// Create an `Infinite` `UnitCell`.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::{UnitCell, CellShape};
    /// let cell = UnitCell::infinite().unwrap();
    ///
    /// assert_eq!(cell.lengths(), Ok((0.0, 0.0, 0.0)));
    /// assert_eq!(cell.angles(), Ok((90.0, 90.0, 90.0)));
    /// assert_eq!(cell.shape(), Ok(CellShape::Infinite));
    /// ```
    pub fn infinite() -> Result<UnitCell> {
        let mut cell = try!(UnitCell::new(0.0, 0.0, 0.0));
        try!(cell.set_shape(CellShape::Infinite));
        Ok(cell)
    }

    /// Create an `Triclinic` `UnitCell` from the three lengths (in Angstroms)
    /// and three angles (in degree). `alpha` is the angle between the vectors
    /// `b` and `c`; `beta` is the between the vectors `a` and `c` and `gamma`
    /// is the angle between the vectors `a` and `b`.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::{UnitCell, CellShape};
    /// let cell = UnitCell::triclinic(10.0, 10.0, 10.0, 98.0, 99.0, 90.0).unwrap();
    ///
    /// assert_eq!(cell.lengths(), Ok((10.0, 10.0, 10.0)));
    /// assert_eq!(cell.angles(), Ok((98.0, 99.0, 90.0)));
    /// assert_eq!(cell.shape(), Ok(CellShape::Triclinic));
    /// ```
    pub fn triclinic(a: f64, b: f64, c: f64, alpha: f64, beta: f64, gamma: f64) -> Result<UnitCell> {
        let lengths = [a, b, c];
        let angles = [alpha, beta, gamma];
        unsafe {
            let handle = chfl_cell_triclinic(lengths.as_ptr(), angles.as_ptr());
            UnitCell::from_ptr(handle)
        }
    }

    /// Get the three lengths of the cell, in Angstroms.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::UnitCell;
    /// let cell = UnitCell::new(30.0, 30.0, 23.0).unwrap();
    /// assert_eq!(cell.lengths(), Ok((30.0, 30.0, 23.0)));
    /// ```
    pub fn lengths(&self) -> Result<(f64, f64, f64)> {
        let mut lengths = [0.0_f64; 3];
        unsafe {
            try!(check(chfl_cell_lengths(self.as_ptr(), lengths.as_mut_ptr())));
        }
        Ok((lengths[0], lengths[1], lengths[2]))
    }

    /// Set the three lengths of the cell, in Angstroms.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::UnitCell;
    /// let mut cell = UnitCell::new(30.0, 30.0, 23.0).unwrap();
    ///
    /// cell.set_lengths(10.0, 30.0, 42.0).unwrap();
    /// assert_eq!(cell.lengths(), Ok((10.0, 30.0, 42.0)));
    /// ```
    pub fn set_lengths(&mut self, a: f64, b: f64, c: f64) -> Result<()> {
        let lengths = [a, b, c];
        unsafe {
            try!(check(chfl_cell_set_lengths(self.as_mut_ptr(), lengths.as_ptr())));
        }
        Ok(())
    }

    /// Get the three angles of the cell, in degrees.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::UnitCell;
    /// let cell = UnitCell::new(20.0, 20.0, 20.0).unwrap();
    /// assert_eq!(cell.angles(), Ok((90.0, 90.0, 90.0)));
    ///
    /// let cell = UnitCell::triclinic(20.0, 20.0, 20.0, 100.0, 120.0, 90.0).unwrap();
    /// assert_eq!(cell.angles(), Ok((100.0, 120.0, 90.0)));
    /// ```
    pub fn angles(&self) -> Result<(f64, f64, f64)> {
        let mut angles = [0.0_f64; 3];
        unsafe {
            try!(check(chfl_cell_angles(self.as_ptr(), angles.as_mut_ptr())));
        }
        Ok((angles[0], angles[1], angles[2]))
    }

    /// Set the three angles of the cell, in degrees. This is only possible
    /// with `Triclinic` cells.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::UnitCell;
    /// let mut cell = UnitCell::triclinic(20.0, 20.0, 20.0, 100.0, 120.0, 90.0).unwrap();
    /// assert_eq!(cell.angles(), Ok((100.0, 120.0, 90.0)));
    ///
    /// cell.set_angles(90.0, 90.0, 90.0).unwrap();
    /// assert_eq!(cell.angles(), Ok((90.0, 90.0, 90.0)));
    /// ```
    pub fn set_angles(&mut self, alpha: f64, beta: f64, gamma: f64) -> Result<()> {
        let angles = [alpha, beta, gamma];
        unsafe {
            try!(check(chfl_cell_set_angles(self.as_mut_ptr(), angles.as_ptr())));
        }
        Ok(())
    }

    /// Get the unit cell matricial representation.
    ///
    /// The unit cell representation is obtained by aligning the a vector along
    /// the *x* axis and putting the b vector in the *xy* plane. This make the
    /// matrix an upper triangular matrix:
    ///
    /// ```text
    /// | a_x   b_x   c_x |
    /// |  0    b_y   c_y |
    /// |  0     0    c_z |
    /// ```
    ///
    /// # Example
    /// ```
    /// # use chemfiles::UnitCell;
    /// let mut cell = UnitCell::new(10.0, 20.0, 30.0).unwrap();
    ///
    /// let matrix = cell.matrix().unwrap();
    ///
    /// assert_eq!(matrix[0][0], 10.0);
    /// assert_eq!(matrix[1][1], 20.0);
    /// assert_eq!(matrix[2][2], 30.0);
    ///
    /// assert!(matrix[1][2].abs() < 1e-9);
    /// ```
    pub fn matrix(&self) -> Result<[[f64; 3]; 3]> {
        let mut res = [[0.0; 3]; 3];
        unsafe {
            try!(check(chfl_cell_matrix(self.as_ptr(), res.as_mut_ptr())));
        }
        Ok(res)
    }

    /// Get the shape of the unit cell.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::{UnitCell, CellShape};
    /// let cell = UnitCell::new(10.0, 20.0, 30.0).unwrap();
    /// assert_eq!(cell.shape(), Ok(CellShape::Orthorhombic));
    /// ```
    pub fn shape(&self) -> Result<CellShape> {
        let mut shape = chfl_cell_shape_t::CHFL_CELL_INFINITE;
        unsafe {
            try!(check(chfl_cell_shape(self.as_ptr(), &mut shape)));
        }
        Ok(CellShape::from(shape))
    }

    /// Set the shape of the unit cell to `shape`.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::{UnitCell, CellShape};
    /// let mut cell = UnitCell::new(10.0, 20.0, 30.0).unwrap();
    /// assert_eq!(cell.shape(), Ok(CellShape::Orthorhombic));
    ///
    /// cell.set_shape(CellShape::Infinite).unwrap();
    /// assert_eq!(cell.shape(), Ok(CellShape::Infinite));
    /// ```
    pub fn set_shape(&mut self, shape: CellShape) -> Result<()> {
        unsafe {
            try!(check(chfl_cell_set_shape(self.as_mut_ptr(), shape.into())));
        }
        Ok(())
    }

    /// Get the volume of the unit cell.
    ///
    /// # Example
    /// ```
    /// # use chemfiles::UnitCell;
    /// let cell = UnitCell::new(10.0, 20.0, 30.0).unwrap();
    /// assert_eq!(cell.volume(), Ok(10.0 * 20.0 * 30.0));
    /// ```
    pub fn volume(&self) -> Result<f64> {
        let mut res = 0.0;
        unsafe {
            try!(check(chfl_cell_volume(self.as_ptr(), &mut res)));
        }
        Ok(res)
    }
}

impl Drop for UnitCell {
    fn drop(&mut self) {
        unsafe {
            let status = chfl_cell_free(self.as_mut_ptr());
            debug_assert_eq!(status, chfl_status::CHFL_SUCCESS);
        }
    }
}


#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn clone() {
        let mut cell = UnitCell::new(2.0, 3.0, 4.0).unwrap();
        assert_eq!(cell.lengths(), Ok((2.0, 3.0, 4.0)));

        let copy = cell.clone();
        assert_eq!(copy.lengths(), Ok((2.0, 3.0, 4.0)));

        assert!(cell.set_lengths(10.0, 12.0, 11.0).is_ok());
        assert_eq!(cell.lengths(), Ok((10.0, 12.0, 11.0)));
        assert_eq!(copy.lengths(), Ok((2.0, 3.0, 4.0)));
    }

    #[test]
    fn lengths() {
        let mut cell = UnitCell::new(2.0, 3.0, 4.0).unwrap();

        assert_eq!(cell.lengths(), Ok((2.0, 3.0, 4.0)));

        assert!(cell.set_lengths(10.0, 12.0, 11.0).is_ok());
        assert_eq!(cell.lengths(), Ok((10.0, 12.0, 11.0)));
    }

    #[test]
    fn angles() {
        let mut cell = UnitCell::new(2.0, 3.0, 4.0).unwrap();

        assert_eq!(cell.angles(), Ok((90.0, 90.0, 90.0)));

        assert!(cell.set_shape(CellShape::Triclinic).is_ok());
        assert!(cell.set_angles(80.0, 89.0, 100.0).is_ok());

        assert_eq!(cell.angles(), Ok((80.0, 89.0, 100.0)));

        let cell = UnitCell::triclinic(1., 2., 3., 80., 90., 100.).unwrap();
        assert_eq!(cell.angles(), Ok((80.0, 90.0, 100.0)));
    }

    #[test]
    fn volume() {
        let cell = UnitCell::new(2.0, 3.0, 4.0).unwrap();

        assert_eq!(cell.volume(), Ok(2.0 * 3.0 * 4.0));
    }

    #[test]
    fn matrix() {
        let cell = UnitCell::new(2.0, 3.0, 4.0).unwrap();

        let matrix = cell.matrix().unwrap();
        let result = [[2.0, 0.0, 0.0], [0.0, 3.0, 0.0], [0.0, 0.0, 4.0]];

        for i in 0..3 {
            for j in 0..3 {
                assert_ulps_eq!(matrix[i][j], result[i][j], epsilon=1e-12);
            }
        }
    }

    #[test]
    fn shape() {
        let mut cell = UnitCell::new(2.0, 3.0, 4.0).unwrap();
        assert_eq!(cell.shape(), Ok(CellShape::Orthorhombic));

        assert!(cell.set_shape(CellShape::Infinite).is_ok());
        assert_eq!(cell.shape(), Ok(CellShape::Infinite));

        let cell = UnitCell::infinite().unwrap();
        assert_eq!(cell.shape(), Ok(CellShape::Infinite));

        let cell = UnitCell::triclinic(1., 2., 3., 80., 90., 100.).unwrap();
        assert_eq!(cell.shape(), Ok(CellShape::Triclinic));
    }
}