-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathwrap_PDFEnvelope.cpp
More file actions
212 lines (170 loc) · 6.27 KB
/
wrap_PDFEnvelope.cpp
File metadata and controls
212 lines (170 loc) · 6.27 KB
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
/*****************************************************************************
*
* 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 PDFEnvelope class. The business methods can be overloaded
* from Python to create custom PDF envelope functions.
*
*****************************************************************************/
#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/register_ptr_to_python.hpp>
#include <diffpy/srreal/PDFEnvelope.hpp>
#include <diffpy/srreal/QResolutionEnvelope.hpp>
#include <diffpy/srreal/ScaleEnvelope.hpp>
#include <diffpy/srreal/SphericalShapeEnvelope.hpp>
#include <diffpy/srreal/StepCutEnvelope.hpp>
#include "srreal_numpy_symbol.hpp"
// numpy/arrayobject.h needs to be included after srreal_numpy_symbol.hpp,
// which defines PY_ARRAY_UNIQUE_SYMBOL. NO_IMPORT_ARRAY indicates
// import_array will be called in the extension module initializer.
#define NO_IMPORT_ARRAY
#include <numpy/arrayobject.h>
#include "srreal_converters.hpp"
#include "srreal_pickling.hpp"
#include "srreal_registry.hpp"
namespace srrealmodule {
namespace nswrap_PDFEnvelope {
using namespace boost;
using namespace boost::python;
using namespace diffpy::srreal;
// docstrings ----------------------------------------------------------------
const char* doc_PDFEnvelope = "\
Base class and registry for functions that return PDF scaling envelopes.\n\
";
const char* doc_PDFEnvelope___call__ = "\
Calculate PDF envelope at the specified r.\n\
\n\
r -- atom distance in Angstroms where the scale envelope is evaluated.\n\
Float or NumPy array.\n\
\n\
Return float or NumPy array.\n\
";
const char* doc_QResolutionEnvelope = "\
Gaussian dampening envelope function due to limited Q-resolution.\n\
\n\
Returns exp(-qdamp * x**2).\n\
Returns 1 when qdamp is zero or negative.\n\
";
const char* doc_ScaleEnvelope = "\
Uniform scaling envelope function.\n\
\n\
Returns scale.\n\
";
const char* doc_SphericalShapeEnvelope = "\
Dampening PDF envelope due to finite spherical particle shape.\n\
\n\
Returns (1 - 1.5*tau + 0.5*tau**3), where tau = x/spdiameter.\n\
Returns 1 when spdiameter <= 0.\n\
";
const char* doc_StepCutEnvelope = "\n\
Step function damping envelope.\n\
\n\
Returns 1 when x <= stepcut or if stepcut <= 0.\n\
Returns 0 when x > stepcut.\n\
";
// wrappers ------------------------------------------------------------------
// Helper class allows overload of the PDFEnvelope methods from Python.
class PDFEnvelopeWrap :
public PDFEnvelope,
public wrapper_srreal<PDFEnvelope>
{
public:
// HasClassRegistry methods
PDFEnvelopePtr create() const
{
object rv = this->get_pure_virtual_override("create")();
return mconfigurator.fetch(rv);
}
PDFEnvelopePtr clone() const
{
return this->get_pure_virtual_override("clone")();
}
const std::string& type() const
{
python::object tp = this->get_pure_virtual_override("type")();
mtype = python::extract<std::string>(tp);
return mtype;
}
// own methods
double operator()(const double& x) const
{
return this->get_pure_virtual_override("__call__")(x);
}
protected:
// HasClassRegistry method
void setupRegisteredObject(PDFEnvelopePtr p) const
{
mconfigurator.setup(p);
}
private:
mutable std::string mtype;
wrapper_registry_configurator<PDFEnvelope> mconfigurator;
// serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
using boost::serialization::base_object;
ar & base_object<PDFEnvelope>(*this);
}
}; // class PDFEnvelopeWrap
object callnparray(const PDFEnvelope* obj, object& x)
{
NumPyArray_DoublePtr xx = extractNumPyDoubleArray(x);
NumPyArray_DoublePtr yy = createNumPyDoubleArrayLike(xx.first);
double* src = xx.second;
double* last = xx.second + PyArray_Size(xx.first.ptr());
double* dst = yy.second;
for (; src != last; ++src, ++dst) *dst = (*obj)(*src);
return yy.first;
}
} // namespace nswrap_PDFEnvelope
// Wrapper definition --------------------------------------------------------
void wrap_PDFEnvelope()
{
using namespace nswrap_PDFEnvelope;
using diffpy::Attributes;
namespace bp = boost::python;
class_<PDFEnvelopeWrap, bases<Attributes>, noncopyable>
pdfenvelope("PDFEnvelope", doc_PDFEnvelope);
wrap_registry_methods(pdfenvelope)
.def("__call__", callnparray,
bp::arg("r_array"))
.def("__call__", &PDFEnvelope::operator(),
bp::arg("r"), doc_PDFEnvelope___call__)
.def_pickle(SerializationPickleSuite<PDFEnvelope,DICT_PICKLE>())
;
register_ptr_to_python<PDFEnvelopePtr>();
class_<QResolutionEnvelope, bases<PDFEnvelope> >(
"QResolutionEnvelope", doc_QResolutionEnvelope)
.def_pickle(SerializationPickleSuite<QResolutionEnvelope>())
;
class_<ScaleEnvelope, bases<PDFEnvelope> >(
"ScaleEnvelope", doc_ScaleEnvelope)
.def_pickle(SerializationPickleSuite<ScaleEnvelope>())
;
class_<SphericalShapeEnvelope, bases<PDFEnvelope> >(
"SphericalShapeEnvelope", doc_SphericalShapeEnvelope)
.def_pickle(SerializationPickleSuite<SphericalShapeEnvelope>())
;
class_<StepCutEnvelope, bases<PDFEnvelope> >(
"StepCutEnvelope", doc_StepCutEnvelope)
.def_pickle(SerializationPickleSuite<StepCutEnvelope>())
;
}
} // namespace srrealmodule
// Serialization -------------------------------------------------------------
BOOST_CLASS_EXPORT(srrealmodule::nswrap_PDFEnvelope::PDFEnvelopeWrap)
// End of file