oILAB
Loading...
Searching...
No Matches
LatticeCore.cpp
Go to the documentation of this file.
1/* This file is part of gbLAB.
2 *
3 * gbLAB is distributed without any warranty under the MIT License.
4 */
5
6
7#ifndef gbLAB_LatticeCore_cpp_
8#define gbLAB_LatticeCore_cpp_
9
10#include "../../include/Lattices/LatticeCore.h"
11#include "../../include/Math/BestRationalApproximation.h"
12#include <Eigen/Dense>
13
14namespace oILAB {
15
16template <int dim>
18{
19 Eigen::Array<IntScalarType, dim, 1> nums = VectorDimI::Zero();
20
21 if (nd.squaredNorm() > 0.0)
22 {
23 const Eigen::Array<double, dim, 1> nda(nd.array().abs()); // vector of close-to-integer numbers corresponding to lattice coordinates
24 size_t maxID = 0;
25 const double maxVal(nda.maxCoeff(&maxID));
26 nd /= maxVal; // make each value of nd in [-1:1]
27
28 nums = VectorDimI::Ones();
29 Eigen::Array<IntScalarType, dim, 1> dens = VectorDimI::Ones();
30 IntScalarType denProd = 1;
31
32 for (int k = 0; k < dim; ++k)
33 {
34 //BestRationalApproximation bra(nd(k), 10000);
35 BestRationalApproximation bra(nd(k), 1000);
36
37 nums(k) = bra.num;
38 dens(k) = bra.den;
39 denProd *= bra.den;
40 }
41
42 for (int k = 0; k < dim; ++k)
43 {
44 nums(k) *= (denProd / dens(k));
45 }
46 }
47
48 return nums.matrix();
49}
50
51template <int dim>
53{
54 const VectorDimD nd(invA*d);
55 const VectorDimD rd(nd.array().round());
56 if ((nd - rd).norm() > roundTol)
57 {
58 std::cout << "nd=" << nd.transpose() << std::endl;
59 std::cout << "rd=" << rd.transpose() << std::endl;
60 std::cout << "rounding error = |nd-rd| = " << (nd-rd).norm() << std::endl;
61 throw(std::runtime_error("Input vector is not a lattice vector"));
62 }
63 return rd.template cast<IntScalarType>();
64}
65
66
67
68 template struct LatticeCore<1>;
69 template struct LatticeCore<2>;
70 template struct LatticeCore<3>;
71 template struct LatticeCore<4>;
72 template struct LatticeCore<5>;
73 } // namespace oILAB
74#endif
Eigen::Matrix< IntScalarType, dim, 1 > VectorDimI
Definition LatticeCore.h:23
long long int IntScalarType
Definition LatticeCore.h:22
Eigen::Matrix< double, dim, 1 > VectorDimD
Definition LatticeCore.h:20
static VectorDimI rationalApproximation(VectorDimD v)
Approximates a direction in terms of integer coordinates.
Eigen::Matrix< double, dim, dim > MatrixDimD
Definition LatticeCore.h:21
static VectorDimI integerCoordinates(const VectorDimD &d, const MatrixDimD &invA)
Returns the integer coordinates of a vector with respect to a lattices with structure matrix .