forked from diffpy/diffpy.pdffit2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.cc
More file actions
2218 lines (2036 loc) · 63.2 KB
/
misc.cc
File metadata and controls
2218 lines (2036 loc) · 63.2 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
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************
*
* pdffit2 by DANSE Diffraction group
* Simon J. L. Billinge
* (c) 2006 trustees of the Michigan State University
* All rights reserved.
*
* File coded by: Chris Farrow
*
* See AUTHORS.txt for a list of people who contributed.
* See LICENSE.txt for license information.
*
************************************************************************
*
* Bindings from python to c++ PdfFit class.
*
* Comments:
*
***********************************************************************/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cassert>
#include "misc.h"
#include "pyexceptions.h"
#include "PyFileStreambuf.h"
#include "../libpdffit2/StringUtils.h"
#include "../libpdffit2/LocalPeriodicTable.h"
#include "../libpdffit2/pdffit.h"
// ostream buffer used for engine output redirection
PyFileStreambuf* py_stdout_streambuf = NULL;
// copyright
char pypdffit2_copyright__doc__[] = "";
char pypdffit2_copyright__name__[] = "copyright";
static char pypdffit2_copyright_note[] =
"pdffit2 python module: Copyright (c) 2005-2016 Simon J. L. Billinge et al.";
// constant strings for python capsule names (cn)
static char* cnpfit = "pdffit";
static char* cnvar = "pdfvar";
PyObject * pypdffit2_copyright(PyObject *, PyObject *)
{
return Py_BuildValue("s", pypdffit2_copyright_note);
}
//helper function to convert a pylist to a double array.
void double_array_from_pylist(PyObject *pylist, double *d_array, int const length)
{
//length is the size of the d_array and not necessarily equal to the length
//of the pylist
PyObject *pyval = 0;
for(int i = 0; i < length; i++) {
pyval = PyList_GetItem(pylist, i);
d_array[i] = PyFloat_AsDouble(pyval);
}
}
// helper function to delete PdfFit object
static void deletePdfFit(PyObject* ptr)
{
PdfFit *pdf = (PdfFit *)PyCapsule_GetPointer(ptr, cnpfit);
delete pdf;
return;
}
// create a PdfFit instance
char pypdffit2_create__doc__[] = "";
char pypdffit2_create__name__[] = "create";
PyObject * pypdffit2_create(PyObject *, PyObject *args)
{
PdfFit *ppdf = new PdfFit();
PyObject *py_ppdf = PyCapsule_New((void *)ppdf, cnpfit, deletePdfFit);
return py_ppdf;
}
// read_struct
char pypdffit2_read_struct__doc__[] = "Read structure file into memory.";
char pypdffit2_read_struct__name__[] = "read_struct";
PyObject * pypdffit2_read_struct(PyObject *, PyObject *args)
{
char *fname;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Os", &py_ppdf, &fname);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try {
ppdf->read_struct(fname);
}
catch(structureError e) {
PyErr_SetString(pypdffit2_structureError, e.GetMsg().c_str());
return 0;
}
catch(ValueError e) {
PyErr_SetString(pypdffit2_structureError, e.GetMsg().c_str());
return 0;
}
catch(calculationError e) {
PyErr_SetString(pypdffit2_calculationError, e.GetMsg().c_str());
return 0;
}
catch(IOError e) {
PyErr_SetString(PyExc_IOError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// read_struct_string
char pypdffit2_read_struct_string__doc__[] = "Read structure file into memory from a c-string.";
char pypdffit2_read_struct_string__name__[] = "read_struct_string";
PyObject * pypdffit2_read_struct_string(PyObject *, PyObject *args)
{
char *buffer;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Os", &py_ppdf, &buffer);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try {
ppdf->read_struct_string(buffer);
}
catch(structureError e) {
PyErr_SetString(pypdffit2_structureError, e.GetMsg().c_str());
return 0;
}
catch(ValueError e) {
PyErr_SetString(pypdffit2_structureError, e.GetMsg().c_str());
return 0;
}
catch(calculationError e) {
PyErr_SetString(pypdffit2_calculationError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// read_data
char pypdffit2_read_data__doc__[] = "Read data file into memory.";
char pypdffit2_read_data__name__[] = "read_data";
PyObject * pypdffit2_read_data(PyObject *, PyObject *args)
{
char *fname;
char stype;
double qmax, qdamp;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Oscdd", &py_ppdf, &fname, &stype, &qmax, &qdamp);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try {
ppdf->read_data(fname, stype, qmax, qdamp);
}
catch(IOError e) {
PyErr_SetString(PyExc_IOError, e.GetMsg().c_str());
return 0;
}
catch(dataError e) {
PyErr_SetString(pypdffit2_dataError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// read_data_string
char pypdffit2_read_data_string__doc__[] = "Read data from string into memory.";
char pypdffit2_read_data_string__name__[] = "read_data_string";
PyObject * pypdffit2_read_data_string(PyObject *, PyObject *args)
{
char *buffer;
char *c_name = NULL;
char stype;
double qmax, qdamp;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Oscdd|s", &py_ppdf, &buffer, &stype, &qmax, &qdamp, &c_name);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
string name = c_name ? c_name : "";
try {
string sbuffer(buffer);
ppdf->read_data_string(sbuffer, stype, qmax, qdamp, name);
}
catch(IOError e) {
PyErr_SetString(PyExc_IOError, e.GetMsg().c_str());
return 0;
}
catch(dataError e) {
PyErr_SetString(pypdffit2_dataError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// read_data_arrays - read_data_lists in PdfFit class
char pypdffit2_read_data_arrays__doc__[] = "Read data from arrays into memory.";
char pypdffit2_read_data_arrays__name__[] = "read_data_arrays";
PyObject * pypdffit2_read_data_arrays(PyObject *, PyObject *args)
{
char stype;
double qmax, qdamp;
int length;
char * c_name = NULL;
double *r_data = NULL;
double *Gr_data = NULL;
double *dGr_data = NULL;
PyObject *py_r_data = Py_None;
PyObject *py_Gr_data = Py_None;
PyObject *py_dGr_data = Py_None;
PyObject *py_ppdf = NULL;
int ok = PyArg_ParseTuple(args, "OcddOO|Os", &py_ppdf, &stype, &qmax, &qdamp,
&py_r_data, &py_Gr_data, &py_dGr_data, &c_name);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
length = PyList_Size(py_Gr_data);
//quick check that the arrays are all the same length //
int r_len = PyList_Size(py_r_data);
int dGr_len = length;
if(py_dGr_data != Py_None) {
dGr_len = PyList_Size(py_dGr_data);
}
if(r_len != length || dGr_len != length)
{
string err_string = "Data arrays have different lengths";
PyErr_SetString(PyExc_ValueError, err_string.c_str());
return 0;
}
// create data arrays
r_data = new double [length];
double_array_from_pylist(py_r_data, r_data, length);
Gr_data = new double [length];
double_array_from_pylist(py_Gr_data, Gr_data, length);
if(py_dGr_data != Py_None) {
dGr_data = new double [length];
double_array_from_pylist(py_dGr_data, dGr_data, length);
}
string name = c_name;
try {
ppdf->read_data_arrays(stype, qmax, qdamp, length,
r_data, Gr_data, dGr_data, name);
}
catch(dataError e) {
PyErr_SetString(pypdffit2_dataError, e.GetMsg().c_str());
return 0;
}
// read_data_arrays creates its own copy of the data, so we must delete our
// copies here.
delete [] r_data;
delete [] Gr_data;
if( dGr_data != NULL ) delete [] dGr_data;
Py_INCREF(Py_None);
return Py_None;
}
// pdfrange (range in c)
char pypdffit2_pdfrange__doc__[] = "Set r-range of pdf.";
char pypdffit2_pdfrange__name__[] = "pdfrange";
PyObject * pypdffit2_pdfrange(PyObject *, PyObject *args)
{
int iset;
double rmin, rmax;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Oidd", &py_ppdf, &iset, &rmin, &rmax);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try {
ppdf->range(iset, rmin, rmax);
}
catch (ValueError e) {
PyErr_SetString(PyExc_ValueError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// reset
char pypdffit2_reset__doc__[] = "reset pdf data";
char pypdffit2_reset__name__[] = "reset";
PyObject * pypdffit2_reset(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
ppdf->reset();
Py_INCREF(Py_None);
return Py_None;
}
// alloc
char pypdffit2_alloc__doc__[] = "Allocate space for pdf data";
char pypdffit2_alloc__name__[] = "alloc";
PyObject * pypdffit2_alloc(PyObject *, PyObject *args)
{
char stype;
double qmax, qdamp, rmin, rmax;
int bin;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Ocddddi", &py_ppdf, &stype, &qmax, &qdamp, &rmin, &rmax, &bin);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try {
ppdf->alloc(stype, qmax, qdamp, rmin, rmax, bin);
}
catch (ValueError e) {
PyErr_SetString(PyExc_ValueError, e.GetMsg().c_str());
return 0;
}
catch (unassignedError e) {
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// calc
char pypdffit2_calc__doc__[] = "calculate pdf from data";
char pypdffit2_calc__name__[] = "calc";
PyObject * pypdffit2_calc(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try {
ppdf->calc();
}
catch (calculationError e) {
PyErr_SetString(pypdffit2_calculationError, e.GetMsg().c_str());
return 0;
}
catch (unassignedError e) {
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
catch (parseError e) {
PyErr_SetString(pypdffit2_constraintError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// refine
char pypdffit2_refine__doc__[] = "refine model to pdf data";
char pypdffit2_refine__name__[] = "refine";
PyObject * pypdffit2_refine(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
double toler;
int ok = PyArg_ParseTuple(args, "Od", &py_ppdf, &toler);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try {
ppdf->refine(true, toler);
}
catch(constraintError e) {
PyErr_SetString(pypdffit2_constraintError, e.GetMsg().c_str());
return 0;
}
catch(calculationError e) {
PyErr_SetString(pypdffit2_calculationError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// local helper class for background thread in pypdffit2_refine_step()
namespace {
class RefineStepHelper
{
private:
PyThreadState* thread_state;
ostringstream msgout;
public:
// Constructor saves thread state and arranges for holding
// engine output when redirected
RefineStepHelper()
{
if (py_stdout_streambuf)
{
NS_PDFFIT2::pout->rdbuf(msgout.rdbuf());
}
thread_state = PyEval_SaveThread();
}
// method for restoring thread state and writing any outstanding output
void clean()
{
PyEval_RestoreThread(thread_state);
if (py_stdout_streambuf)
{
NS_PDFFIT2::pout->rdbuf(py_stdout_streambuf);
*NS_PDFFIT2::pout << msgout.str();
}
}
};
} // local namespace
// refine_step
char pypdffit2_refine_step__doc__[] = "Make one step in the refinement process.";
char pypdffit2_refine_step__name__[] = "refine_step";
PyObject * pypdffit2_refine_step(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
double toler;
int ok = PyArg_ParseTuple(args, "Od", &py_ppdf, &toler);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
RefineStepHelper janitor; // takes care of thread an output issues
int finished = 1;
try {
finished = ppdf->refine_step(true, toler);
}
catch(parseError e) {
// parseError is due to invalid constraint
janitor.clean();
PyErr_SetString(pypdffit2_constraintError, e.GetMsg().c_str());
return 0;
}
catch(constraintError e) {
janitor.clean();
PyErr_SetString(pypdffit2_constraintError, e.GetMsg().c_str());
return 0;
}
catch(calculationError e) {
janitor.clean();
PyErr_SetString(pypdffit2_calculationError, e.GetMsg().c_str());
return 0;
}
catch(unassignedError e) {
janitor.clean();
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
catch(...) {
janitor.clean();
return 0;
}
janitor.clean();
return Py_BuildValue("i", finished);
}
// save_pdf
char pypdffit2_save_pdf__doc__[] = "Save calculated pdf to file";
char pypdffit2_save_pdf__name__[] = "save_pdf";
PyObject * pypdffit2_save_pdf(PyObject *, PyObject *args)
{
char *fname;
int iset;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Ois", &py_ppdf, &iset, &fname);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
string outfilestring = ppdf->save_pdf(iset, fname);
return Py_BuildValue("s", outfilestring.c_str());
}
catch(IOError e)
{
PyErr_SetString(PyExc_IOError, e.GetMsg().c_str());
return 0;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// save_dif
char pypdffit2_save_dif__doc__[] = "Save pdf difference to file";
char pypdffit2_save_dif__name__[] = "save_dif";
PyObject * pypdffit2_save_dif(PyObject *, PyObject *args)
{
char *fname;
int iset;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Ois", &py_ppdf, &iset, &fname);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
string outfilestring = ppdf->save_dif(iset, fname);
return Py_BuildValue("s", outfilestring.c_str());
}
catch(IOError e)
{
PyErr_SetString(PyExc_IOError, e.GetMsg().c_str());
return 0;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// save_res
char pypdffit2_save_res__doc__[] = "Save residual to file";
char pypdffit2_save_res__name__[] = "save_res";
PyObject * pypdffit2_save_res(PyObject *, PyObject *args)
{
char *fname;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Os", &py_ppdf, &fname);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
string outfilestring = ppdf->save_res(fname);
return Py_BuildValue("s", outfilestring.c_str());
}
catch(IOError e)
{
PyErr_SetString(PyExc_IOError, e.GetMsg().c_str());
return 0;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// save_struct
char pypdffit2_save_struct__doc__[] = "Save refined structure to file";
char pypdffit2_save_struct__name__[] = "save_struct";
PyObject * pypdffit2_save_struct(PyObject *, PyObject *args)
{
char *fname;
int iset;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Ois", &py_ppdf, &iset, &fname);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
string outfilestring = ppdf->save_struct(iset, fname);
return Py_BuildValue("s", outfilestring.c_str());
}
catch(IOError e)
{
PyErr_SetString(PyExc_IOError, e.GetMsg().c_str());
return 0;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// show_struct
char pypdffit2_show_struct__doc__[] = "Show structure.";
char pypdffit2_show_struct__name__[] = "show_struct";
PyObject * pypdffit2_show_struct(PyObject *, PyObject *args)
{
int ip;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "Oi", &py_ppdf, &ip);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
string outfilestring = ppdf->show_struct(ip);
return Py_BuildValue("s", outfilestring.c_str());
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// constrain to string
char pypdffit2_constrain_str__doc__[] = "Constrain refined variable to string.";
char pypdffit2_constrain_str__name__[] = "constrain_str";
PyObject* pypdffit2_constrain_str(PyObject*, PyObject* args)
{
PyObject* py_v = 0;
char* vname;
char* form;
PyObject* py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "OOss", &py_ppdf, &py_v, &vname, &form);
if (!ok) return 0;
PdfFit* ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
RefVar* v = (RefVar *) PyCapsule_GetPointer(py_v, cnvar);
if (v->type() != "RefVar")
{
string emsg = "cannot constrain non-refinable variable ";
emsg += vname;
PyErr_SetString(pypdffit2_constraintError, emsg.c_str());
return 0;
}
if (v->isAssigned()) {
try {
ppdf->constrain(*v, form);
}
catch (constraintError e) {
PyErr_SetString(pypdffit2_constraintError, e.GetMsg().c_str());
return 0;
}
catch (unassignedError e) {
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
}
else {
ostringstream emsg;
emsg << "Variable " << vname << " was not yet assigned";
PyErr_SetString(pypdffit2_unassignedError, emsg.str().c_str());
}
Py_INCREF(Py_None);
return Py_None;
}
// constrain to integer.
char pypdffit2_constrain_int__doc__[] = "Constrain refined variable to integer.";
char pypdffit2_constrain_int__name__[] = "constrain_int";
PyObject* pypdffit2_constrain_int(PyObject*, PyObject* args)
{
PyObject* py_v = 0;
int ftype = 0;
char* vname;
int ipar;
PyObject* py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "OOsi|i", &py_ppdf, &py_v, &vname, &ipar, &ftype);
if (!ok) return 0;
PdfFit* ppdf = (PdfFit*) PyCapsule_GetPointer(py_ppdf, cnpfit);
RefVar* v = (RefVar*) PyCapsule_GetPointer(py_v, cnvar);
if (v->type() != "RefVar")
{
string emsg = "cannot constrain non-refinable variable ";
emsg += vname;
PyErr_SetString(pypdffit2_constraintError, emsg.c_str());
return 0;
}
if (v->isAssigned()) {
try {
if (ftype)
{
ppdf->constrain(*v, ipar, (FCON)ftype);
}
else ppdf->constrain(*v, ipar);
}
catch (constraintError e) {
PyErr_SetString(pypdffit2_constraintError, e.GetMsg().c_str());
return 0;
}
catch (unassignedError e) {
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
}
else {
ostringstream emsg;
emsg << "Variable " << vname << " was not yet assigned";
PyErr_SetString(pypdffit2_unassignedError, emsg.str().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// setpar with value of double
char pypdffit2_setpar_dbl__doc__[] = "Set parameter value.";
char pypdffit2_setpar_dbl__name__[] = "setpar_dbl";
PyObject * pypdffit2_setpar_dbl(PyObject *, PyObject *args)
{
unsigned int n;
double val;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "OId", &py_ppdf, &n, &val);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
ppdf->setpar(n, val);
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// setpar with value of RefVar
char pypdffit2_setpar_RV__doc__[] = "Set parameter value via refined variable.";
char pypdffit2_setpar_RV__name__[] = "setpar_RV";
PyObject * pypdffit2_setpar_RV(PyObject *, PyObject *args)
{
unsigned int n;
RefVar *v;
PyObject *py_v;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "OIO", &py_ppdf, &n, &py_v);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
v = (RefVar *) PyCapsule_GetPointer(py_v, cnvar);
if( v->isAssigned() ) {
try
{
ppdf->setpar(n, *v);
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
}
else {
string eout = "Variable not yet assigned";
PyErr_SetString(pypdffit2_unassignedError, eout.c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// setvar
char pypdffit2_setvar__doc__[] = "Set variable to value.";
char pypdffit2_setvar__name__[] = "setvar";
PyObject * pypdffit2_setvar(PyObject *, PyObject *args)
{
double a;
NonRefVar *v;
PyObject *py_v;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "OOd", &py_ppdf, &py_v, &a);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
v = (NonRefVar *) PyCapsule_GetPointer(py_v, cnvar);
if( v->isAssigned() ) {
ppdf->setvar(*v, a);
}
else {
string eout = "Must import a structure";
PyErr_SetString(pypdffit2_unassignedError, eout.c_str());
return 0;
}
Py_INCREF(Py_None);
return Py_None;
}
// getvar
char pypdffit2_getvar__doc__[] = "Get variable value.";
char pypdffit2_getvar__name__[] = "getvar";
PyObject * pypdffit2_getvar(PyObject *, PyObject *args)
{
NonRefVar *v = 0;
PyObject *py_v = 0;
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "OO", &py_ppdf, &py_v);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
v = (NonRefVar *) PyCapsule_GetPointer(py_v, cnvar);
if(v->isAssigned()) {
double crval = ppdf->getvar(*v);
return Py_BuildValue("d", crval);
}
else {
string eout = "Variable not yet assigned";
PyErr_SetString(pypdffit2_unassignedError, eout.c_str());
return 0;
Py_INCREF(Py_None);
return Py_None;
}
}
// getcrw
char pypdffit2_getcrw__doc__[] = "Get cumulative Rw for the current dataset.";
char pypdffit2_getcrw__name__[] = "getcrw";
PyObject * pypdffit2_getcrw(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
vector<double> crw = ppdf->getcrw();
PyObject *py_r;
py_r = PyList_New(crw.size());
for (int i = 0; i != int(crw.size()); ++i)
{
PyList_SetItem(py_r, i, PyFloat_FromDouble(crw[i]));
}
return py_r;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
}
// getrw
char pypdffit2_getrw__doc__[] = "Get rw of fit.";
char pypdffit2_getrw__name__[] = "getrw";
PyObject * pypdffit2_getrw(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
double crval = ppdf->getrw();
return Py_BuildValue("d", crval);
}
// getR
char pypdffit2_getR__doc__[] = "Get list of r-values for plotting.";
char pypdffit2_getR__name__[] = "getR";
PyObject * pypdffit2_getR(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
/* One should not put functionality in the bindings. However,
* this function is meant to create a python object from a
* c-object that does not actually exist. All that is stored
* in pdffit about the fitted R-range is the minimum, maximum,
* and step size. This binding turns that info into a python
* list. If the number of fit points stored in nfmax and nfmin
* have not yet been assigned, then this falls back on the
* r-points. This is crucial so that one can investigate the
* PDF before any fitting has been done. Once fit parameters
* have been established, they can be used and the r list,
* pdf-obs list, and pdf-fit list will be the same size.
* Also see getpdf_obs().
*/
try
{
int nfmin = ppdf->getnfmin();
int nfmax = ppdf->getnfmax();
int len = nfmax - nfmin + 1;
double rmin = ppdf->getrmin();
double rmax = ppdf->getrmax();
double deltar = ppdf->getdeltar();
PyObject *py_r;
if(len == 1)
{
nfmin = 0;
nfmax = (int) ((rmax - rmin)/deltar);
len = nfmax + 1;
}
py_r = PyList_New(len);
for (int i=nfmin;i<=nfmax;i++)
{
PyList_SetItem(py_r, i-nfmin, Py_BuildValue("d", i*deltar + rmin));
}
return py_r;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
}
// getpdf_fit
char pypdffit2_getpdf_fit__doc__[] = "Get list of calculated pdf points.";
char pypdffit2_getpdf_fit__name__[] = "getpdf_fit";
PyObject * pypdffit2_getpdf_fit(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
//Return only the data range used in the fit
try
{
int min = ppdf->getnfmin();
int max = ppdf->getnfmax();
int len = max - min + 1;
vector<double> v_pdfdata = ppdf->getpdf_fit();
PyObject *py_r;
py_r = PyList_New(len);
for (int i=min;i<=max;i++) {
PyList_SetItem(py_r, i-min, Py_BuildValue("d", v_pdfdata[i]));
}
return py_r;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
}
// getpdf_obs
char pypdffit2_getpdf_obs__doc__[] = "Get list of observed (theory) pdf points.";
char pypdffit2_getpdf_obs__name__[] = "getpdf_obs";
PyObject * pypdffit2_getpdf_obs(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{
vector<double> v_pdfdata = ppdf->getpdf_obs();
int nfmin = ppdf->getnfmin();
int nfmax = ppdf->getnfmax();
int len = nfmax - nfmin + 1;
//Return only the data range used in the fit
PyObject *py_r;
py_r = PyList_New(len);
for (int i=nfmin;i<=nfmax;i++) {
PyList_SetItem(py_r, i-nfmin, Py_BuildValue("d", v_pdfdata[i]));
}
return py_r;
}
catch(unassignedError e)
{
PyErr_SetString(pypdffit2_unassignedError, e.GetMsg().c_str());
return 0;
}
}
// getpdf_diff
char pypdffit2_getpdf_diff__doc__[] = "Get list of differences between observed and fitted PDF points.";
char pypdffit2_getpdf_diff__name__[] = "getpdf_diff";
PyObject * pypdffit2_getpdf_diff(PyObject *, PyObject *args)
{
PyObject *py_ppdf = 0;
int ok = PyArg_ParseTuple(args, "O", &py_ppdf);
if (!ok) return 0;
PdfFit *ppdf = (PdfFit *) PyCapsule_GetPointer(py_ppdf, cnpfit);
try
{