PSCF v1.5.0
SpaceGroup.tpp
1#ifndef PSCF_SPACE_GROUP_TPP
2#define PSCF_SPACE_GROUP_TPP
3
4/*
5* Simpatico - Simulation Package for Polymeric and Molecular Liquids
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <pscf/crystal/SpaceGroup.h>
12#include <pscf/crystal/groupFile.h>
13
14namespace Pscf {
15
16 using namespace Util;
17
18 /*
19 * Find inversion center, if any.
20 */
21 template <int D>
22 bool
24 typename SpaceSymmetry<D>::Translation& center)
25 const
26 {
27 bool isInversion = false;
28 int i, j, k;
29 for (i = 0; i < size(); ++i) {
30 isInversion = true;
31 for (j = 0; j < D; ++j) {
32 for (k = 0; k < D; ++k) {
33 if (j == k) {
34 if ((*this)[i].R(j,k) != -1) isInversion = false;
35 } else {
36 if ((*this)[i].R(j,k) != 0) isInversion = false;
37 }
38 }
39 }
40 if (isInversion) {
41 for (int j = 0; j < D; ++j) {
42 center[j] = (*this)[i].t(j)/2;
43 }
44 return true;
45 }
46 }
47 return false;
48 }
49
50 template <int D>
52 typename SpaceSymmetry<D>::Translation const & origin)
53 {
54 for (int i = 0; i < size(); ++i) {
55 (*this)[i].shiftOrigin(origin);
56 }
57 }
58
59 /*
60 * Check if a mesh is compatible with this space group.
61 */
62 template <int D>
64 const
65 {
66
67 // ---------------------------------------------------------------
68 // Check compatibility of mesh dimensions & translations
69 // ---------------------------------------------------------------
70
71 // Identify required divisor of mesh dimension in each direction
72 int numerator, denominator;
73 IntVec<D> divisors;
74 for (int i = 0; i < D; ++i) {
75 // Find maximum denominator
76 divisors[i] = 1;
77 for (int j = 0; j < size(); ++j) {
78 numerator = (*this)[j].t(i).num();
79 denominator = (*this)[j].t(i).den();
80 if (numerator != 0) {
81 UTIL_CHECK(denominator > 0);
82 if (denominator > divisors[i]) {
83 divisors[i] = denominator;
84 }
85 }
86 }
87 // Make sure each divisor is a multiple of all denominators
88 for (int j = 0; j < size(); ++j) {
89 numerator = (*this)[j].t(i).num();
90 denominator = (*this)[j].t(i).den();
91 if (numerator != 0) {
92 if (denominator < divisors[i]) {
93 if (divisors[i]%denominator != 0) {
94 divisors[i] = divisors[i]*denominator;
95 }
96 }
97 }
98 }
99 }
100
101 // Check that mesh dimensions are multiples of required divisor
102 for (int i = 0; i < D; ++i) {
103 if (dimensions[i]%divisors[i] != 0) {
104 Log::file()
105 << "\n"
106 << "Mesh dimensions incompatible with the space group:\n"
107 << " dimensions[" << i << "] = " << dimensions[i] << "\n"
108 << " This dimension must be a multiple of " << divisors[i]
109 << "\n\n";
110 UTIL_THROW("Error: Mesh not incompatible with space group");
111 }
112 }
113
114 // ---------------------------------------------------------------
115 // Check compatibility of mesh dimensions & point group operations
116 // ---------------------------------------------------------------
117
118 // Identify pairs of directions that are related by point group ops
119 FMatrix<bool, D, D> areRelated;
120 for (int i = 0; i < D; ++i) {
121 for (int j = 0; j < D; ++j) {
122 areRelated(i, j) = false;
123 }
124 areRelated(i, i) = true;
125 }
126 for (int k = 0; k < size(); ++k) {
127 for (int i = 0; i < D; ++i) {
128 for (int j = 0; j < D; ++j) {
129 if (i != j) {
130 if ( (*this)[k].R(i,j) != 0) {
131 areRelated(i, j) = true;
132 areRelated(j, i) = true;
133 }
134 }
135 }
136 }
137 }
138
139 // Check if mesh dimensions are equal for related directions
140 for (int i = 0; i < D; ++i) {
141 for (int j = 0; j < i; ++j) {
142 if (areRelated(i,j) && (dimensions[i] != dimensions[j])) {
143 Log::file()
144 << "\n"
145 << "Mesh dimensions incompatible with the space group - \n"
146 << "Unequal dimensions for related directions:\n"
147 << " dimensions[" << i << "] = " << dimensions[i] << "\n"
148 << " dimensions[" << j << "] = " << dimensions[j]
149 << "\n\n";
150 UTIL_THROW("Error: Mesh not incompatible with space group");
151 }
152 }
153 }
154
155 }
156
157 /*
158 * Read a group from file
159 */
160 template <int D>
161 void readGroup(std::string groupName, SpaceGroup<D>& group)
162 {
163 if (groupName == "I") {
164 // Replace any previous group by the identity group
165 group.clear();
166 } else {
167 bool foundFile = false;
168 {
169 // Search first in this directory
170 std::ifstream in;
171 in.open(groupName);
172 if (in.is_open()) {
173 in >> group;
174 UTIL_CHECK(group.isValid());
175 foundFile = true;
176 }
177 }
178 if (!foundFile) {
179 // Search in the data directory containing standard space groups
180 std::string fileName = makeGroupFileName(D, groupName);
181 std::ifstream in;
182 in.open(fileName);
183 if (in.is_open()) {
184 in >> group;
185 UTIL_CHECK(group.isValid());
186 } else {
187 Log::file() << "\nFailed to open group file: "
188 << fileName << "\n";
189 Log::file() << "\n Error: Unknown space group\n";
190 UTIL_THROW("Unknown space group");
191 }
192 }
193 }
194 }
195
196 /*
197 * Open an output file and write group to file.
198 */
199 template <int D>
200 void writeGroup(std::string filename, SpaceGroup<D> const & group)
201 {
202 std::ofstream out;
203 out.open(filename);
204 out << group;
205 }
206
207}
208#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Crystallographic space group.
Definition SpaceGroup.h:31
bool hasInversionCenter(typename SpaceSymmetry< D >::Translation &center) const
Determines if this space group has an inversion center.
void checkMeshDimensions(IntVec< D > const &dimensions) const
Check if input mesh dimensions are compatible with space group.
void shiftOrigin(typename SpaceSymmetry< D >::Translation const &origin)
Shift the origin of space used in the coordinate system.
FArray< Rational, D > Translation
Typedef for vector used to represent fractional translation.
void clear()
Remove all elements except the identity.
Fixed Size Matrix.
Definition FMatrix.h:29
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
std::string makeGroupFileName(int D, std::string groupName)
Generates the file name from a group name.
Definition groupFile.cpp:25
PSCF package top-level namespace.