PSCF v1.1
shiftToMinimum.cpp
1/*
2* PSCF - Polymer Self-Consistent Field Theory
3*
4* Copyright 2016 - 2022, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "shiftToMinimum.h"
9#include <util/global.h>
10
11namespace Pscf
12{
13
14 using namespace Util;
15
16 template <>
17 IntVec<1> shiftToMinimum(IntVec<1>& v, IntVec<1> d, UnitCell<1> const & cell)
18 {
19 IntVec<1> u;
20 if (v[0] > d[0]/2) {
21 u[0] = v[0] - d[0];
22 } else
23 if (v[0] < -(d[0]/2) ) {
24 u[0] = v[0] + d[0];
25 } else {
26 u[0] = v[0];
27 }
28 UTIL_ASSERT(abs(u[0]) <= d[0]/2);
29 return u;
30 }
31
32 template <>
33 IntVec<2> shiftToMinimum(IntVec<2>& v, IntVec<2> d, UnitCell<2> const & cell)
34 {
35 // Initialize minimum to input value
36 const double epsilon = 1.0E-6;
37 double Gsq = cell.ksq(v);
38 double Gsq_min_lo = Gsq - epsilon;
39 double Gsq_min_hi = Gsq + epsilon;
40
41 // Loop over neighboring images
42 IntVec<2> r = v; // Minimum image of v
43 IntVec<2> u; // Vector used in loop
44 int s0, s1;
45 const int q = 2;
46 for (s0 = -q; s0 < q+1; ++s0) {
47 u[0] = v[0] + s0*d[0];
48 for (s1 = -q; s1 < q+1; ++s1) {
49 u[1] = v[1] + s1*d[1];
50 Gsq = cell.ksq(u);
51 if (Gsq < Gsq_min_hi) {
52 if ((Gsq < Gsq_min_lo) || (r < u)) {
53 Gsq_min_lo = Gsq - epsilon;
54 Gsq_min_hi = Gsq + epsilon;
55 r = u;
56 }
57 }
58 }
59 }
60 return r;
61 }
62
63 template <>
64 IntVec<3> shiftToMinimum(IntVec<3>& v, IntVec<3> d, UnitCell<3> const & cell)
65 {
66 // Initialize minimum to input value
67 const double epsilon = 1.0E-6;
68 double Gsq = cell.ksq(v);
69 double Gsq_min_lo = Gsq - epsilon;
70 double Gsq_min_hi = Gsq + epsilon;
71
72 // Loop over neighboring images
73 IntVec<3> r = v; // Minimum image
74 IntVec<3> u; // Vector used in loop
75 int s0, s1, s2;
76 const int q = 2;
77 for (s0 = -q; s0 < q + 1; ++s0) {
78 u[0] = v[0] + s0*d[0];
79 for (s1 = -q; s1 < q + 1; ++s1) {
80 u[1] = v[1] + s1*d[1];
81 for (s2 = -q; s2 < q + 1; ++s2) {
82 u[2] = v[2] + s2*d[2];
83 Gsq = cell.ksq(u);
84 if (Gsq < Gsq_min_hi) {
85 if ((Gsq < Gsq_min_lo) || (r < u)) {
86 Gsq_min_lo = Gsq - epsilon;
87 Gsq_min_hi = Gsq + epsilon;
88 r = u;
89 }
90 }
91 }
92 }
93 }
94 return r;
95 }
96
97}
File containing preprocessor macros for error handling.
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition: global.h:75
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1