FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
diffpy.srreal/src/extensions/wrap_ScatteringFactorTable.cpp at main · diffpy/diffpy.srreal · GitHub
diffpy
diffpy.srreal
Repository navigation
Code
Issues
13
(13)
Pull requests
5
(5)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
diffpy.srreal
/
src
/
extensions
/
wrap_ScatteringFactorTable.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
458 lines (378 loc) · 15.2 KB
Breadcrumbs
diffpy.srreal
/
src
/
extensions
/
wrap_ScatteringFactorTable.cpp
Copy path
File metadata and controls
458 lines (378 loc) · 15.2 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
****************************************************************************
*
* diffpy.srreal by DANSE Diffraction group
* Simon J. L. Billinge
* (c) 2010 The Trustees of 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_DANSE.txt for license information.
*
******************************************************************************
*
* Bindings to the ScatteringFactorTable class. The business methods can be
* overridden from Python to return custom scattering factor values.
*
****************************************************************************
*/
#
include
<
nanobind/nanobind.h
>
#
include
"
srreal_trampoline.hpp
"
#
include
<
diffpy/srreal/ScatteringFactorTable.hpp
>
#
include
<
diffpy/srreal/SFTXray.hpp
>
#
include
<
diffpy/srreal/SFTElectron.hpp
>
#
include
<
diffpy/srreal/SFTNeutron.hpp
>
#
include
<
diffpy/srreal/SFTElectronNumber.hpp
>
#
include
"
srreal_converters.hpp
"
#
include
"
srreal_pickling.hpp
"
#
include
"
srreal_legacy.hpp
"
#
include
"
srreal_registry.hpp
"
namespace
nb
=
nanobind;
namespace
srrealmodule
{
namespace
nswrap_ScatteringFactorTable
{
using
namespace
diffpy
::srreal
;
//
docstrings
const
char
* doc_ScatteringFactorTable =
"
\
Base class for looking up scattering factors by atom symbols.
\n
\
This class has virtual methods and cannot be used as is.
\n
\
\n
\
A derived class has to override the following methods:
\n
\
\n
\
create(self)
\n
\
clone(self)
\n
\
type(self)
\n
\
radiationType(self)
\n
\
_standardLookup(self, smbl, q)
\n
\
\n
\
Derived class can be added to the global registry of ScatteringFactorTable
\n
\
types by calling the _registerThisType method with any instance.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_radiationType =
"
\
Return a string identifying the radiation type.
\n
\
'X' for x-rays, 'N' for neutrons.
\n
\
This method must be overridden in a derived class.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_lookup =
"
\
Scattering factor of a specified atom at Q in 1/A. The standard value
\n
\
can be redefined using the setCustomAs method.
\n
\
\n
\
smbl -- string symbol for atom, ion or isotope.
\n
\
Q -- Q value in inverse Angstroms, by default 0.
\n
\
Q can be either float or NumPy array.
\n
\
\n
\
Return float or NumPy array of the same shape as Q.
\n
\
No support for Python override.
\n
\
"
;
const
char
* doc_ScatteringFactorTable__standardLookup =
"
\
Standard value of the atom scattering factor at given Q in 1/A.
\n
\
\n
\
smbl -- string symbol for atom, ion or isotope.
\n
\
q -- scattering vector amplitude in 1/A.
\n
\
\n
\
Return float.
\n
\
Raise ValueError for unknown atom symbol.
\n
\
This method must be overridden in a derived class.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_setCustomAs2 =
"
\
Define custom alias for the specified atom symbol.
\n
\
Example: setCustomAs('12-C', 'C') will declare the same
\n
\
scattering factors for '12-C' as for 'C'.
\n
\
\n
\
smbl -- custom string alias for an existing standard symbol.
\n
\
src -- standard atom symbol (cannot be another alias).
\n
\
\n
\
No return value. No support for Python override.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_setCustomAs4 =
"
\
Define custom scattering factor for the specified atom symbol.
\n
\
The custom value is calculated by rescaling standard value
\n
\
from a source atom type.
\n
\
\n
\
smbl -- string symbol of the atom with custom scattering factor.
\n
\
src -- atom symbol for the source standard scattering factor.
\n
\
sf -- new scattering factor value, defaults to the standard src factor.
\n
\
q -- optional Q value for the new custom scattering factor.
\n
\
The internal scaling of the standard value is calculated at this Q.
\n
\
\n
\
No return value. No support for Python override.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_resetCustom =
"
\
Revert scattering factor for the specified symbol to a standard value.
\n
\
\n
\
smbl -- string symbol for atom, ion or isotope.
\n
\
\n
\
No return value. No support for Python override.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_resetAll =
"
\
Reset all custom scattering factor values.
\n
\
\n
\
No return value. No support for Python override.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_getCustomSymbols =
"
\
Return a set of all atom symbols with custom scattering factors.
\n
\
"
;
const
char
* doc_ScatteringFactorTable_ticker =
"
\
Return EventTicker that marks last modification time of this object.
\n
\
This ticker object is used in fast PDF update, to check if scattering
\n
\
factors changed since the last calculation. The ticker.click() method
\n
\
thus needs to be used for _standardLookup that returns variable values.
\n
\
\n
\
Return EventTicker object.
\n
\
This method can be overridden in a Python-derived class.
\n
\
"
;
const
char
* doc_ScatteringFactorTableOwner =
"
\
Base class for classes that own ScatteringFactorTable instance.
\n
\
"
;
const
char
* doc_ScatteringFactorTableOwner_scatteringfactortable =
"
\
ScatteringFactorTable object used for a lookup of scattering factors.
\n
\
This attribute can be assigned either a ScatteringFactorTable-derived object
\n
\
or a string name of a registered ScatteringFactorTable class.
\n
\
Use ScatteringFactorTable.getRegisteredTypes() for a set of registered names.
\n
\
"
;
const
char
* doc_ScatteringFactorTableOwner_getRadiationType =
"
\
Return string identifying the radiation type.
\n
\
'X' for x-rays, 'N' for neutrons.
\n
\
"
;
const
char
* doc_SFTXray =
"
\
X-ray scattering factors table.
\n
\
\n
\
Q-dependence of scattering factors is calculated using
\n
\
Waasmaier - Kirfel approximation valid up to Q=75/A.
\n
\
"
;
const
char
* doc_SFTElectron =
"
\
Electron scattering factors table.
\n
\
\n
\
Q-dependence is derived from X-ray scattering factors according
\n
\
to the International Tables Volume C.
\n
\
"
;
const
char
* doc_SFTNeutron =
"
\
Table of neutron scattering lengths in fm.
\n
\
"
;
const
char
* doc_SFTElectronNumber =
"
\
Table of electron numbers for elements and ions.
\n
\
\n
\
Can be used as Q-indendent scattering factors for X-rays.
\n
\
"
;
//
wrappers ------------------------------------------------------------------
DECLARE_PYSET_METHOD_WRAPPER
(getCustomSymbols, getCustomSymbols_asset)
//
wrappers for the scatteringfactortable property
ScatteringFactorTablePtr
getsftable
(ScatteringFactorTableOwner& obj)
{
return
obj.
getScatteringFactorTable
();
}
DECLARE_BYTYPE_SETTER_WRAPPER
(setScatteringFactorTable, setsftable)
//
Helper class to support Python override of ScatteringFactorTable methods
class
ScatteringFactorTableWrap
:
public ScatteringFactorTable,
public PythonTrampolineTag
{
public:
NB_TRAMPOLINE
(ScatteringFactorTable);
//
Copy Constructor
ScatteringFactorTableWrap
() { }
ScatteringFactorTableWrap
(
const
ScatteringFactorTable& src)
{
ScatteringFactorTable& thistable = *
this
;
//
workaround for weird implicit ScatteringFactorTable::operator=
//
in g++ Red Hat 4.7.2-2, which must have non-constant argument.
thistable =
const_cast
<ScatteringFactorTable&>(src);
}
//
HasClassRegistry methods
ScatteringFactorTablePtr
create
()
const
override
{
nb::gil_scoped_acquire gil;
OverrideTicket
ticket
(nb_trampoline,
"
create
"
,
true
);
if
(!ticket.
key
.
is_valid
())
{
throw
nb::type_error
(
"
pure virtual method ScatteringFactorTable.create() called
"
);
}
nb::object rv = nb_trampoline.
base
().
attr
(ticket.
key
)();
return
mconfigurator.
fetch
(rv);
}
ScatteringFactorTablePtr
clone
()
const
override
{
NB_OVERRIDE_PURE
(clone);
}
const
std::string&
type
()
const
override
{
nb::gil_scoped_acquire gil;
OverrideTicket
ticket
(nb_trampoline,
"
type
"
,
true
);
if
(!ticket.
key
.
is_valid
())
{
throw
nb::type_error
(
"
pure virtual method ScatteringFactorTable.type() called
"
);
}
nb::object tp = nb_trampoline.
base
().
attr
(ticket.
key
)();
mtype = nb::cast<std::string>(tp);
return
mtype;
}
//
own methods
const
std::string&
radiationType
()
const
override
{
nb::gil_scoped_acquire gil;
OverrideTicket
ticket
(nb_trampoline,
"
radiationType
"
,
true
);
if
(!ticket.
key
.
is_valid
())
{
throw
nb::type_error
(
"
pure virtual method ScatteringFactorTable.radiationType() called
"
);
}
nb::object tp = nb_trampoline.
base
().
attr
(ticket.
key
)();
mradiationtype = nb::cast<std::string>(tp);
return
mradiationtype;
}
double
standardLookup
(
const
std::string& smbl,
double
q)
const
override
{
NB_OVERRIDE_PURE_NAME
(
"
_standardLookup
"
, standardLookup, smbl, q);
}
//
Make the ticker method overridable from Python
diffpy::eventticker::EventTicker&
ticker
()
const
{
using
diffpy::eventticker::EventTicker;
nb::gil_scoped_acquire gil;
OverrideTicket
ticket
(nb_trampoline,
"
ticker
"
,
false
);
if
(ticket.
key
.
is_valid
())
{
nb::object ptic = nb_trampoline.
base
().
attr
(ticket.
key
)();
return
nb::cast<EventTicker&>(ptic);
}
return
this
->
default_ticker
();
}
diffpy::eventticker::EventTicker&
default_ticker
()
const
{
return
this
->
ScatteringFactorTable
::ticker
();
}
protected:
//
HasClassRegistry method
void
setupRegisteredObject
(ScatteringFactorTablePtr p)
const
override
{
mconfigurator.
setup
(p);
}
private:
mutable
std::string mtype;
mutable
std::string mradiationtype;
wrapper_registry_configurator<ScatteringFactorTable> mconfigurator;
};
//
class ScatteringFactorTableWrap
nb::object
lookupnparray
(
const
ScatteringFactorTable& sftb,
std::string smbl, nb::object& qobj)
{
if
(nb::isinstance<nb::float_>(qobj) || nb::isinstance<nb::int_>(qobj))
return
nb::float_
(sftb.
lookup
(smbl,
extractdouble
(qobj)));
NumPyDoubleInput aa =
extractNumPyDoubleArray
(qobj);
auto
bb =
createNumPyDoubleArrayLike
(aa);
const
double
* src = aa.
data
();
const
double
* last = src + aa.
size
();
double
* dst = bb.
array
.
data
();
for
(; src != last; ++src, ++dst)
{
*dst = sftb.
lookup
(smbl, *src);
}
return
bb.
object
;
}
}
//
namespace nswrap_ScatteringFactorTable
//
Wrapper definition --------------------------------------------------------
//
TODO: rework pickle helpers
void
wrap_ScatteringFactorTable
(nb::module_& m)
{
using
namespace
nswrap_ScatteringFactorTable
;
typedef
ScatteringFactorTableOwner SFTOwner;
nb::class_<ScatteringFactorTable, ScatteringFactorTableWrap>
sftb
(m,
"
ScatteringFactorTable
"
,
nb::dynamic_attr
(),
nb::is_weak_referenceable
(), doc_ScatteringFactorTable);
wrap_registry_methods
(sftb)
.
def
(nb::init<>())
.
def
(
"
radiationType
"
,
[](
const
ScatteringFactorTable &obj)
{
return
std::string
(obj.
radiationType
());
},
doc_ScatteringFactorTable_radiationType)
.
def
(
"
lookup
"
,
lookupnparray,
nb::arg
(
"
smbl
"
),
nb::arg
(
"
qarray
"
))
.
def
(
"
lookup
"
,
&ScatteringFactorTable::lookup,
nb::arg
(
"
smbl
"
),
nb::arg
(
"
q
"
)=
0.0
,
doc_ScatteringFactorTable_lookup)
.
def
(
"
_standardLookup
"
,
&ScatteringFactorTable::standardLookup,
nb::arg
(
"
smbl
"
),
nb::arg
(
"
q
"
),
doc_ScatteringFactorTable__standardLookup)
.
def
(
"
setCustomAs
"
, (
void
(ScatteringFactorTable::*)
(
const
std::string&,
const
std::string&))
&ScatteringFactorTable::setCustomAs,
nb::arg
(
"
smbl
"
),
nb::arg
(
"
src
"
),
doc_ScatteringFactorTable_setCustomAs2)
.
def
(
"
setCustomAs
"
, (
void
(ScatteringFactorTable::*)
(
const
std::string&,
const
std::string&,
double
,
double
))
&ScatteringFactorTable::setCustomAs,
nb::arg
(
"
smbl
"
),
nb::arg
(
"
src
"
),
nb::arg
(
"
sf
"
),
nb::arg
(
"
q
"
)=
0.0
,
doc_ScatteringFactorTable_setCustomAs4)
.
def
(
"
resetCustom
"
, &ScatteringFactorTable::resetCustom,
nb::arg
(
"
smbl
"
), doc_ScatteringFactorTable_resetCustom)
.
def
(
"
resetAll
"
, &ScatteringFactorTable::resetAll,
doc_ScatteringFactorTable_resetAll)
.
def
(
"
getCustomSymbols
"
, getCustomSymbols_asset<ScatteringFactorTable>,
doc_ScatteringFactorTable_getCustomSymbols)
.
def
(
"
ticker
"
,
[](
const
ScatteringFactorTable& obj)
-> diffpy::eventticker::EventTicker&
{
return
obj.
ScatteringFactorTable
::ticker
();
},
nb::rv_policy::reference_internal)
.
def
(
"
ticker
"
,
&ScatteringFactorTable::ticker,
nb::rv_policy::reference_internal,
doc_ScatteringFactorTable_ticker)
;
SerializationPickleSuite<
ScatteringFactorTable,
DICT_PICKLE
>::
bind
(sftb);
nb::class_<SFTXray, ScatteringFactorTable>
sftxray
(m,
"
SFTXray
"
, doc_SFTXray);
sftxray
.
def
(nb::init<>())
;
SerializationPickleSuite<SFTXray,
DICT_GUARD
>::
bind
(sftxray);
nb::class_<SFTElectron, ScatteringFactorTable>
sftelectron
(m,
"
SFTElectron
"
, doc_SFTElectron);
sftelectron
.
def
(nb::init<>())
;
SerializationPickleSuite<SFTElectron,
DICT_GUARD
>::
bind
(sftelectron);
nb::class_<SFTNeutron, ScatteringFactorTable>
sftneutron
(m,
"
SFTNeutron
"
, doc_SFTNeutron);
sftneutron
.
def
(nb::init<>())
;
SerializationPickleSuite<SFTNeutron,
DICT_GUARD
>::
bind
(sftneutron);
nb::class_<SFTElectronNumber, ScatteringFactorTable>
sftelectronnumber
(m,
"
SFTElectronNumber
"
, doc_SFTElectronNumber);
sftelectronnumber
.
def
(nb::init<>())
;
SerializationPickleSuite<SFTElectronNumber,
DICT_GUARD
>::
bind
(sftelectronnumber);
nb::class_<ScatteringFactorTableOwner>(m,
"
ScatteringFactorTableOwner
"
,
doc_ScatteringFactorTableOwner,
nb::dynamic_attr
(),
nb::is_weak_referenceable
())
.
def
(nb::init<>())
.
def_prop_rw
(
"
scatteringfactortable
"
,
getsftable,
setsftable<ScatteringFactorTableOwner,ScatteringFactorTable>,
doc_ScatteringFactorTableOwner_scatteringfactortable)
.
def
(
"
getRadiationType
"
,
[](
const
SFTOwner &obj)
{
return
std::string
(obj.
getRadiationType
());
},
doc_ScatteringFactorTableOwner_getRadiationType)
;
}
}
//
namespace srrealmodule
//
Serialization -------------------------------------------------------------
BOOST_CLASS_EXPORT_GUID
(srrealmodule::LegacyComponent<diffpy::srreal::ScatteringFactorTable>,
"
srrealmodule::nswrap_ScatteringFactorTable::ScatteringFactorTableWrap
"
)
//
End of file
Back
|
FazBrowse Home
|
New Git URL