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,1277 @@
1
+ // LevMarFit.cpp
2
+ // Implementation of all methods in LevMarFit.h for Levenberg-Marquardt fitting
3
+
4
+ #define _CRT_SECURE_NO_WARNINGS
5
+ #define _USE_MATH_DEFINES
6
+ #include "LevMarFit.h"
7
+ #include "bumper.h"
8
+ #include <VBBinaryLensingLibrary.h>
9
+ #include <stdio.h>
10
+ #include <time.h>
11
+ #include <stdlib.h>
12
+ #include <string.h>
13
+ #include <math.h>
14
+ #include <malloc.h>
15
+ #include <regex>
16
+ #include <filesystem>
17
+
18
+ using namespace std;
19
+ using namespace std::filesystem;
20
+
21
+
22
+
23
+ int nlc = 5; // Number of models to be calculated from the same initial condition using the bumper method
24
+ int maxsteps = 50; // Maximum number of steps in each fit
25
+ double maxtime=600.0; // Maximum time in seconds for total execution
26
+ double bumperpower = 2.0; // Repulsion factor of bumpers
27
+ double maxbumpcount = 25;
28
+
29
+
30
+ const double epsilon = 1.e-100;
31
+
32
+ LevMar::LevMar(int argc,char *argv[]){
33
+ printf("******************************************\n");
34
+ printf("************* LevMar **********\n");
35
+ printf("******************************************\n\n\n");
36
+ printf("This program fits an event from specific initial conditions\n\n");
37
+ error=0;
38
+ // Setting default values
39
+ Tol=1.e-2;
40
+ VBBL =new VBBinaryLensing;
41
+ VBBL->Tol = Tol;
42
+ VBBL->RelTol = 0.001;
43
+ VBBL->parallaxsystem = 1;
44
+
45
+ ReadFiles(argc,argv);
46
+
47
+ }
48
+
49
+ LevMar::~LevMar(){
50
+ delete VBBL;
51
+ if(error<9){
52
+ free(t);
53
+ free(y);
54
+ free(w);
55
+ free(satel);
56
+ free(filter);
57
+ free(delta);
58
+ free(maxdelta);
59
+ free(Curv);
60
+ free(A);
61
+ free(B);
62
+ free(B0);
63
+ free(Cov);
64
+ free(fb);
65
+ free(pr);
66
+ free(prn);
67
+ free(errs);
68
+ free(sumy);
69
+ free(sumy2);
70
+ free(sumsigma);
71
+ free(sumfy);
72
+ free(sumf);
73
+ free(sumf2);
74
+ free(limbdarks);
75
+ free(dFdp);
76
+ for(int i=0;i<nps;i++){
77
+ free(Gr[i]);
78
+ }
79
+ free(Gr);
80
+
81
+
82
+ bumper *scanbumper=bumperlist,*scanbumper2;
83
+ while(scanbumper){
84
+ scanbumper2=scanbumper->next;
85
+ delete scanbumper;
86
+ scanbumper=scanbumper2;
87
+ }
88
+
89
+
90
+ scanbumper=stepchain;
91
+ while(scanbumper){
92
+ scanbumper2=scanbumper->next;
93
+ delete scanbumper;
94
+ scanbumper=scanbumper2;
95
+ }
96
+
97
+ }
98
+ if(error<10){
99
+ free(sigmapr);
100
+ free(leftlim);
101
+ free(rightlim);
102
+ }
103
+ }
104
+
105
+ void LevMar::ReadFiles(int argc,char *argv[]){
106
+ FILE *f;
107
+ char buffer[3200], initcondfile[256];
108
+ char command[256];
109
+ double value;
110
+
111
+ try{
112
+
113
+ // Reading directory and event information
114
+
115
+ exedir = current_path();
116
+ strcpy(satellitedir, exedir.string().c_str());
117
+
118
+ if (argc > 2) {
119
+ strcpy(eventname, argv[1]);
120
+ strcpy(outdir, argv[2]);
121
+ if (argc > 3) {
122
+ strcpy(satellitedir, argv[3]);
123
+ }
124
+ }
125
+ else {
126
+ printf("\n\nEvent name? ");
127
+ scanf("%s", eventname);
128
+ //sprintf(eventname, "WDC10193");
129
+ }
130
+
131
+ printf("\n\n- Event: %s\n", eventname);
132
+
133
+ current_path(eventname);
134
+
135
+ //if(argc>2){
136
+ // strcpy(eventname,argv[1]);
137
+ // strcpy(outdir,argv[2]);
138
+ //}else{
139
+ // *eventname = 0;
140
+ // f = fopen("Model.ini", "r");
141
+ // fscanf(f, "%s", outdir);
142
+ // fclose(f);
143
+ // nlc = 1;
144
+ //}
145
+
146
+
147
+ /* Reading coordinates of the model */
148
+
149
+ if (exists("ini")) {
150
+ current_path("ini");
151
+ f = fopen("LevMar.ini", "r");
152
+ if (f != 0) {
153
+ printf("\n\n- Reading options in LevMar.ini");
154
+ while (!feof(f)) {
155
+ int red = fscanf(f, "%s %s %lf", command, buffer, &value);
156
+ if (red < 1) {
157
+ command[0] = 0;
158
+ //if (red != 0) {
159
+ // printf("\n\n!!! Bad command in Reader.ini");
160
+ // return -1;
161
+ //};
162
+ }
163
+ if (strcmp(command, "nfits") == 0) {
164
+ nlc = (int) value;
165
+ }
166
+ if (strcmp(command, "maxsteps") == 0) {
167
+ maxsteps = (int) value;
168
+ }
169
+ if (strcmp(command, "timelimit") == 0) {
170
+ maxtime = value;
171
+ }
172
+ if (strcmp(command, "bumperpower") == 0) {
173
+ bumperpower = value;
174
+ }
175
+ }
176
+ fclose(f);
177
+ }
178
+ else {
179
+ printf("\n\n- Default options:");
180
+ }
181
+ }
182
+
183
+ current_path(eventname);
184
+ current_path("Data");
185
+
186
+ auto searchstring = regex(".*\\.coordinates");
187
+ for (auto const& itr : directory_iterator(".")) {
188
+ string curfile = (itr).path().filename().string();
189
+ if (regex_match(curfile, searchstring)) {
190
+ VBBL->SetObjectCoordinates((char*) curfile.c_str(), satellitedir);
191
+ printf("\n- Coordinates set.");
192
+ break;
193
+ }
194
+ }
195
+
196
+ current_path(eventname);
197
+
198
+ // Establishing model to be fit
199
+ strcpy(modelcode,outdir);
200
+ switch(modelcode[0]){
201
+ case 'P':
202
+ if(modelcode[1]=='X'){
203
+ model=&(VBBinaryLensing::ESPLLightCurveParallax);
204
+ nps=6;
205
+ double presigmapr[] ={.5,.5,5.,4.6,1,1};
206
+ double preleftlim[]={-13.,-6.9,-10.e100,-11.5,-10.,-10.};
207
+ double prerightlim[]={.7,6.9,10.e100,0.0,10.,10.};
208
+ strcpy(initcondfile, "InitCondPX.txt");
209
+ InitCond(presigmapr,preleftlim,prerightlim);
210
+ pr[1]=log(pr[1]);
211
+ pr[3]=log(pr[3]);
212
+ PrintOut=&LevMar::PrintOutPX;
213
+ PrintFile=&LevMar::PrintFilePX;
214
+ }else{
215
+ model=&(VBBinaryLensing::ESPLLightCurve);
216
+ nps=4;
217
+ double presigmapr[] ={.5,.5,5.,4.6};
218
+ double preleftlim[]={-13.,-6.9,-10.e100,-11.5};
219
+ double prerightlim[]={.7,6.9,10.e100,0.0};
220
+ InitCond(presigmapr, preleftlim, prerightlim);
221
+ pr[0]=log(pr[0]);
222
+ pr[1]=log(pr[1]);
223
+ pr[3]=log(pr[3]);
224
+ PrintOut=&LevMar::PrintOutPS;
225
+ PrintFile=&LevMar::PrintFilePS;
226
+ }
227
+ current_path(exedir);
228
+ current_path("..");
229
+ current_path("data");
230
+ VBBL->LoadESPLTable("ESPL.tbl");
231
+ current_path(eventname);
232
+ break;
233
+ case 'B':
234
+ if(modelcode[1]=='O'){
235
+ model=&(VBBinaryLensing::BinSourceSingleLensXallarap);
236
+ nps=10;
237
+ double presigmapr[] ={1,1,1,15,3,3,1,3,6,3};
238
+ double preleftlim[]={-3.,-1.e100,-6.9,-11.5,-3.,-3.,0,-3,-6,-4.6};
239
+ double prerightlim[]={3.,1.e100,6.9,0.,3.,3.,1,3,6,1,+4.6};
240
+ InitCond(presigmapr, preleftlim, prerightlim);
241
+ pr[2]=log(pr[2]);
242
+ pr[3]=log(pr[3]);
243
+ pr[9] = log(pr[9]);
244
+ PrintOut=&LevMar::PrintOutBO;
245
+ PrintFile=&LevMar::PrintFileBO;
246
+ }else{
247
+ model=&(VBBinaryLensing::BinSourceExtLightCurve);
248
+ nps=7;
249
+ double presigmapr[] ={.1,.4,1,1,1,1,4.6};
250
+ double preleftlim[]={-6.9,-11.5,0.,0.,-10.e100,-10.e100,-11.5};
251
+ double prerightlim[]={6.9,11.5,3.,3.,10.e100,10.e100,0.0};
252
+ InitCond(presigmapr, preleftlim, prerightlim);
253
+ pr[0]=log(pr[0]);
254
+ pr[1]=log(pr[1]);
255
+ pr[6] = log(pr[6]);
256
+ PrintOut=&LevMar::PrintOutBS;
257
+ PrintFile=&LevMar::PrintFileBS;
258
+ }
259
+ current_path(exedir);
260
+ current_path("..");
261
+ current_path("data");
262
+ VBBL->LoadESPLTable("ESPL.tbl");
263
+ current_path(eventname);
264
+ break;
265
+ case 'L':
266
+ if(modelcode[1]=='X'){
267
+ model=&(VBBinaryLensing::BinaryLightCurveParallax);
268
+ nps=9;
269
+ double presigmapr[] ={.1,.4,.1,.1,4.6,.1,1.,1.,1.};
270
+ double preleftlim[]={-4.0,-11.5,-3.,-12.56,-11.5,-6.9,-10.e100,-3.,-3.};
271
+ double prerightlim[]={3.0,11.5,3.,12.56,-2.5,7.6,10.e100,3.,3.};
272
+ InitCond(presigmapr, preleftlim, prerightlim);
273
+ pr[0]=log(pr[0]);
274
+ pr[1]=log(pr[1]);
275
+ pr[4]=log(pr[4]);
276
+ pr[5]=log(pr[5]);
277
+ PrintOut=&LevMar::PrintOutLX;
278
+ PrintFile=&LevMar::PrintFileLX;
279
+ }else{
280
+ if(modelcode[1]=='O'){
281
+ model=&(VBBinaryLensing::BinaryLightCurveOrbital);
282
+ nps=12;
283
+ double presigmapr[]={1.,2.,1.,5.,15.6,2.,10.,3.,3.,1.,1.,3.};
284
+ double preleftlim[]={-4.0,-11.5,-3.,-12.56,-11.5,-6.9,-10.e100,-3.,-3.,-1,-1,1.e-7};
285
+ double prerightlim[]={3.0,11.5,3.,12.56,-2.5,7.6,10.e100,3.,3.,1,1,1};
286
+ InitCond(presigmapr, preleftlim, prerightlim);
287
+ pr[0]=log(pr[0]);
288
+ pr[1]=log(pr[1]);
289
+ pr[4]=log(pr[4]);
290
+ pr[5]=log(pr[5]);
291
+ PrintOut=&LevMar::PrintOutLO;
292
+ PrintFile=&LevMar::PrintFileLO;
293
+ }else{
294
+ if (modelcode[1] == 'P') {
295
+ model = &(VBBinaryLensing::BinaryLightCurveOrbital);
296
+ nps = 12;
297
+ double presigmapr[] = { 1.,2.,1.,5.,15.6,2.,10.,.0001,.0001,1.,1.,3. };
298
+ double preleftlim[] = { -4.0,-11.5,-3.,-12.56,-11.5,-6.9,-10.e100,-.0001,-.0001,-1,-1,1.e-7 };
299
+ double prerightlim[] = { 3.0,11.5,3.,12.56,-2.5,7.6,10.e100,.0001,.0001,1,1,1 };
300
+ InitCond(presigmapr, preleftlim, prerightlim);
301
+ pr[0] = log(pr[0]);
302
+ pr[1] = log(pr[1]);
303
+ pr[4] = log(pr[4]);
304
+ pr[5] = log(pr[5]);
305
+ pr[7] = 0.;
306
+ pr[8] = 0.;
307
+ PrintOut = &LevMar::PrintOutLO;
308
+ PrintFile = &LevMar::PrintFileLO;
309
+ }
310
+ else {
311
+ if (modelcode[1] == 'K') {
312
+ model = &(VBBinaryLensing::BinaryLightCurveKepler);
313
+ nps = 14;
314
+ double presigmapr[] = { 1.,2.,1.,5.,15.6,2.,10.,3.,3.,1.,1.,3., 3., 3. };
315
+ double preleftlim[] = { -4.0,-11.5,-3.,-12.56,-11.5,-6.9,-10.e100,-3.,-3.,-1,-1,1.e-7, -10,0.5001 };
316
+ double prerightlim[] = { 3.0,11.5,3.,12.56,-2.5,7.6,10.e100,3.,3.,1,1,1,10,10 };
317
+ InitCond(presigmapr, preleftlim, prerightlim);
318
+ pr[0] = log(pr[0]);
319
+ pr[1] = log(pr[1]);
320
+ pr[4] = log(pr[4]);
321
+ pr[5] = log(pr[5]);
322
+ PrintOut = &LevMar::PrintOutLK;
323
+ PrintFile = &LevMar::PrintFileLK;
324
+ }
325
+ else {
326
+ model = &(VBBinaryLensing::BinaryLightCurve);
327
+ nps = 7;
328
+ double presigmapr[] = { .1,.4,.1,.1,4.6,.1,1. };
329
+ double preleftlim[] = { -4.0,-11.5,-3.,-12.56,-11.5,-6.9,-10.e100 };
330
+ double prerightlim[] = { 3.0,11.5,3.,12.56,-2.5,7.6,10.e100};
331
+ InitCond(presigmapr, preleftlim, prerightlim);
332
+ pr[0] = log(pr[0]);
333
+ pr[1] = log(pr[1]);
334
+ pr[4] = log(pr[4]);
335
+ pr[5] = log(pr[5]);
336
+ PrintOut = &LevMar::PrintOutLS;
337
+ PrintFile = &LevMar::PrintFileLS;
338
+ }
339
+ }
340
+ }
341
+ }
342
+ break;
343
+ }
344
+ printf("\n- Model: %s",modelcode);
345
+
346
+ }catch(...){
347
+ error=10;
348
+ }
349
+
350
+ if(!error){
351
+ // Reading Light Curve
352
+ ReadCurve();
353
+ }
354
+
355
+ }
356
+
357
+ int LevMar::InitCond(double* presigmapr, double* preleftlim, double* prerightlim) {
358
+ char buffer[3200],initcondfile[256];
359
+ sigmapr = (double*)malloc(sizeof(double) * nps);
360
+ leftlim = (double*)malloc(sizeof(double) * nps);
361
+ rightlim = (double*)malloc(sizeof(double) * nps);
362
+ pr = (double*)malloc(sizeof(double) * nps);
363
+ sprintf(initcondfile, "InitCond%c%c.txt", modelcode[0],modelcode[1]);
364
+ current_path("InitCond");
365
+ FILE *f = fopen(initcondfile, "r");
366
+ int npeaks, ninit, incond;
367
+ fscanf(f, "%d %d", &npeaks, &ninit);
368
+ incond = atoi(outdir + 2);
369
+ if (incond >= ninit) nps=nps/0;
370
+ fgets(buffer, 3200, f);
371
+ for (int i = 0; i < npeaks + incond; i++) {
372
+ fgets(buffer,3200,f);
373
+ }
374
+ for (int i = 0; i < nps; i++) {
375
+ sigmapr[i] = presigmapr[i];
376
+ leftlim[i] = preleftlim[i];
377
+ rightlim[i] = prerightlim[i];
378
+ fscanf(f, "%lg", &(pr[i]));
379
+ if (rightlim[i] > 1.e90) {
380
+ rightlim[i] = pr[i] + 300.0;
381
+ leftlim[i] = pr[i] - 300.0;
382
+ }
383
+ }
384
+ fclose(f);
385
+ current_path(eventname);
386
+ return 0;
387
+ }
388
+
389
+ void LevMar::ReadCurve(){
390
+ FILE *f;
391
+ int k;
392
+ double *normfacs;
393
+
394
+ try{
395
+ printf("\n\n- Reading data \n");
396
+
397
+ f=fopen("LCToFit.txt","r");
398
+ fscanf(f,"%d",&np);
399
+ filter=(int *) malloc(sizeof(int)*np);
400
+ t=(double *) malloc(sizeof(double)*np);
401
+ y=(double *) malloc(sizeof(double)*np);
402
+ w=(double *) malloc(sizeof(double)*np);
403
+ satel=(int *) malloc(sizeof(int)*np);
404
+ delta=(double *) malloc(sizeof(double)*nps);
405
+ maxdelta=(double *) malloc(sizeof(double)*nps);
406
+ B=(double *) malloc(sizeof(double)*nps);
407
+ B0=(double *) malloc(sizeof(double)*(nps));
408
+ A=(double *) malloc(sizeof(double)*nps*nps);
409
+ Curv=(double *) malloc(sizeof(double)*nps*nps);
410
+ Cov=(double *) malloc(sizeof(double )*nps*nps);
411
+ fb=(double *) malloc(sizeof(double)*np*(nps+1));
412
+
413
+
414
+ nfil=1;
415
+ for(int i=0;i<np;i++){
416
+ fscanf(f,"%d %lf %lf %lf %d",&(filter[i]),&(t[i]),&(y[i]),&(w[i]),&(satel[i]));
417
+ if((i!=0)&&(filter[i]>filter[i-1])){
418
+ nfil++;
419
+ }
420
+ w[i]=1/(w[i]);
421
+ }
422
+ fclose(f);
423
+
424
+ sumf=(double *) malloc(sizeof(double)*nfil);
425
+ sumf2=(double *) malloc(sizeof(double)*nfil);
426
+ sumfy=(double *) malloc(sizeof(double)*nfil);
427
+ sumy=(double *) malloc(sizeof(double)*nfil);
428
+ sumy2=(double *) malloc(sizeof(double)*nfil);
429
+ sumsigma=(double *) malloc(sizeof(double)*nfil);
430
+ pr=(double *) realloc(pr,sizeof(double)*(nps+2*nfil));
431
+ prn=(double *) malloc(sizeof(double)*(nps+2*nfil));
432
+ errs=(double *) malloc(sizeof(double)*(nps+2*nfil));
433
+ dFdp=(double *) malloc(sizeof(double )*2*nfil*nps);
434
+
435
+ Gr=(double **) malloc(sizeof(double *)*nps);
436
+ for(int i=0;i<nps;i++){
437
+ Gr[i]=(double *) malloc(sizeof(double)*np);
438
+ }
439
+
440
+ current_path("Data");
441
+
442
+ // If Normalization.txt is present, use numbers therein to normalize datasets.
443
+
444
+ normfacs = (double *)malloc(sizeof(double)*nfil);
445
+ for (int i = 0; i < nfil; i++) {
446
+ normfacs[i] = 1.;
447
+ }
448
+ if (exists("Normalizations.txt")) {
449
+ f = fopen("Normalizations.txt", "r");
450
+ for (int i = 0; i < nfil; i++) {
451
+ fscanf(f, "%lf", &normfacs[i]);
452
+ printf("\nNormalization %d: %lf", i, normfacs[i]);
453
+ }
454
+ fclose(f);
455
+ }
456
+
457
+ k = 0;
458
+ sumsigma[k] = sumy[k] = sumy2[k] = 0;
459
+ for (int i = 0; i<np; i++) {
460
+ w[i] /= normfacs[filter[i]];
461
+ if ((i != 0) && (filter[i]>filter[i - 1])) {
462
+ k++;
463
+ sumsigma[k] = sumy[k] = sumy2[k] = 0;
464
+ }
465
+ sumsigma[k] += w[i] * w[i];
466
+ sumy[k] += w[i] * w[i] * y[i];
467
+ sumy2[k] += w[i] * w[i] * y[i] * y[i];
468
+ }
469
+
470
+ for(int i=0;i<nps;i++){
471
+ maxdelta[i]=sigmapr[i];
472
+ }
473
+
474
+ // If LimbDarkening.txt is present, use number therein for linear limb darkening coefficient
475
+ limbdarks = (double *)malloc(sizeof(double)*nfil);
476
+ for (int i = 0; i < nfil; i++) {
477
+ limbdarks[i] = 0;
478
+ }
479
+ if(exists("LimbDarkening.txt")){
480
+ f = fopen("LimbDarkening.txt", "r");
481
+ for (int i = 0; i < nfil; i++) {
482
+ fscanf(f, "%lf", &limbdarks[i]);
483
+ printf("\nLimbDarkening %d: %lf", i, limbdarks[i]);
484
+ }
485
+ fclose(f);
486
+ }
487
+ current_path("..");
488
+
489
+ //jfile=_findfirst("ZeroBlending.txt",&str2file); // Option for fitting with zero blending, to be elaborated
490
+ //if(jfile!=-1){
491
+
492
+ // f=fopen("FilterToData.txt","r");
493
+ // k=0;
494
+ // while(k>=0){
495
+ // k++;
496
+ // fscanf(f,"%s",filnum);
497
+ // if(filnum[0]=='O'){
498
+ // OGLE=k;
499
+ // k=-1;
500
+ // }
501
+ // if(feof(f)) k=-1;
502
+ // }
503
+ // fclose(f);
504
+ // OGLE--;
505
+ //}else{
506
+ // OGLE=-1;
507
+ //}
508
+
509
+ }catch(...){
510
+ error=9;
511
+ }
512
+ free(normfacs);
513
+
514
+ }
515
+
516
+ void LevMar::Run(){
517
+ FILE *f;
518
+ int il,k,ichi,flag,ilam,bumpnum,bumpcounter;
519
+ double minchi,bestchi,c1,c0,oldlambda,lambda,inclambda,fac,fac2;
520
+ bumper *scanbumper,*scanbumper2;
521
+
522
+ if(!error){
523
+ try{
524
+ // Initializing step chain and bumper list
525
+
526
+ printf("\n\n- Initializing step chain \n");
527
+
528
+ stepchain=new bumper(pr,nps);
529
+ laststep=stepchain;
530
+ bumperlist=0;
531
+
532
+ // Going to right directory
533
+
534
+ if(!exists("PreModels")) create_directory("PreModels");
535
+ current_path("PreModels");
536
+ if (!exists(outdir)) create_directory(outdir);
537
+ current_path(outdir);
538
+ il = 0;
539
+ if (f = fopen("nlc.dat", "r")) {
540
+ int i1, i2;
541
+ fscanf(f, "%d %d", &i1, &i2);
542
+ if (i2 == i1 + 1) {
543
+ il = nlc;
544
+ }
545
+ fclose(f);
546
+ }
547
+ if(il==0){
548
+ // Removing all existing files
549
+ auto searchstring = regex(".*");
550
+ for (auto const& itr : directory_iterator(".")) {
551
+ string curfile = (itr).path().filename().string();
552
+ if (is_regular_file(curfile)) {
553
+ remove(curfile);
554
+ }
555
+ }
556
+ // nlc.dat contains the number of models calculated so far and the total number to be computed
557
+ f = fopen("nlc.dat", "w");
558
+ fprintf(f, "%d %d", -1, nlc);
559
+ fclose(f);
560
+ // minchi.dat contains the best chi square found so far in all kinds of models for this event
561
+ minchi = bestchi = 1.e100;
562
+ }
563
+
564
+
565
+ time_t ltime,rtime;
566
+ time(&ltime);
567
+
568
+ // Calculate nlc models starting from the initial condition
569
+ // il is the number of the current model being calculated
570
+ while(il<nlc){
571
+ printf("\n*************************************");
572
+ printf("\n************ Curve %d *************",il);
573
+ printf("\n*************************************\n");
574
+
575
+ for(int i=0;i<nps;i++){
576
+ pr[i]=(laststep->p0)[i];
577
+ }
578
+ bumpcounter = 0;
579
+
580
+ (this->*PrintOut)(pr);
581
+ /* Chi Squared at initial conditions */
582
+
583
+ c1=ChiSquared(pr);
584
+ printf("\nStarting chi2 = %lf",c1);
585
+
586
+ // Saving to file
587
+ sprintf(filename, "%s-stepchain%d.dat", modelcode, il);
588
+ f = fopen(filename, "a");
589
+ (this->*PrintFile)(f, c1, false);
590
+ fprintf(f, "\n");
591
+ fclose(f);
592
+
593
+ // Initializing the main cycle
594
+
595
+ lambda=3.;
596
+ inclambda=3.;
597
+ bumpnum=1;
598
+ k=1;
599
+ ichi=0;
600
+ // Main fit
601
+ while((ichi<3)&&(lambda<1.e10)&&(k<=maxsteps)){
602
+ c0=c1;
603
+ printf("\nStep %d\n",k++);
604
+
605
+ /* Calculation of the gradient */
606
+
607
+ Grad();
608
+
609
+ // Preparing the matrices A=(Curv + lambda diag(Curv))
610
+ // with Curv = Gr^T Gr.
611
+ // B=Gr (y-f)*w
612
+
613
+ oldlambda=lambda;
614
+ lambda/=inclambda;
615
+
616
+ // Levenberg-Marquardt with parameter lambda
617
+ ilam=0;
618
+ while((c1>=c0)&&(ilam<20)){
619
+ for(int i=0;i<nps;i++){
620
+ for(int j=0;j<nps;j++){
621
+ A[i*nps+j]=Curv[i*nps+j];
622
+ if(i==j){
623
+ A[i*nps+j]+=lambda*Curv[i*nps+i];
624
+ }
625
+ }
626
+ B[i]=B0[i];
627
+ }
628
+
629
+ // Triangularizing the equations A.delta = B
630
+
631
+ for(int i=0;i<nps-1;i++){
632
+ for(int j=i+1;j<nps;j++){
633
+ fac=-A[j*nps+i]/A[i*nps+i];
634
+ for(int m=i+1;m<nps;m++){
635
+ A[j*nps+m]+=A[i*nps+m]*fac;
636
+ }
637
+ B[j]+=B[i]*fac;
638
+ }
639
+ }
640
+
641
+ // Solving for delta
642
+
643
+ for(int i=nps-1;i>=0;i--){
644
+ fac=B[i];
645
+ for(int j=i+1;j<nps;j++){
646
+ fac-=delta[j]*A[i*nps+j];
647
+ }
648
+ delta[i]=fac/A[i*nps+i];
649
+ }
650
+ // If we end up out of bounds, the point is taken at the bound.
651
+ for(int i=0;i<nps;i++){
652
+ // printf("%lf ",delta[i]);
653
+ if(!((delta[i]>0)||(delta[i]<0))){
654
+ delta[i]=0.;
655
+ }
656
+ if(fabs(delta[i])>maxdelta[i]){
657
+ delta[i]*=maxdelta[i]/fabs(delta[i]);
658
+ }
659
+ prn[i]=pr[i]+delta[i];
660
+ if(prn[i]>rightlim[i]){
661
+ prn[i]=(0.99*rightlim[i]+(0.01+ilam)*pr[i])/(1+ilam);
662
+ }else if(prn[i]<leftlim[i]){
663
+ prn[i]= (0.99*leftlim[i] + (0.01 + ilam)*pr[i]) / (1 + ilam);
664
+ }
665
+ }
666
+ // Current parameters, current chi square and current lambda are displayed
667
+ (this->*PrintOut)(prn);
668
+ c1=ChiSquared(prn);
669
+ printf("\nilam= %d lambda= %lf\nc1 = %lf prec=%le",ilam,lambda,c1,Tol);
670
+
671
+ lambda*=inclambda;
672
+ ilam++;
673
+ }
674
+ lambda/=inclambda;
675
+
676
+
677
+ // if new point is better than previous, pr is updated.
678
+ if(ilam<20){
679
+ for(int i=0;i<nps+2*nfil;i++){
680
+ pr[i]=prn[i];
681
+ }
682
+ }
683
+
684
+
685
+ // Bumping mechanism: if point is within the covariance ellipsoid it gets kicked off
686
+ fac=0;
687
+ flag=0;
688
+ while(fac<1 && bumperlist){
689
+ for(scanbumper=bumperlist;scanbumper; scanbumper=scanbumper->next){
690
+ fac=scanbumper->distance(pr);
691
+ if(fac<1){
692
+ printf("\nBumped!");
693
+ bumpcounter++;
694
+ for(int i=0;i<nps;i++){
695
+ fac = 2.0*bumperpower / sqrt(fac);
696
+ prn[i]=pr[i]-fac*scanbumper->dp[i];
697
+ if (prn[i]>rightlim[i]) {
698
+ prn[i] = 0.99*rightlim[i] + 0.01*pr[i];
699
+ }
700
+ else if (prn[i]<leftlim[i]) {
701
+ prn[i] = 0.99*leftlim[i] + 0.01*pr[i];
702
+ }
703
+ pr[i] = prn[i];
704
+ }
705
+ scanbumper->UpdateCurvature(bumperpower);
706
+ flag=1;
707
+ (this->*PrintOut)(pr);
708
+ }
709
+ }
710
+ if (bumpcounter >= maxbumpcount) break;
711
+ }
712
+ if(flag){
713
+ c1=ChiSquared(pr);
714
+ printf("\nc1 = %lf",c1);
715
+ }
716
+ // Add new point to stepchain
717
+ laststep->next=new bumper(pr,nps);
718
+ laststep=laststep->next;
719
+ // Saving to file
720
+ sprintf(filename, "%s-stepchain%d.dat", modelcode ,il);
721
+ f = fopen(filename, "a");
722
+ (this->*PrintFile)(f, c0, false);
723
+ fprintf(f, "\n");
724
+ fclose(f);
725
+
726
+ // If new point's chi square is very close to previous one, ichi is increased.
727
+ // When ichi reaches 3, we declare convergence achieved.
728
+ if(fabs(1-c1/c0)<1.0e-3){
729
+ ichi++;
730
+ }else{
731
+ ichi=0;
732
+ }
733
+ // Time limit is set by maxtime
734
+ // Also close if bumpcounter has reached maxbumpcount
735
+ time(&rtime);
736
+ if(difftime(rtime,ltime)>maxtime || bumpcounter>= maxbumpcount) {
737
+ ichi=3;
738
+ nlc=il;
739
+ printf("\n---- Time limit has been reached. Exiting...");
740
+ }
741
+
742
+
743
+ }
744
+
745
+ if(ichi<4){
746
+ // Final chi square of this model
747
+ c0=c1;//ChiSquared(pr);
748
+ printf("\nFinal chi square = %lf\n",c0);
749
+
750
+ //Check if this is the best model with this initial condition
751
+ if(c0<bestchi) bestchi=c0;
752
+
753
+ //Grad(); // Are these two lines really needed? They seem a repetition
754
+ //Covariance();
755
+
756
+ // Add a new bumper at the position of this model
757
+ scanbumper=bumperlist;
758
+ bumperlist=new bumper(pr,nps);
759
+ bumperlist->next=scanbumper;
760
+ // Calculate covariance matrix
761
+ Grad();
762
+ Covariance();
763
+ bumperlist->SetCurvature(Curv,np/c0);
764
+
765
+
766
+ // Saving the steps done to this model
767
+ // sprintf(filename, "stepchain%d.dat", il);
768
+ //f = fopen(filename, "w");
769
+ //for (scanbumper = stepchain; scanbumper; scanbumper = scanbumper->next) {
770
+ // fprintf(f, "%.16le", scanbumper->p0[0]);
771
+ // for (int j = 1; j < nps; j++) {
772
+ // fprintf(f, " %.16le", scanbumper->p0[j]);
773
+ // }
774
+ // fprintf(f, "\n");
775
+ //}
776
+ //fclose(f);
777
+
778
+ // Finding the last step outside any bumper
779
+ k=1;
780
+ for(scanbumper=stepchain;scanbumper&&(scanbumper->next);scanbumper=scanbumper->next){
781
+ k++;
782
+ fac=1.e100;
783
+ for(scanbumper2=bumperlist;scanbumper2;scanbumper2=scanbumper2->next){
784
+ fac2=scanbumper2->distance(scanbumper->next->p0);
785
+ if(fac2<fac) fac=fac2;
786
+ }
787
+ printf("\n%d %lf",k,fac);
788
+ if(fac<1.){
789
+ laststep=scanbumper;
790
+ k--;
791
+ scanbumper2=scanbumper->next;
792
+ while(scanbumper2){
793
+ scanbumper=scanbumper2->next;
794
+ delete scanbumper2;
795
+ scanbumper2=scanbumper;
796
+ }
797
+ laststep->next=0;
798
+ scanbumper=laststep;
799
+ }
800
+ }
801
+
802
+
803
+ // Check if the model makes sense, otherwise set negative chi square for
804
+ // processing by subsequent programs.
805
+ if((c0>2)&&(c0<1.e100) && flagblending <np/2){
806
+ c0=c0;
807
+ }else{
808
+ c0=c1=-1.;
809
+ }
810
+
811
+ // Check if the parameters make sense, otherwise set negative chi square for
812
+ // processing by subsequent programs.
813
+ for(int i=0;i<nps;i++){
814
+ if(!((pr[i]>-1.e300)&&(pr[i]<1.e300))){
815
+ pr[i]=1.5;
816
+ c0=c1=-1.;
817
+ }
818
+ }
819
+
820
+
821
+
822
+ // Store model in a file named "(il).txt"
823
+ sprintf(filename, "%d.txt",il);
824
+ f=fopen(filename,"w");
825
+
826
+ // Print parameters to file
827
+
828
+ (this->*PrintFile)(f,c0, true);
829
+
830
+ // Print covariance matrix to file
831
+ fprintf(f,"\n");
832
+ for(int i=0;i<nps;i++){
833
+ if(i>0) fprintf(f,"\n");
834
+ fprintf(f,"%le",Cov[i]);
835
+ for(int j=1;j<nps;j++){
836
+ fprintf(f," %le",Cov[i+nps*j]);
837
+ }
838
+ }
839
+ fclose(f);
840
+
841
+
842
+ // Updating time count
843
+ //printf("\npartial time=%lf secs\n",(Environment::TickCount-tm)/1000.0);
844
+ //printf("total time = %lf mins\n",(Environment::TickCount-tim0)/60000.0);
845
+ //tm=Environment::TickCount;
846
+
847
+ // Updating the nlc.dat file
848
+ f=fopen("nlc.dat","w");
849
+ fprintf(f,"%d %d",il,nlc);
850
+ fclose(f);
851
+ }
852
+
853
+ il++;
854
+ }
855
+
856
+ // When terminating, update the Termination file,
857
+ // so that subsequent programs know everything has been concluded with this fit
858
+ sprintf(filename,"t%s.dat",outdir);
859
+ f=fopen(filename,"w");
860
+ fprintf(f,"%d %d",il,nlc);
861
+ fclose(f);
862
+
863
+ // If this fit has found the best minimum so far, update minchi.dat
864
+ current_path("..");
865
+ strcpy(filename,"minchi.dat");
866
+ if(exists(filename)){
867
+ f=fopen(filename,"r");
868
+ fscanf(f,"%lf",&minchi);
869
+ fclose(f);
870
+ }
871
+ if(bestchi<minchi){
872
+ f=fopen(filename,"w");
873
+ fprintf(f,"%lf %s",bestchi,outdir);
874
+ fclose(f);
875
+ }
876
+
877
+ }catch(...){
878
+ error=8;
879
+ }
880
+ }
881
+ }
882
+
883
+ double LevMar::ChiSquared(double *pr){
884
+ double chi2=0,chi0,chi1,p1;
885
+ int fl;
886
+
887
+ fl=0;
888
+ sumf[0]=sumfy[0]=sumf2[0]=0;
889
+ for(int i=0;i<np; i++){
890
+ if (filter[i] != fl) {
891
+ fl++;
892
+ sumf[fl] = sumfy[fl] = sumf2[fl] = 0;
893
+ }
894
+ VBBL->satellite=satel[i];
895
+ VBBL->a1 = limbdarks[fl];
896
+ fb[i]=(VBBL->*model)(pr,t[i]);
897
+ sumf[fl]+=w[i]*w[i]*fb[i];
898
+ sumf2[fl]+=w[i]*w[i]*fb[i]*fb[i];
899
+ sumfy[fl]+=w[i]*w[i]*fb[i]*y[i];
900
+ }
901
+ for(int i=0; i<=fl;i++){
902
+ #ifndef NOgOGLE
903
+ p1=sumf[i]*sumf[i]-sumf2[i]*sumsigma[i] + epsilon;
904
+ pr[nps+i*2]=(sumf[i]*sumfy[i]-sumf2[i]*sumy[i])/p1;
905
+ pr[nps+1+i*2]=(sumf[i]*sumy[i]-sumsigma[i]*sumfy[i])/p1;
906
+ //chi2+=fabs(sumy2[i]+(sumfy[i]*sumfy[i]*sumsigma[i]+sumf2[i]*sumy[i]*sumy[i]-2*sumf[i]*sumy[i]*sumfy[i])/p1);
907
+ #else
908
+ if(i==OGLE){
909
+ pr[nps+i*2]=0.;
910
+ pr[nps+1+i*2]=sumfy[i]/sumf2[i];
911
+ chi2+=fabs(sumy2[i]-sumfy[i]*pr[nps+1+i*2]);
912
+ }else{
913
+ p1=sumf[i]*sumf[i]-sumf2[i]*sumsigma[i] + epsilon;
914
+ pr[nps+i*2]=(sumf[i]*sumfy[i]-sumf2[i]*sumy[i])/p1;
915
+ pr[nps+1+i*2]=(sumf[i]*sumy[i]-sumsigma[i]*sumfy[i])/p1;
916
+ chi2+=fabs(sumy2[i]+(sumfy[i]*sumfy[i]*sumsigma[i]+sumf2[i]*sumy[i]*sumy[i]-2*sumf[i]*sumy[i]*sumfy[i])/p1);
917
+ }
918
+ #endif
919
+ if(pr[nps+1+i*2]<0){
920
+ pr[nps + 1 + i * 2] = 0;
921
+ }
922
+
923
+ }
924
+ chi2 = 0.;
925
+ chi0 = 0;
926
+ flagblending = 0;
927
+ for(int i=0;i<np; i++){
928
+ p1=(y[i]-pr[nps+filter[i]*2]-pr[nps+1+filter[i]*2]*fb[i])*w[i]*w[i]*pr[nps+1+filter[i]*2]*Tol;
929
+ chi0+=p1*p1;
930
+ p1 = (y[i] - pr[nps + filter[i] * 2] - pr[nps + 1 + filter[i] * 2] * fb[i]) * w[i];
931
+ chi2 += p1 * p1;
932
+ if (pr[nps + 1 + filter[i] * 2] > 2 * y[i]) {
933
+ flagblending++;
934
+ chi2 += (pr[nps + 1 + filter[i] * 2] - 2 * y[i]) * (pr[nps + 1 + filter[i] * 2] - 2 * y[i]) * w[i] * w[i];
935
+ }
936
+ }
937
+ chi0=sqrt(2*chi0);
938
+ if(chi0/chi2>0.1) Tol*=0.5;
939
+ if(chi0/chi2<0.01 && Tol<.99e-2) Tol*=2;
940
+
941
+ return chi2;
942
+ }
943
+
944
+ void LevMar::Grad(){
945
+ double inc,p1;
946
+ int fl;
947
+
948
+ for(int j=0;j<nps;j++){
949
+ printf("%d ",j);
950
+ //if((j<2)||(j>3)){
951
+ // inc=pr[j]*1.0e-3;
952
+ //}else{
953
+ inc=1.0e-3;
954
+ //}
955
+ pr[j]+=inc;
956
+ fl=0;
957
+ sumf[0]=sumfy[0]=sumf2[0]=0;
958
+ for(int i=0;i<np;i++){
959
+ if (filter[i] != fl) {
960
+ fl++;
961
+ sumf[fl] = sumfy[fl] = sumf2[fl] = 0;
962
+ }
963
+ VBBL->satellite=satel[i];
964
+ VBBL->a1 = limbdarks[fl];
965
+ fb[i+np*(j+1)]=(VBBL->*model)(pr,t[i]);
966
+ sumf[fl]+=w[i]*w[i]*fb[i+np*(j+1)];
967
+ sumf2[fl]+=w[i]*w[i]*fb[i+np*(j+1)]*fb[i+np*(j+1)];
968
+ sumfy[fl]+=w[i]*w[i]*fb[i+np*(j+1)]*y[i];
969
+ }
970
+ pr[j]-=inc;
971
+ for(int i=0; i<=fl;i++){
972
+ #ifndef NOgOGLE
973
+ p1=sumf[i]*sumf[i]-sumf2[i]*sumsigma[i] + epsilon;
974
+ prn[i*2]=(sumf[i]*sumfy[i]-sumf2[i]*sumy[i])/p1;
975
+ prn[1+i*2]=(sumf[i]*sumy[i]-sumsigma[i]*sumfy[i])/p1;
976
+ #else
977
+ if(i==OGLE){
978
+ prn[i*2]=0.;
979
+ prn[1+i*2]=sumfy[i]/sumf2[i];
980
+ }else{
981
+ p1=sumf[i]*sumf[i]-sumf2[i]*sumsigma[i] + epsilon;
982
+ prn[i*2]=(sumf[i]*sumfy[i]-sumf2[i]*sumy[i])/p1;
983
+ prn[1+i*2]=(sumf[i]*sumy[i]-sumsigma[i]*sumfy[i])/p1;
984
+ }
985
+ #endif
986
+ if(prn[1+i*2]<0) prn[1+i*2]=0;
987
+ dFdp[(1+2*i)*nps+j]=(prn[i*2]+prn[1+i*2]-pr[nps+i*2]-pr[nps+1+i*2])/inc; // error on baseline FB+FS
988
+ dFdp[(2*i)*nps+j]=(prn[i*2]/prn[1+i*2]-pr[nps+i*2]/pr[nps+1+i*2])/inc; // error on blending FB/FS
989
+ }
990
+ for(int i=0;i<np;i++){
991
+ Gr[j][i]=w[i]*(prn[filter[i]*2]+prn[1+filter[i]*2]*fb[i+np*(j+1)]-pr[nps+filter[i]*2]-pr[nps+1+filter[i]*2]*fb[i])/inc;
992
+ }
993
+ }
994
+ printf("OK\n");
995
+
996
+ for(int j=0;j<nps;j++){
997
+ for(int i=0;i<nps;i++){
998
+ Curv[i*nps+j]=0;
999
+ for(int k=0;k<np;k++){
1000
+ Curv[i*nps+j]+=Gr[i][k]*Gr[j][k];
1001
+ }
1002
+ }
1003
+ }
1004
+ for(int i=0;i<nps;i++){
1005
+ inc=0;
1006
+ for(int k=0;k<np;k++){
1007
+ inc+=w[k]*Gr[i][k]*(y[k]-pr[nps+filter[k]*2]-pr[nps+1+filter[k]*2]*fb[k]);
1008
+ }
1009
+ B0[i]=inc;
1010
+ }
1011
+ }
1012
+
1013
+ void LevMar::Covariance(){
1014
+ //double p1;
1015
+ //int j1,k1;
1016
+ //p1=Determinant(Curv,nps);
1017
+ //for(int i=0;i<nps;i++){
1018
+ // for(int i2=0;i2<nps;i2++){
1019
+ // for(int j=0;j<nps-1;j++){
1020
+ // for(int k=0;k<nps-1;k++){
1021
+ // j1=(j<i2)? j : j+1;
1022
+ // k1=(k<i)? k : k+1;
1023
+ // A[j*(nps-1)+k]=Curv[j1*nps+k1];
1024
+ // }
1025
+ // }
1026
+ // Cov[i*nps+i2]=Determinant(A,nps-1)/p1*(((i+i2)%2)? -1 : 1);
1027
+ // }
1028
+ //}
1029
+ Inverse(Curv, Cov, nps);
1030
+ for (int i = 0; i < nps; i++) {
1031
+ errs[i] = sqrt(fabs(Cov[i * nps + i]));
1032
+ }
1033
+ for(int i=0;i<2*nfil;i++){
1034
+ errs[i+nps]=0;
1035
+ for(int j1=0;j1<nps;j1++){
1036
+ for(int j2=0;j2<nps;j2++){
1037
+ errs[i+nps]+=Cov[j1*nps+j2]*dFdp[i*nps+j1]*dFdp[i*nps+j2];
1038
+ }
1039
+ }
1040
+ errs[i+nps]=sqrt(fabs(errs[i+nps]));
1041
+ if (!(errs[i + nps] > 0)) errs[i + nps] = 1.e100;
1042
+ }
1043
+ }
1044
+
1045
+ void LevMar::PrintOutPS(double *pr){
1046
+ printf("\nu0=%lf tE=%lf t0=%lf RS=%lf",exp(pr[0]),exp(pr[1]),pr[2],exp(pr[3]));
1047
+ }
1048
+
1049
+ void LevMar::PrintOutPX(double *pr){
1050
+ printf("\nu0=%lf tE=%lf t0=%lf RS=%lf pai1=%lf pai2=%lf",exp(pr[0]),exp(pr[1]),pr[2],exp(pr[3]),pr[4],pr[5]);
1051
+ }
1052
+
1053
+ void LevMar::PrintOutBS(double *pr){
1054
+ printf("\ntE=%lf FR=%lf u1=%lf u2=%lf t1=%lf t2=%lf rho=%lf",exp(pr[0]),exp(pr[1]),pr[2],pr[3],pr[4],pr[5],exp(pr[6]));
1055
+ }
1056
+
1057
+ void LevMar::PrintOutBO(double *pr){
1058
+ printf("\nu0=%lf t0=%lf tE=%lf rho=%lf xi1=%lf xi2=%lf\nom=%lf inc=%lf phi=%lf qs=%lf",pr[0],pr[1],exp(pr[2]),exp(pr[3]),pr[4],pr[5],pr[6],pr[7],pr[8],exp(pr[9]));
1059
+ }
1060
+
1061
+ void LevMar::PrintOutLS(double *pr){
1062
+ printf("\na=%lf q=%lf uc=%lf th=%lf RS=%lf\ntE=%lf tc=%lf",exp(pr[0]),exp(pr[1]),pr[2],pr[3],exp(pr[4]),exp(pr[5]),pr[6]);
1063
+ }
1064
+
1065
+ void LevMar::PrintOutLX(double *pr){
1066
+ printf("\na=%lf q=%lf u0=%lf th=%lf RS=%lf\ntE=%lf t0=%lf pai1=%lf pai2=%lf",exp(pr[0]),exp(pr[1]),pr[2],pr[3],exp(pr[4]),exp(pr[5]),pr[6],pr[7],pr[8]);
1067
+ }
1068
+
1069
+ void LevMar::PrintOutLO(double *pr){
1070
+ printf("\na=%lf q=%lf u0=%lf th=%lf RS=%lf\ntE=%lf t0=%lf pai1=%lf pai2=%lf\ndsdt=%lf dthdt=%lf w3=%lf",exp(pr[0]),exp(pr[1]),pr[2],pr[3],exp(pr[4]),exp(pr[5]),pr[6],pr[7],pr[8],pr[9],pr[10],pr[11]);
1071
+ }
1072
+
1073
+ void LevMar::PrintOutLK(double* pr) {
1074
+ printf("\na=%lf q=%lf u0=%lf th=%lf RS=%lf\ntE=%lf t0=%lf pai1=%lf pai2=%lf\ndsdt=%lf dthdt=%lf w3=%lf szs=%lf ar=%lf", exp(pr[0]), exp(pr[1]), pr[2], pr[3], exp(pr[4]), exp(pr[5]), pr[6], pr[7], pr[8], pr[9], pr[10], pr[11], pr[12], pr[13]);
1075
+ }
1076
+
1077
+
1078
+ void LevMar::PrintFilePS(FILE *f,double c0, bool printerrors){
1079
+ fprintf(f,"%.16le %.16le %.16le %.16le",exp(pr[0]),exp(pr[1]),pr[2],exp(pr[3]));
1080
+ for(int i=nps;i<nps+2*nfil;i++){
1081
+ pr[i]=((pr[i]>-1.e300)&&(pr[i]<1.e300))? pr[i] : -1.e300;
1082
+ fprintf(f," %le",pr[i]);
1083
+ }
1084
+ fprintf(f," %.16le",c0);
1085
+ if (printerrors) {
1086
+ fprintf(f, "\n%le %le %le %le", errs[0] * exp(pr[0]), errs[1] * exp(pr[1]), errs[2], errs[3] * exp(pr[3]));
1087
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1088
+ fprintf(f, " %le", errs[i]);
1089
+ }
1090
+ }
1091
+ }
1092
+
1093
+ void LevMar::PrintFilePX(FILE *f,double c0, bool printerrors){
1094
+ fprintf(f,"%.16le %.16le %.16le %.16le %.16le %.16le",pr[0],exp(pr[1]),pr[2],exp(pr[3]),pr[4],pr[5]);
1095
+ for(int i=nps;i<nps+2*nfil;i++){
1096
+ pr[i]=((pr[i]>-1.e300)&&(pr[i]<1.e300))? pr[i] : -1.e300;
1097
+ fprintf(f," %le",pr[i]);
1098
+ }
1099
+ fprintf(f," %.16le",c0);
1100
+ if (printerrors) {
1101
+ fprintf(f, "\n%le %le %le %le %le %le", errs[0], errs[1] * exp(pr[1]), errs[2], errs[3] * exp(pr[3]), errs[4], errs[5]);
1102
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1103
+ fprintf(f, " %le", errs[i]);
1104
+ }
1105
+ }
1106
+ }
1107
+
1108
+ void LevMar::PrintFileBS(FILE *f,double c0, bool printerrors){
1109
+ if (pr[1] > 0) { // Invert sources
1110
+ double sc;
1111
+ pr[1] = -pr[1];
1112
+ sc = pr[3];
1113
+ pr[3] = pr[2];
1114
+ pr[2] = sc;
1115
+ sc = pr[5];
1116
+ pr[5] = pr[4];
1117
+ pr[4] = sc;
1118
+ sc = errs[3];
1119
+ errs[3] = errs[2];
1120
+ errs[2] = sc;
1121
+ sc = errs[5];
1122
+ errs[5] = errs[4];
1123
+ errs[4] = sc;
1124
+ for (int k = 0; k < nps; k++) {
1125
+ Cov[1 + nps * k] = - Cov[1 + nps * k];
1126
+ Cov[k + nps * 1] = - Cov[k + nps * 1];
1127
+ }
1128
+ for (int k = 0; k < nps; k++) {
1129
+ sc = Cov[2 + nps * k];
1130
+ Cov[2 + nps * k] = Cov[3 + nps * k];
1131
+ Cov[3 + nps * k] = sc;
1132
+ sc = Cov[5 + nps * k];
1133
+ Cov[5 + nps * k] = Cov[4 + nps * k];
1134
+ Cov[4 + nps * k] = sc;
1135
+ }
1136
+ for (int k = 0; k < nps; k++) {
1137
+ sc = Cov[k + nps * 2];
1138
+ Cov[k + nps * 2] = Cov[k + nps * 3];
1139
+ Cov[k + nps * 3] = sc;
1140
+ sc = Cov[k + nps * 5];
1141
+ Cov[k + nps * 5] = Cov[k + nps * 4];
1142
+ Cov[k + nps * 4] = sc;
1143
+ }
1144
+ }
1145
+
1146
+ fprintf(f,"%.16le %.16le %.16le %.16le %.16le %.16le %.16le",exp(pr[0]),exp(pr[1]),pr[2],pr[3],pr[4],pr[5],exp(pr[6]));
1147
+ for(int i=nps;i<nps+2*nfil;i++){
1148
+ pr[i]=((pr[i]>-1.e300)&&(pr[i]<1.e300))? pr[i] : -1.e300;
1149
+ fprintf(f," %le",pr[i]);
1150
+ }
1151
+ fprintf(f," %.16le",c0);
1152
+ if (printerrors) {
1153
+ fprintf(f, "\n%le %le %le %le %le %le %le", errs[0] * exp(pr[0]), errs[1] * exp(pr[1]), errs[2], errs[3], errs[4], errs[5], errs[6] * exp(pr[6]));
1154
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1155
+ fprintf(f, " %le", errs[i]);
1156
+ }
1157
+ }
1158
+ }
1159
+
1160
+ void LevMar::PrintFileBO(FILE *f,double c0, bool printerrors){
1161
+ fprintf(f,"%.16lf %.16lf %.16lf %.16lf %.16lf %.16lf %.16lf %.16lf %.16lf %.16lf", pr[0], pr[1], exp(pr[2]), exp(pr[3]), pr[4], pr[5], pr[6], pr[7], pr[8], exp(pr[9]));
1162
+ for(int i=nps;i<nps+2*nfil;i++){
1163
+ pr[i]=((pr[i]>-1.e300)&&(pr[i]<1.e300))? pr[i] : -1.e300;
1164
+ fprintf(f," %le",pr[i]);
1165
+ }
1166
+ fprintf(f," %.16le",c0);
1167
+ if (printerrors) {
1168
+ fprintf(f, "\n%le %le %le %le %le %le %le %le %le %le", errs[0], errs[1], errs[2] * exp(pr[2]), errs[3] * exp(pr[3]), errs[4], errs[5], errs[6], errs[7], errs[8], errs[9] * exp(pr[9]));
1169
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1170
+ fprintf(f, " %le", errs[i]);
1171
+ }
1172
+ }
1173
+ }
1174
+
1175
+ void LevMar::PrintFileLS(FILE *f,double c0, bool printerrors){
1176
+ if (pr[1] > 0) {
1177
+ pr[3] = pr[3] - M_PI;
1178
+ pr[1] = -pr[1];
1179
+ for (int k = 0; k < nps; k++) {
1180
+ Cov[1 + nps * k] = -Cov[1 + nps * k];
1181
+ Cov[k + nps * 1] = -Cov[k + nps * 1];
1182
+ }
1183
+ }
1184
+ while (pr[3] > 2*M_PI) pr[3] -= 2*M_PI;
1185
+ while (pr[3] < 0) pr[3] += 2*M_PI;
1186
+ if (pr[2] < 0) {
1187
+ pr[3] = 2 * M_PI - pr[3];
1188
+ pr[2] = -pr[2];
1189
+ for (int k = 0; k < nps; k++) {
1190
+ Cov[2 + nps * k] = -Cov[2 + nps * k];
1191
+ Cov[k + nps * 2] = -Cov[k + nps * 2];
1192
+ Cov[3 + nps * k] = -Cov[3 + nps * k];
1193
+ Cov[k + nps * 3] = -Cov[k + nps * 3];
1194
+ }
1195
+ }
1196
+
1197
+ fprintf(f,"%.16le %.16le %.16le %.16le %.16le %.16le %.16le",exp(pr[0]),exp(pr[1]),pr[2],pr[3],exp(pr[4]),exp(pr[5]),pr[6]);
1198
+ for(int i=nps;i<nps+2*nfil;i++){
1199
+ pr[i]=((pr[i]>-1.e300)&&(pr[i]<1.e300))? pr[i] : -1.e300;
1200
+ fprintf(f," %le",pr[i]);
1201
+ }
1202
+ fprintf(f," %.16le",c0);
1203
+ if (printerrors) {
1204
+ fprintf(f, "\n%le %le %le %le %le %le %le", errs[0] * exp(pr[0]), errs[1] * exp(pr[1]), errs[2], errs[3], errs[4] * exp(pr[4]), errs[5] * exp(pr[5]), errs[6]);
1205
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1206
+ fprintf(f, " %le", errs[i]);
1207
+ }
1208
+ }
1209
+ }
1210
+
1211
+ void LevMar::PrintFileLX(FILE *f,double c0, bool printerrors){
1212
+ if (pr[1] > 0) {
1213
+ pr[3] = pr[3] - M_PI;
1214
+ pr[1] = -pr[1];
1215
+ for (int k = 0; k < nps; k++) {
1216
+ Cov[1 + nps * k] = -Cov[1 + nps * k];
1217
+ Cov[k + nps * 1] = -Cov[k + nps * 1];
1218
+ }
1219
+ }
1220
+ while (pr[3] > 2 * M_PI) pr[3] -= 2 * M_PI;
1221
+ while (pr[3] < 0) pr[3] += 2 * M_PI;
1222
+
1223
+ fprintf(f,"%.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le",exp(pr[0]),exp(pr[1]),pr[2],pr[3],exp(pr[4]),exp(pr[5]),pr[6],pr[7],pr[8]);
1224
+ for(int i=nps;i<nps+2*nfil;i++){
1225
+ pr[i]=((pr[i]>-1.e300)&&(pr[i]<1.e300))? pr[i] : -1.e300;
1226
+ fprintf(f," %le",pr[i]);
1227
+ }
1228
+ fprintf(f," %.16le",c0);
1229
+ if (printerrors) {
1230
+ fprintf(f, "\n%le %le %le %le %le %le %le %le %le", errs[0] * exp(pr[0]), errs[1] * exp(pr[1]), errs[2], errs[3], errs[4] * exp(pr[4]), errs[5] * exp(pr[5]), errs[6], errs[7], errs[8]);
1231
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1232
+ fprintf(f, " %le", errs[i]);
1233
+ }
1234
+ }
1235
+ }
1236
+
1237
+ void LevMar::PrintFileLO(FILE *f,double c0, bool printerrors){
1238
+ if (pr[1] > 0) {
1239
+ pr[3] = pr[3] - M_PI;
1240
+ pr[1] = -pr[1];
1241
+ for (int k = 0; k < nps; k++) {
1242
+ Cov[1 + nps * k] = -Cov[1 + nps * k];
1243
+ Cov[k + nps * 1] = -Cov[k + nps * 1];
1244
+ }
1245
+ }
1246
+ while (pr[3] > 2 * M_PI) pr[3] -= 2 * M_PI;
1247
+ while (pr[3] < 0) pr[3] += 2 * M_PI;
1248
+
1249
+ fprintf(f,"%.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le",exp(pr[0]),exp(pr[1]),pr[2],pr[3],exp(pr[4]),exp(pr[5]),pr[6],pr[7],pr[8],pr[9],pr[10],pr[11]);
1250
+ for(int i=nps;i<nps+2*nfil;i++){
1251
+ pr[i]=((pr[i]>-1.e300)&&(pr[i]<1.e300))? pr[i] : -1.e300;
1252
+ fprintf(f," %le",pr[i]);
1253
+ }
1254
+ fprintf(f," %.16le",c0);
1255
+ if (printerrors) {
1256
+ fprintf(f, "\n%le %le %le %le %le %le %le %le %le %le %le %le", errs[0] * exp(pr[0]), errs[1] * exp(pr[1]), errs[2], errs[3], errs[4] * exp(pr[4]), errs[5] * exp(pr[5]), errs[6], errs[7], errs[8], errs[9], errs[10], errs[11]);
1257
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1258
+ fprintf(f, " %le", errs[i]);
1259
+ }
1260
+ }
1261
+ }
1262
+
1263
+
1264
+ void LevMar::PrintFileLK(FILE* f, double c0, bool printerrors) {
1265
+ fprintf(f, "%.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le %.16le", exp(pr[0]), exp(pr[1]), pr[2], pr[3], exp(pr[4]), exp(pr[5]), pr[6], pr[7], pr[8], pr[9], pr[10], pr[11],pr[12],pr[13]);
1266
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1267
+ pr[i] = ((pr[i] > -1.e300) && (pr[i] < 1.e300)) ? pr[i] : -1.e300;
1268
+ fprintf(f, " %le", pr[i]);
1269
+ }
1270
+ fprintf(f, " %.16le", c0);
1271
+ if (printerrors) {
1272
+ fprintf(f, "\n%le %le %le %le %le %le %le %le %le %le %le %le %le %le", errs[0] * exp(pr[0]), errs[1] * exp(pr[1]), errs[2], errs[3], errs[4] * exp(pr[4]), errs[5] * exp(pr[5]), errs[6], errs[7], errs[8], errs[9], errs[10], errs[11], errs[12], errs[13]);
1273
+ for (int i = nps; i < nps + 2 * nfil; i++) {
1274
+ fprintf(f, " %le", errs[i]);
1275
+ }
1276
+ }
1277
+ }