FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
libdiffpy/src/diffpy/srreal/AtomicStructureAdapter.cpp at develop · cfarrow/libdiffpy · GitHub
cfarrow
libdiffpy
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
libdiffpy
/
src
/
diffpy
/
srreal
/
AtomicStructureAdapter.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
276 lines (230 loc) · 8.25 KB
Breadcrumbs
libdiffpy
/
src
/
diffpy
/
srreal
/
AtomicStructureAdapter.cpp
Copy path
File metadata and controls
276 lines (230 loc) · 8.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
****************************************************************************
*
* diffpy.srreal by DANSE Diffraction group
* Simon J. L. Billinge
* (c) 2012 Trustees of the Columbia University
* in the City of New York. All rights reserved.
*
* File coded by: Pavol Juhas
*
* See AUTHORS.txt for a list of people who contributed.
* See LICENSE.txt for license information.
*
******************************************************************************
*
* class AtomicStructureAdapter -- universal structure adapter for
* a non-periodic set of atoms.
*
****************************************************************************
*/
#
include
<
cassert
>
#
include
<
algorithm
>
#
include
<
boost/functional/hash.hpp
>
#
include
<
diffpy/serialization.ipp
>
#
include
<
diffpy/srreal/AtomicStructureAdapter.hpp
>
#
include
<
diffpy/srreal/StructureDifference.hpp
>
using
std::string;
namespace
diffpy
{
namespace
srreal
{
//
////////////////////////////////////////////////////////////////////////////
//
class Atom
//
////////////////////////////////////////////////////////////////////////////
bool
operator
< (
const
Atom& a0,
const
Atom& a1)
{
if
(a0.
atomtype
< a1.
atomtype
)
return
true
;
if
(a0.
atomtype
> a1.
atomtype
)
return
false
;
R3
::Vector::const_iterator p0 = a0.
cartesianposition
.
begin
();
R3
::Vector::const_iterator p1 = a1.
cartesianposition
.
begin
();
for
(; p0 != a0.
cartesianposition
.
end
(); ++p0, ++p1)
{
if
(*p0 < *p1)
return
true
;
if
(*p0 > *p1)
return
false
;
}
if
(a0.
occupancy
< a1.
occupancy
)
return
true
;
if
(a0.
occupancy
> a1.
occupancy
)
return
false
;
if
(a0.
anisotropy
< a1.
anisotropy
)
return
true
;
if
(a0.
anisotropy
> a1.
anisotropy
)
return
false
;
typedef
R3
::Matrix::array_type::const_iterator CIter;
CIter u0 = a0.
cartesianuij
.
data
().
begin
();
CIter u0last = a0.
cartesianuij
.
data
().
end
();
CIter u1 = a1.
cartesianuij
.
data
().
begin
();
for
(; u0 != u0last; ++u0, ++u1)
{
if
(*u0 < *u1)
return
true
;
if
(*u0 > *u1)
return
false
;
}
return
false
;
}
bool
operator
==(
const
Atom& a0,
const
Atom& a1)
{
bool
rv = (&a0 == &a1) || (
a0.
atomtype
== a1.
atomtype
&&
(a0.
cartesianposition
== a1.
cartesianposition
) &&
a0.
occupancy
== a1.
occupancy
&&
a0.
anisotropy
== a1.
anisotropy
&&
a0.
cartesianuij
== a1.
cartesianuij
);
return
rv;
}
bool
operator
!=(
const
Atom& a0,
const
Atom& a1)
{
return
!(a0 == a1);
}
size_t
hash_value
(
const
Atom& a)
{
size_t
seed =
0
;
boost::hash_combine
(seed, a.
atomtype
);
boost::hash_combine
(seed, a.
cartesianposition
);
boost::hash_combine
(seed, a.
occupancy
);
boost::hash_combine
(seed, a.
anisotropy
);
boost::hash_combine
(seed, a.
cartesianuij
);
return
seed;
}
//
////////////////////////////////////////////////////////////////////////////
//
class AtomicStructureAdapter
//
////////////////////////////////////////////////////////////////////////////
//
Public Methods ------------------------------------------------------------
BaseBondGeneratorPtr
AtomicStructureAdapter::createBondGenerator
()
const
{
BaseBondGeneratorPtr
bnds
(
new
BaseBondGenerator
(
shared_from_this
()));
return
bnds;
}
int
AtomicStructureAdapter::countSites
()
const
{
return
matoms.
size
();
}
const
string&
AtomicStructureAdapter::siteAtomType
(
int
idx)
const
{
assert
(
0
<= idx && idx <
this
->
countSites
());
return
matoms[idx].
atomtype
;
}
const
R3
::Vector&
AtomicStructureAdapter::siteCartesianPosition
(
int
idx)
const
{
assert
(
0
<= idx && idx <
this
->
countSites
());
return
matoms[idx].
cartesianposition
;
}
double
AtomicStructureAdapter::siteOccupancy
(
int
idx)
const
{
assert
(
0
<= idx && idx <
this
->
countSites
());
return
matoms[idx].
occupancy
;
}
bool
AtomicStructureAdapter::siteAnisotropy
(
int
idx)
const
{
assert
(
0
<= idx && idx <
this
->
countSites
());
return
matoms[idx].
anisotropy
;
}
const
R3
::Matrix&
AtomicStructureAdapter::siteCartesianUij
(
int
idx)
const
{
assert
(
0
<= idx && idx <
this
->
countSites
());
return
matoms[idx].
cartesianuij
;
}
//
helper for diff
namespace
{
typedef
std::pair<
const
Atom*,
int
> atomindex;
bool
cmpatomindex
(
const
atomindex& ai0,
const
atomindex& ai1)
{
return
(*(ai0.
first
) < *(ai1.
first
));
}
}
//
namespace
StructureDifference
AtomicStructureAdapter::diff
(StructureAdapterConstPtr other)
const
{
using
std::min;
typedef
boost::shared_ptr<
const
class
AtomicStructureAdapter
> Ptr;
StructureDifference
sd
(
this
->
shared_from_this
(), other);
if
(sd.
stru0
== sd.
stru1
)
return
sd;
Ptr pstru0 = boost::static_pointer_cast<Ptr::element_type>(sd.
stru0
);
Ptr pstru1 = boost::dynamic_pointer_cast<Ptr::element_type>(sd.
stru1
);
if
(!pstru1)
return
this
->
StructureAdapter
::diff
(other);
assert
(pstru0);
assert
(pstru1);
const
AtomicStructureAdapter& astru0 = *pstru0;
const
AtomicStructureAdapter& astru1 = *pstru1;
sd.
pop0
.
reserve
(astru0.
countSites
());
sd.
add1
.
reserve
(astru1.
countSites
());
//
try fast side-by-side comparison for equal length structures
if
(astru0.
countSites
() == astru1.
countSites
())
{
sd.
diffmethod
= StructureDifference::Method::
SIDEBYSIDE
;
const_iterator ai0 = astru0.
matoms
.
begin
();
const_iterator ai1 = astru1.
matoms
.
begin
();
for
(
int
i =
0
; ai0 != astru0.
matoms
.
end
(); ++i, ++ai0, ++ai1)
{
if
(*ai0 != *ai1)
{
sd.
pop0
.
push_back
(i);
sd.
add1
.
push_back
(i);
}
if
(!sd.
allowsfastupdate
())
break
;
}
if
(sd.
allowsfastupdate
())
return
sd;
}
//
here the structures are either of different length or differ too much
//
when compared side by side. Let's start from a blank slate.
sd.
pop0
.
clear
();
sd.
add1
.
clear
();
//
let's build sorted vectors of atoms in stru0 and stru1
sd.
diffmethod
= StructureDifference::Method::
SORTED
;
std::vector<atomindex> satoms0, satoms1;
satoms0.
reserve
(astru0.
countSites
());
const_iterator ai = astru0.
matoms
.
begin
();
for
(
int
i =
0
; ai != astru0.
matoms
.
end
(); ++ai, ++i)
{
satoms0.
push_back
(
atomindex
(&(*ai), i));
}
//
use negative index for stru1 atoms so we can tell them apart
//
in the output of set_symmetric_difference
satoms1.
reserve
(astru1.
countSites
());
ai = astru1.
matoms
.
begin
();
for
(
int
i = -
1
; ai != astru1.
matoms
.
end
(); ++ai, --i)
{
satoms1.
push_back
(
atomindex
(&(*ai), i));
}
sort
(satoms0.
begin
(), satoms0.
end
(), cmpatomindex);
sort
(satoms1.
begin
(), satoms1.
end
(), cmpatomindex);
std::vector<atomindex>
symdiffatoms
(satoms0.
size
() + satoms1.
size
());
std::vector<atomindex>::iterator ii;
ii =
std::set_symmetric_difference
(satoms0.
begin
(), satoms0.
end
(),
satoms1.
begin
(), satoms1.
end
(),
symdiffatoms.
begin
(), cmpatomindex);
symdiffatoms.
erase
(ii, symdiffatoms.
end
());
for
(ii = symdiffatoms.
begin
(); ii != symdiffatoms.
end
(); ++ii)
{
if
(ii->
second
>=
0
) sd.
pop0
.
push_back
(ii->
second
);
else
sd.
add1
.
push_back
(-
1
* ii->
second
-
1
);
}
assert
(sd.
pop0
.
size
() <=
min
(satoms0.
size
(), symdiffatoms.
size
()));
assert
(sd.
add1
.
size
() <=
min
(satoms1.
size
(), symdiffatoms.
size
()));
sort
(sd.
pop0
.
begin
(), sd.
pop0
.
end
());
sort
(sd.
add1
.
begin
(), sd.
add1
.
end
());
return
sd;
}
void
AtomicStructureAdapter::insert
(
int
idx,
const
Atom& atom)
{
assert
(
0
<= idx && idx <=
this
->
countSites
());
matoms.
insert
(matoms.
begin
() + idx, atom);
}
void
AtomicStructureAdapter::append
(
const
Atom& atom)
{
matoms.
push_back
(atom);
}
void
AtomicStructureAdapter::remove
(
int
idx)
{
assert
(
0
<= idx && idx <
this
->
countSites
());
matoms.
erase
(matoms.
begin
() + idx);
}
Atom& AtomicStructureAdapter::
operator
[](
int
idx)
{
assert
(
0
<= idx && idx <
this
->
countSites
());
return
matoms[idx];
}
const
Atom& AtomicStructureAdapter::
operator
[](
int
idx)
const
{
assert
(
0
<= idx && idx <
this
->
countSites
());
return
matoms[idx];
}
}
//
namespace srreal
}
//
namespace diffpy
//
Serialization -------------------------------------------------------------
DIFFPY_INSTANTIATE_SERIALIZATION
(diffpy::srreal::AtomicStructureAdapter)
BOOST_CLASS_EXPORT_IMPLEMENT
(diffpy::srreal::AtomicStructureAdapter)
//
End of file
Back
|
FazBrowse Home
|
New Git URL