RTModel 2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,412 @@
1
+ // Finalizer.cpp : main project file.
2
+
3
+ #define _USE_MATH_DEFINES
4
+ #define _CRT_SECURE_NO_WARNINGS
5
+ #include <stdio.h>
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+ #include <math.h>
9
+ #include <malloc.h>
10
+ #include <regex>
11
+ #include <filesystem>
12
+ #include "bumper.h"
13
+
14
+ using namespace std;
15
+ using namespace std::filesystem;
16
+
17
+ const double failthr = 20000000.0; // threshold for chisquare/dof for declaring failure
18
+ const int ncategories = 7;
19
+
20
+ int main(int argc, char *argv[]){
21
+
22
+ char eventname[512]="";
23
+ char filename[30]="";
24
+ char command[256], buffer[256];
25
+ double value;
26
+ int nfil, * satel;
27
+ double *t,*y,*w,*pr,*sigmapr,thsigma;
28
+ double thrs[10] = { 0,36.,40.0872,43.4518,46.4625,49.2497,51.878 ,54.3854 ,56.7964 ,59.1282 }; // thresholds at 6 sigma for n more parameters
29
+ string modelcodes[ncategories] = { "PS","PX","BS","BO","LS","LX","LO" };
30
+ string modelnames[ncategories] = { "Single-Lens-Single-Source","Single-Lens-Single-Source with parallax",\
31
+ "Binary Source","Binary Source with xallarap",\
32
+ ""," with parallax"," with orbital motion" };
33
+ int npss[ncategories] = { 4,6,7,10,7,9,12 };
34
+ double chis[ncategories];
35
+ double cmin, c0, c1, c2, cflat;
36
+ double chiblp=1.e100, chiblb = 1.e100;
37
+ int mn;
38
+ char final[2000],stringa[1024];
39
+ int flag,nmod,ngoodmod,*filter,np;
40
+ FILE *f,*g;
41
+ bumper* bumperlist=0, * scanbumper, * scanbumper2;
42
+
43
+
44
+ printf("******************************************\n");
45
+ printf("********** Finalizer *********\n");
46
+ printf("******************************************\n\n\n");
47
+ printf("This program proposes a final interpretation for the event\n\n");
48
+
49
+ // Directory preliminaries. Reads event name from arguments.
50
+
51
+ auto exedir = current_path();
52
+
53
+ if (argc > 1) {
54
+ strcpy(eventname, argv[1]);
55
+ }
56
+ else {
57
+ printf("\n\nEvent name? ");
58
+ scanf("%s", eventname);
59
+ }
60
+
61
+ printf("\n\n- Event: %s\n", eventname);
62
+
63
+ current_path(eventname);
64
+
65
+ if (exists("Nature.txt")) {
66
+ printf("\n\nEvent already finalized");
67
+ return 0;
68
+ }
69
+
70
+ if (exists("ini")) {
71
+ current_path("ini");
72
+ f = fopen("Finalizer.ini", "r");
73
+ if (f != 0) {
74
+ printf("\n\n- Reading options in Finalizer.ini");
75
+ while (!feof(f)) {
76
+ int red = fscanf(f, "%s %s %lf", command, buffer, &value);
77
+ if (red < 1) {
78
+ command[0] = 0;
79
+ //if (red != 0) {
80
+ // printf("\n\n!!! Bad command in Reader.ini");
81
+ // return -1;
82
+ //};
83
+ }
84
+ //if (strcmp(command, "maxmodels") == 0) {
85
+ // maxmodels = value;
86
+ //}
87
+
88
+ }
89
+ fclose(f);
90
+ }
91
+ else {
92
+ printf("\n\n- Default options:");
93
+ }
94
+ }
95
+
96
+ /*printf("\nNumber of sigmas used to declare overlap between two models: %lf", supfac);
97
+ printf("\nNumber of sigmas in chi square distribution for model acceptance: %lf", chifac);
98
+ printf("\nMaximum number of models reported: %d", maxmodels);*/
99
+
100
+
101
+ // Read curve to fit
102
+
103
+ current_path(eventname);
104
+
105
+ printf("\n\nReading data\n");
106
+
107
+ f=fopen("LCToFit.txt","r");
108
+ fscanf(f,"%d",&np);
109
+ filter=(int *) malloc(sizeof(int)*np);
110
+ satel=(int *) malloc(sizeof(int)*np);
111
+ t=(double *) malloc(sizeof(double)*np);
112
+ y=(double *) malloc(sizeof(double)*np);
113
+ w=(double *) malloc(sizeof(double)*np);
114
+
115
+ nfil=1;
116
+ for(int i=0;i<np;i++){
117
+ fscanf(f,"%d %lf %lf %lf %d",&(filter[i]),&(t[i]),&(y[i]),&(w[i]),&(satel[i]));
118
+ if((i!=0)&&(filter[i]!=filter[i-1])){
119
+ nfil++;
120
+ }
121
+ w[i]=1/(w[i]);
122
+ }
123
+ fclose(f);
124
+
125
+
126
+ pr=(double *) malloc(sizeof(double)*(12+2*nfil));
127
+ sigmapr = (double *)malloc(sizeof(double)*(12 + 2 * nfil));
128
+
129
+ // Calculate flat chi square
130
+
131
+ c0 = c1=c2=cflat=0;
132
+ nfil = 0;
133
+ for (int i = 0; i < np; i++) {
134
+ if (filter[i] == nfil) {
135
+ c0 += w[i] * w[i];
136
+ c1 += y[i] * w[i] * w[i];
137
+ c2 += y[i] * y[i] * w[i] * w[i];
138
+ }
139
+ else {
140
+ nfil++;
141
+ cflat += c2 - c1 * c1 / c0;
142
+ c0 = c1 = c2 = 0;
143
+ }
144
+ }
145
+ nfil++;
146
+ cflat += c2 - c1 * c1 / c0;
147
+
148
+ // Compare all models and draw conclusions
149
+
150
+ printf("\n\n********************");
151
+ printf("\n Finalization ");
152
+ printf("\n********************\n");
153
+
154
+ current_path(eventname);
155
+
156
+ auto selectmodelsname = path("FinalModels");
157
+ create_directory(selectmodelsname);
158
+
159
+
160
+ g=fopen("Nature.txt","w");
161
+
162
+ current_path("Models");
163
+
164
+ // Load chi square of best models of each type
165
+
166
+ printf("\n- Reading models\n\n");
167
+
168
+ nmod = 0;
169
+ for (int icat = 0; icat < ncategories; icat++) {
170
+ chis[icat] = 1.e100;
171
+ auto searchstring = regex(modelcodes[icat] + "[0-9]*-[0-9]*.txt");
172
+ for (auto const& itr : directory_iterator(".")) {
173
+ string curfile = (itr).path().filename().string();
174
+ if (regex_match(curfile, searchstring)) {
175
+ strcpy(filename, curfile.c_str());
176
+ f = fopen(filename, "r");
177
+ for (int j = 0; j < npss[icat] + 2 * nfil; j++) {
178
+ fscanf(f, "%le", &(pr[j]));
179
+ }
180
+ fscanf(f, "%le", &(c0));
181
+ fclose(f);
182
+
183
+ if (c0 < chis[icat]) chis[icat] = c0;
184
+
185
+ if (modelcodes[icat][0] == 'L') {
186
+ if (pr[1] <= 0.03) {
187
+ if (c0 < chiblp) chiblp = c0;
188
+ }
189
+ else {
190
+ if (c0 < chiblb) chiblb = c0;
191
+ }
192
+ }
193
+ if (nmod) {
194
+ if (c0 < bumperlist->Amp) {
195
+ scanbumper = bumperlist;
196
+ bumperlist = new bumper(pr, npss[icat]);
197
+ strcpy(bumperlist->modelcode, (char*)(itr).path().filename().string().c_str());
198
+ bumperlist->il = icat;
199
+ bumperlist->Amp = c0;
200
+ bumperlist->next = scanbumper;
201
+ }
202
+ else {
203
+ scanbumper = bumperlist;
204
+ while ((scanbumper->next) && (scanbumper->next->Amp < c0)) scanbumper = scanbumper->next;
205
+ scanbumper2 = new bumper(pr, npss[icat]);
206
+ strcpy(scanbumper2->modelcode, (char*)(itr).path().filename().string().c_str());
207
+ scanbumper2->il = icat;
208
+ scanbumper2->Amp = c0;
209
+ scanbumper2->next = scanbumper->next;
210
+ scanbumper->next = scanbumper2;
211
+ }
212
+ }
213
+ else {
214
+ bumperlist = new bumper(pr, npss[icat]);
215
+ strcpy(bumperlist->modelcode, (char*)(itr).path().filename().string().c_str());
216
+ bumperlist->il = icat;
217
+ bumperlist->Amp = c0;
218
+ }
219
+ nmod++;
220
+ }
221
+ }
222
+ printf("%s: ", modelcodes[icat].c_str());
223
+ fprintf(g, "%s: ", modelcodes[icat].c_str());
224
+ if (chis[icat] < 1.e99) {
225
+ printf("%lf\n", chis[icat]);
226
+ fprintf(g,"%lf\n", chis[icat]);
227
+ }
228
+ else {
229
+ printf("N/A\n");
230
+ fprintf(g,"N/A\n");
231
+ }
232
+ }
233
+
234
+
235
+ //// Check that there are no weird chi squares
236
+ //for (int i = 0; i<9; i++) {
237
+ // if (chis[i]<np/2) {
238
+ // chis[i] = 1.e100;
239
+ // }
240
+ //}
241
+
242
+ // Minimum chi square
243
+
244
+ mn=0;
245
+ cmin=bumperlist->Amp;
246
+
247
+ thsigma = cmin + cmin / np * sqrt(2 * np);
248
+ for (int i = 0; i < 10; i++) {
249
+ thrs[i] *= cmin / np;
250
+ }
251
+
252
+ if (chiblp < 1.e99) {
253
+ printf("\nBestPlanetary: %lf", chiblp);
254
+ fprintf(g, "\nBestPlanetary: %lf", chiblp);
255
+ }
256
+ else {
257
+ printf("\nBestPlanetary: N/A");
258
+ fprintf(g, "\nBestPlanetary: N/A");
259
+ }
260
+
261
+ if (chiblb < 1.e99) {
262
+ printf("\nBestBinary: %lf\n\n", chiblb);
263
+ fprintf(g, "\nBestBinary: %lf\n\n", chiblb);
264
+ }
265
+ else {
266
+ printf("\nBestBinary: N/A\n\n");
267
+ fprintf(g, "\nBestBinary: N/A\n\n");
268
+ }
269
+
270
+ if (chiblb + thsigma - cmin < chiblp) {
271
+ strcpy(stringa, "Binary lens");
272
+ }
273
+ else {
274
+ if (chiblp + thsigma - cmin < chiblb) {
275
+ strcpy(stringa, "Planetary lens");
276
+ }
277
+ else {
278
+ strcpy(stringa, "Binary or Planetary lens");
279
+ }
280
+ }
281
+
282
+ // Making assessment. Each category is tested against nested ones
283
+
284
+ if (chis[1] > chis[0] - thrs[npss[1] - npss[0]]) {
285
+ chis[1] = 1.e100;
286
+ }
287
+ if(chis[2] > chis[0] - thrs[npss[2] - npss[0]]) {
288
+ chis[2] = 1.e100;
289
+ }
290
+ if (chis[3] > chis[0] - thrs[npss[3] - npss[0]] || chis[3] > chis[1] - thrs[npss[3] - npss[1]] || chis[3] > chis[2] - thrs[npss[3] - npss[2]]) {
291
+ chis[3] = 1.e100;
292
+ }
293
+ if (chis[4] > chis[0] - thrs[npss[4] - npss[0]]) {
294
+ chis[4] = 1.e100;
295
+ }
296
+ if (chis[5] > chis[0] - thrs[npss[5] - npss[0]] || chis[5] > chis[1] - thrs[npss[5] - npss[1]] || chis[5] > chis[4] - thrs[npss[5] - npss[4]]) {
297
+ chis[5] = 1.e100;
298
+ }
299
+ if (chis[6] > chis[0] - thrs[npss[6] - npss[0]] || chis[6] > chis[1] - thrs[npss[6] - npss[1]] || chis[6] > chis[4] - thrs[npss[6] - npss[4]] || chis[6] > chis[5] - thrs[npss[6] - npss[5]]) {
300
+ chis[6] = 1.e100;
301
+ }
302
+
303
+ // If more complicated category has survived, all nested categories are removed
304
+
305
+ if (chis[6] < 1.e99) {
306
+ chis[5] = chis[4] = chis[1] = chis[0] = 1.e100;
307
+ }
308
+ if (chis[5] < 1.e99) {
309
+ chis[4] = chis[1] = chis[0] = 1.e100;
310
+ }
311
+ if (chis[4] < 1.e99) {
312
+ chis[0] = 1.e100;
313
+ }
314
+ if (chis[3] < 1.e99) {
315
+ chis[2] = chis[1] = chis[0] = 1.e100;
316
+ }
317
+ if (chis[2] < 1.e99) {
318
+ chis[0] = 1.e100;
319
+ }
320
+ if (chis[1] < 1.e99) {
321
+ chis[0] = 1.e100;
322
+ }
323
+
324
+ // Models of discarded categories or with chi square higher than threshold are removed
325
+
326
+ for (scanbumper = bumperlist; scanbumper; scanbumper = scanbumper->next) {
327
+ if (scanbumper->Amp > thsigma || chis[scanbumper->il] > 1.e99) scanbumper->modelcode[0] = 'N';
328
+ }
329
+
330
+ // Counting good models
331
+
332
+ ngoodmod = 0;
333
+ for (scanbumper = bumperlist; scanbumper; scanbumper = scanbumper->next) {
334
+ if (scanbumper->modelcode[0] != 'N') ngoodmod++;
335
+ }
336
+
337
+ // Decide designation
338
+
339
+ if (cflat < thsigma) {
340
+ strcpy(final, "Flat");
341
+ }else{
342
+ if (cmin > np * failthr) {
343
+ strcpy(final, "Uncertain: variable or failed");
344
+ }
345
+ else {
346
+ strcpy(final, "Successful: ");
347
+ flag = 0;
348
+ for (int icat = 0; icat < ncategories; icat++) {
349
+ for (scanbumper = bumperlist; scanbumper; scanbumper = scanbumper->next) {
350
+ if (scanbumper->il == icat && scanbumper->modelcode[0] != 'N') {
351
+ if (flag > 0) {
352
+ strncat(final, " or ", 60);
353
+ }
354
+ else {
355
+ flag = 1;
356
+ }
357
+ if (icat >= 4) strncat(final, stringa, 60);
358
+ strncat(final, modelnames[icat].c_str(), 60);
359
+ break;
360
+ }
361
+ }
362
+ }
363
+ }
364
+ }
365
+
366
+ printf("%s\n", final);
367
+ // Writing file Nature.txt with the conclusion drawn before
368
+ fprintf(g,"%s\n",final);
369
+
370
+ printf("----\n");
371
+ fprintf(g,"----\n");
372
+
373
+
374
+ printf("Number of alternative models: %d\n\nchisquare model\n", ngoodmod);
375
+ fprintf(g, "Number of alternative models: %d\n\nchisquare model\n", ngoodmod);
376
+
377
+
378
+ // Writing the names of the files containing alternative models in Nature.txt
379
+ current_path("..");
380
+ for (scanbumper = bumperlist; scanbumper;scanbumper=scanbumper->next) {
381
+ if (scanbumper->modelcode[0] != 'N') {
382
+ fprintf(g, "%lf %s\n", scanbumper->Amp, scanbumper->modelcode);
383
+ printf("%lf %s", scanbumper->Amp, scanbumper->modelcode);
384
+ copy_file(path("Models") / path(string(scanbumper->modelcode)), path("FinalModels") / path(string(scanbumper->modelcode)),copy_options::overwrite_existing);
385
+ }
386
+ }
387
+ fclose(g);
388
+
389
+
390
+
391
+ printf("\n\n- Done");
392
+
393
+ // Sleep(5000l);
394
+
395
+ scanbumper = bumperlist;
396
+ while (scanbumper) {
397
+ scanbumper2 = scanbumper->next;
398
+ delete scanbumper;
399
+ scanbumper = scanbumper2;
400
+ }
401
+
402
+ free(pr);
403
+ free(sigmapr);
404
+ free(filter);
405
+ free(satel);
406
+ free(t);
407
+ free(y);
408
+ free(w);
409
+
410
+
411
+ return 0;
412
+ }