PSCF v1.3
shiftToMinimum.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, 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 {
12namespace Prdc {
13
14 using namespace Util;
15
16 template <>
17 IntVec<1> shiftToMinimum(IntVec<1> const & v,
18 IntVec<1> const & d,
19 UnitCell<1> const & cell)
20 {
21 IntVec<1> u;
22 if (v[0] > d[0]/2) {
23 u[0] = v[0] - d[0];
24 } else
25 if (v[0] < -(d[0]/2) ) {
26 u[0] = v[0] + d[0];
27 } else {
28 u[0] = v[0];
29 }
30 UTIL_ASSERT(abs(u[0]) <= d[0]/2);
31 return u;
32 }
33
34 template <>
35 IntVec<2> shiftToMinimum(IntVec<2> const & v,
36 IntVec<2> const & d,
37 UnitCell<2> const & cell)
38 {
39 // Initialize minimum to input value
40 const double epsilon = 1.0E-8;
41 double Gsq = cell.ksq(v);
42 double Gsq_min_lo = Gsq - epsilon;
43 double Gsq_min_hi = Gsq + epsilon;
44
45 // Loop over neighboring images
46 IntVec<2> r = v; // Minimum image of v
47 IntVec<2> u; // Vector used in loop
48 int s0, s1;
49 const int q = 2;
50 for (s0 = -q; s0 < q+1; ++s0) {
51 u[0] = v[0] + s0*d[0];
52 for (s1 = -q; s1 < q+1; ++s1) {
53 u[1] = v[1] + s1*d[1];
54 Gsq = cell.ksq(u);
55 if (Gsq < Gsq_min_hi) {
56 if ((Gsq < Gsq_min_lo) || (r < u)) {
57 Gsq_min_lo = Gsq - epsilon;
58 Gsq_min_hi = Gsq + epsilon;
59 r = u;
60 }
61 }
62 }
63 }
64 return r;
65 }
66
67 template <>
68 IntVec<3> shiftToMinimum(IntVec<3> const & v,
69 IntVec<3> const & d,
70 UnitCell<3> const & cell)
71 {
72 // Initialize minimum to input value
73 const double epsilon = 1.0E-8;
74 double Gsq = cell.ksq(v);
75 double Gsq_min_lo = Gsq - epsilon;
76 double Gsq_min_hi = Gsq + epsilon;
77
78 // Loop over neighboring images
79 IntVec<3> r = v; // Minimum image
80 IntVec<3> u; // Vector used in loop
81 int s0, s1, s2;
82 const int q = 2;
83 for (s0 = -q; s0 < q + 1; ++s0) {
84 u[0] = v[0] + s0*d[0];
85 for (s1 = -q; s1 < q + 1; ++s1) {
86 u[1] = v[1] + s1*d[1];
87 for (s2 = -q; s2 < q + 1; ++s2) {
88 u[2] = v[2] + s2*d[2];
89 Gsq = cell.ksq(u);
90 if (Gsq < Gsq_min_hi) {
91 if ((Gsq < Gsq_min_lo) || (r < u)) {
92 Gsq_min_lo = Gsq - epsilon;
93 Gsq_min_hi = Gsq + epsilon;
94 r = u;
95 }
96 }
97 }
98 }
99 }
100 return r;
101 }
102
103}
104}
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:56
File containing preprocessor macros for error handling.
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
RT abs(CT const &a)
Return absolute magnitude of a complex number.
Periodic fields and crystallography.
Definition CField.cpp:11
PSCF package top-level namespace.
Definition param_pc.dox:1