-
Notifications
You must be signed in to change notification settings - Fork 0
/
tidev.cpp
1515 lines (1317 loc) · 37.3 KB
/
tidev.cpp
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
// **********************************************************************
// _______ ________
// /_ __(_)___/ / ____/ __
// / / / / __ / __/ | | / /
// / / / / /_/ / /___ | |/ /
// /_/ /_/\__,_/_____/ |___/
// Tidal Spin Evolution
// **********************************************************************
// Copyright (C) 2013
// Jorge Zuluaga (zuluagajorge@gmail.com, Mario Melita (melita@iafe.uba.ar)
// Pablo Cuartas (quarktas@gmail.com), Bayron Portilla (bayron@gmail.com)
// **********************************************************************
// MAIN PACKAGE FILE
// **********************************************************************
// Use: OPTIONS=-DVERBOSE make <program>.out to enable this macro
//#define VERBOSE
////////////////////////////////////////////////////////////////////////
//HEADERS
////////////////////////////////////////////////////////////////////////
//STANDARD
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <cstring>
#include <unistd.h>
#include <sys/time.h>
//GSL
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_const_mksa.h>
#include <gsl/gsl_const_cgsm.h>
#include <gsl/gsl_const_num.h>
#include <gsl/gsl_sf.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_odeiv2.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_roots.h>
//LIBCONFIG
#include <libconfig.h++>
////////////////////////////////////////////////////////////////////////
//GLOBAL TYPES
////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace libconfig;
typedef void* params;
typedef double Real;
typedef FILE* file;
typedef complex<double> Complex;
////////////////////////////////////////////////////////////////////////
//MACROS
////////////////////////////////////////////////////////////////////////
#define setting const Setting&
#define configInit() setting CFG_ROOT=CFG.getRoot()
#define configList(var,name) setting var=CFG_ROOT[name]
#define configValue(type,var,name) type var=CFG.lookup(name)
#define configValueList(list,var,name) list.lookupValue(name,var)
//ERROR HANDLING
#define errorOff() gsl_set_error_handler_off()
//EXPONENTIATION
#define PINT(x,n) gsl_pow_int(x,n)
#define PREAL(x,y) pow(x,y)
//STRING
#define STR(string) string.c_str()
//VERBOSITY
#define Verbose printf("... ");printf
//MULTILINE STRING
#define MULTI(str) #str
//ALPHA
#define ALPHA(a1,a2) (a1<=a2?a1/a2:a2/a1)
////////////////////////////////////////////////////////////////////////
//CONSTANTS
////////////////////////////////////////////////////////////////////////
//NUMBER OF VARIABLES
#define NUMVARS 8 //5 elements + theta + omega + Etid
//METHOD OF INTEGRATION
#define ODEMETHOD gsl_odeiv2_step_rk4
//NUMERICAL
//#define PI M_PIl
#define PI M_PI
#define PI2 (PI*PI)
#define D2R PI/180
//BEHAVIOR
#define NMAX 50
//PHYSICAL AND ASTRONOMICAL CONSTANTS
#define MSUN (GSL_CONST_MKSA_SOLAR_MASS) //kg
#define RSUN (6.96342E8) //m
#define AU (GSL_CONST_MKSA_ASTRONOMICAL_UNIT) //m
#define HOURS (3600.0) //s
#define YEAR (365.25*GSL_CONST_MKSA_DAY) //s
#define MEARTH (5.9736E24) //kg
#define REARTH (6.371E6) //m
#define GCONST (GSL_CONST_MKSA_GRAVITATIONAL_CONSTANT) // m^3 / (kg s^2)
#define MICRO 1E-6
//BEHAVIOR
#define MAXBODIES 10
//MODES
#define MODE_TIDAL 0
#define MODE_SECULAR 1
#define MODE_TIDALSECULAR 2
////////////////////////////////////////////////////////////////////////
//GLOBAL VARIABLES
////////////////////////////////////////////////////////////////////////
Complex I1(0,1);
int Mode;
int Option;
class Config CFG;
int NBodies,NPlanets,IBody;
Real UL,UM,UT,GPROG;
Real Gecc[11];
Real X_32[NMAX+1][NMAX+1];
Real TauTidal,TauTriax;
gsl_rng* RanGen;
////////////////////////////////////////////////////////////////////////
//CLASSES
////////////////////////////////////////////////////////////////////////
class Body
{
public:
int active;
int tidal;
string name;
string units;
//Physical
Real M,R;
Real rho;
//Rheological
Real mur,alpha,tauM,tauA;
Real thetaini,Pini,Wini;
Real gapo,cosapt,sinapt;//Alpha functions
Real A2;//A_2
//Orbital parameters
Real mu;
Real a,e;
Real n,P;
Real I,Om,w;
Real gmt;//1.5*G*Ms^2*R^5/(a^6)
//Momentum of Inertia: C: main, A,B: secondary
Real MoI,BmA;
Real A,B,C;
//Eccentricities transformation
int NEccFt;
Real TEccFt;
Real** EccFt;
//Inertia momenta
int physicalProperties(){
//Mean density
if(R>0){
rho=M/(4*PI*R*R*R/3);
}else rho=0;
//Moment of inertia
C=MoI*M*R*R;
A=C;
B=C*(1.0+BmA);
//Derived factor
if(a>0){
gmt=1.5*(mu*mu/GPROG)*R*R*R*R*R/(a*a*a*a*a*a);
}
else gmt=0;
}
//Orbital properties
int orbitalProperties(){
if(a>0){
n=2*PI*sqrt((mu/GPROG+M)/(a*a*a));
P=2*PI/n;
}
else n=P=gmt=0.0;
}
//Rheology properties
int rheologyProperties(){
gapo=gsl_sf_gamma(alpha+1);
cosapt=cos(alpha*PI/2);
sinapt=sin(alpha*PI/2);
A2=(57./8)*mur/(PI*GPROG*rho*rho*R*R);
}
//Conversion units
int setUnits(void);
}Bodies[MAXBODIES],Central;
////////////////////////////////////////////////////////////////////////
//ROUTINES
////////////////////////////////////////////////////////////////////////
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//I/O
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
int configLoad(const char* file)
{
if(fopen(file,"r")!=NULL)
CFG.readFile(file);
else {
fprintf(stderr,"No configuration file '%s' found\n",file);
exit(1);
}
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//UTIL
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
double Random(void)
{
return gsl_rng_uniform(RanGen);
}
/*
Test code:
double f,df;
for(i=0;i<niplanets;i++){
ip=iplanets[i];
loadEccData(&Bodies[ip]);
f=fourierSeries(120.0,Bodies[ip].EccFt,Bodies[ip].NEccFt,Bodies[ip].TEccFt);
df=DfourierSeries(120.0,Bodies[ip].EccFt,Bodies[ip].NEccFt,Bodies[ip].TEccFt);
fprintf(stdout,"f = %.17e, df/dt = %.17e\n",f,df);
}
*/
double fourierSeries(double t,double** ft,int M,double T)
{
int k;
Complex fs,A;
double w,phi;
fs=ft[0][2]+ft[0][3]*I1;
for(k=1;k<M;k++){
w=2*PI*k/T;
A=ft[k][2]+ft[k][3]*I1;
phi=ft[k][5];
fs+=A*exp(I1*(w*t+phi));
}
return fs.real();
}
double DfourierSeries(double t,double** ft,int M,double T)
{
int k;
Complex dfs,A;
double w,phi;
dfs=0;
for(k=1;k<M;k++){
w=2*PI*k/T;
A=ft[k][2]+ft[k][3]*I1;
phi=ft[k][5];
dfs+=A*exp(I1*(w*t+phi))*I1*w;
}
return dfs.real();
}
/* ----------------------------------------------------------------------
PRINT VECTOR
---------------------------------------------------------------------- */
void fprintf_vec(FILE* stream,const char* fmt,const double x[],int end,int ini=0)
{
for(int i=ini;i<end;i++){
fprintf(stream,"%d:",i);
fprintf(stream,fmt,x[i]);
}
fprintf(stream,"\n");
}
/* ----------------------------------------------------------------------
TIME ROUTINE
---------------------------------------------------------------------- */
double** matrixAlloc(int n,int m)
{
int i;
double **M;
M=(double**)calloc(n,sizeof(double*));
for(i=0;i<n;i++)
M[i]=(double*)calloc(m,sizeof(double));
return M;
}
double Time(void)
{
double t;
struct timeval tiempo;
gettimeofday(&tiempo,NULL);
t=1e6*tiempo.tv_sec+tiempo.tv_usec;
return t;
}
/* ----------------------------------------------------------------------
SGN FUNCTION
---------------------------------------------------------------------- */
template <typename T>
int SGN(T val) {
return (T(0) < val) - (val < T(0));
}
/* ----------------------------------------------------------------------
FORMAT STRING
---------------------------------------------------------------------- */
char* FRM(int n)
{
char* cadena;
cadena=(char*)calloc(sizeof(char),100);
strcpy(cadena,"");
for(int i=1;i<=n;i++){
strcat(cadena,"%.15e ");
}
strcat(cadena,"\n");
return cadena;
}
int readBodies(void)
{
configInit();
configList(bodies,"bodies");
NBodies=bodies.getLength();
NPlanets=NBodies-1;
IBody=1;//Object by default
for(int i=0;i<NBodies;i++){
//Read properties
configValueList(bodies[i],Bodies[i].active,"active");
configValueList(bodies[i],Bodies[i].tidal,"tidal");
configValueList(bodies[i],Bodies[i].name,"name");
configValueList(bodies[i],Bodies[i].units,"units");
configValueList(bodies[i],Bodies[i].M,"M");
configValueList(bodies[i],Bodies[i].R,"R");
configValueList(bodies[i],Bodies[i].a,"a");
configValueList(bodies[i],Bodies[i].e,"e");
configValueList(bodies[i],Bodies[i].I,"I");
configValueList(bodies[i],Bodies[i].Om,"Om");
configValueList(bodies[i],Bodies[i].w,"w");
configValueList(bodies[i],Bodies[i].MoI,"MoI");
configValueList(bodies[i],Bodies[i].BmA,"BmA");
configValueList(bodies[i],Bodies[i].mur,"mur");
configValueList(bodies[i],Bodies[i].alpha,"alpha");
configValueList(bodies[i],Bodies[i].tauM,"tauM");
configValueList(bodies[i],Bodies[i].tauA,"tauA");
configValueList(bodies[i],Bodies[i].thetaini,"thetaini");
configValueList(bodies[i],Bodies[i].Pini,"Pini");
configValueList(bodies[i],Bodies[i].Wini,"Wini");
//Adjust units
Bodies[i].setUnits();
//Compute derived properties
Bodies[i].mu=GPROG*Bodies[0].M;
Bodies[i].physicalProperties();
Bodies[i].orbitalProperties();
//Orbital period in physical units
if(Bodies[i].Pini<0){
Bodies[i].Pini=Bodies[i].P/Bodies[i].Wini;
}
Bodies[i].rheologyProperties();
}
Central=Bodies[0];
#ifdef VERBOSE
for(int i=0;i<NBodies;i++){
Body b=Bodies[i];
Verbose("Body %d: %s\n",i+1,STR(b.name));
Verbose("\tPhysical: M = %e, R = %e\n",b.M,b.R);
Verbose("\tConstitution: rho = %e\n",b.rho);
Verbose("\tRheology: mur (rigidity) = %e, tau_M = %e, tau_A = %e\n",
b.mur,b.tauM,b.tauA);
Verbose("\t\talpha = %e, Gamma(1+alpha) = %e\n",b.alpha,b.gapo);
Verbose("\tInertia moment: I = (%e,%e,%e)\n",b.A,b.B,b.C);
Verbose("\t\tgmt = %e\n",b.gmt);
Verbose("\tOrbital basic: mu=%e, a = %e, e = %e, i = %f deg, xi = %f deg, Om = %f deg, w = %f deg\n",
b.mu,b.a,b.e,b.i,b.xi,b.Om,b.w);
Verbose("\tOrbital derived: n = %e, P = %e\n",b.n,b.P);
}
#endif
}
int readGEcc(string file)
{
FILE* fh=fopen(STR(file),"r");
int n,m,i,j,k,q;
Real coef,tmp;
for(j=0;j<NMAX;j++) for(k=0;k<NMAX;k++) X_32[j][k]=0.0;
while(!feof(fh)){
fscanf(fh,"%d %d %d %d %lf %lf",&n,&m,&j,&k,&coef,&tmp);
if(n<-3) continue;
else if(n==-3) if(m==2) X_32[j][k]=coef;
else break;
}
for(int i=1;i<=10;i++){
q=-3+i;
k=2+q;
Gecc[i]=0.0;
for(int j=0;j<NMAX;j++){
Gecc[i]+=X_32[j][k]*PINT(Bodies[IBody].e,j);
}
Gecc[i]*=Gecc[i];
}
#ifdef VERBOSE
for(int i=1;i<=10;i++){
Verbose("G[%d] = %e\n",i,Gecc[i]);
}
#endif
return GSL_SUCCESS;
}
int updateGEcc(double e)
{
int n,m,i,j,k,q;
for(int i=1;i<=10;i++){
q=-3+i;
k=2+q;
Gecc[i]=0.0;
for(int j=0;j<NMAX;j++){
Gecc[i]+=X_32[j][k]*PINT(e,j);
}
Gecc[i]*=Gecc[i];
}
return GSL_SUCCESS;
}
int Body::setUnits(void)
{
Real ul,um;
if(units=="EARTH")
{ul=(REARTH/UL);um=(MEARTH/UM);}
else if(units=="SUN")
{ul=(RSUN/UL);um=(MSUN/UM);}
M*=um;
R*=ul;
a*=(AU/UL);
mur=mur/(UM/(UL*UT*UT));
tauM*=(YEAR/UT);
tauA*=(YEAR/UT);
thetaini*=(D2R);
Pini*=(HOURS/UT);
}
int setUnits(Real ul,Real um,Real ut,Real G)
{
Real ratio=(G/GCONST);
if(ut==0){
UL=ul;UM=um;GPROG=G;
UT=sqrt(ratio*ul*ul*ul/um);
}
else if(ul==0){
UM=um;UT=ut;GPROG=G;
UL=1/pow(ratio/(um*ut*ut),1.0/3);
}
else if(um==0){
UL=ul;UT=ut;GPROG=G;
UM=ratio*ul*ul*ul/(ut*ut);
}
else if(G==0){
UL=ul;UT=ut;UM=um;
GPROG=GCONST/(ul*ul*ul/(um*ut*ut));
}
#ifdef VERBOSE
Verbose("Units: UL = %e, UM = %e, UT = %e\n",UL,UM,UT);
Verbose("Gravitational Constant: G = %e UL^3/(UM UT^2)\n",GPROG);
#endif
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//NUMERICAL
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
/* ----------------------------------------------------------------------
SOLVE KEPLER EQUATION
Source:
Taken from http://astro.pas.rochester.edu/~aquillen/ast570/code/kepcart.cpp
supposed to be good to second order in e, from Brouwer+Clemence u0 is
first guess
---------------------------------------------------------------------- */
#define PREC_ECC_ANO 1e-14
Real solveKepler(Real e,Real M)
{
Real du,u0,l0;
du=1.0;
u0=M+e*sin(M)+0.5*e*e*sin(2.0*M);
while(fabs(du)>PREC_ECC_ANO){
l0 = u0 - e*sin(u0);
du = (M - l0)/(1.0 - e*cos(u0));
u0 += du;
}
return u0;
}
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//PHYSICAL
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
////////////////////////////////////////////////////////////////////////
//LAPLACE COEFFICIENTS
////////////////////////////////////////////////////////////////////////
#define JEFF(j) ((int)(j+4))
#define SEFF(s) ((int)(s-0.5))
double coefLaplace(double th,void *param)
{
double* ps=(double*) param;
double j=ps[0];
double s=ps[1];
double alpha=ps[2];
double f=(1/pow((1.0-2.0*alpha*cos(th)+alpha*alpha),s))*cos(j*th);
return f;
}
class LaplaceCoefficients
{
public:
double Alpha;
double Mp;
double time;
gsl_matrix **Bjs;
gsl_integration_workspace* IntWork;
gsl_function Func;
double Laplparam[3];
double df;
double df0;
double df1, df2, df3, df4;
double df01, df02;
double df20, df22;
double df10, df11, df12;
double df13, df14;
double df31, df32, df31b, df32b;
double df100, df101, df102, df103, df104;
int set(double alpha,double mp)
{
Mp=mp;
Alpha=alpha;
#ifdef VERBOSE
Verbose("Initial alpha:%e\n",Alpha);
#endif
Bjs=(gsl_matrix**)calloc(5,sizeof(gsl_matrix*));
IntWork=gsl_integration_workspace_alloc(1000);
Func.function=&coefLaplace;
Func.params=&Laplparam;
update(alpha);
return 0;
}
int update(double alpha)
{
Alpha=alpha;
for(int i=0;i<=4;i++){
Bjs[i]=gsl_matrix_alloc(11,11);
gsl_matrix_set_all(Bjs[i],2E100);
}
}
double Bsget(int order,int j,double s)
{
return gsl_matrix_get(Bjs[order],JEFF(j),SEFF(s));
}
double Bsset(int order,int j,double s,double f)
{
gsl_matrix_set(Bjs[order],JEFF(j),SEFF(s),f);
}
double bsget(int order,int j,double s,double alpha)
{
double f;
f=Bsget(order,j,s);
f>1E100&&bsfunc(order,j,s,alpha,&f);
return f;
}
int bsfunc(int order,int j,double s,double alpha,double *f)
{
switch(order){
case 0:{
double integral,error;
Laplparam[0]=j;
Laplparam[1]=s;
Laplparam[2]=alpha;
gsl_integration_qags(&Func,0.0,PI,0.0,1E-6,1000,IntWork,&integral,&error);
*f=(2/PI)*integral;
break;
}
case 1:{
double f1,f2,f3;
f1=bsget(0,j,s+1.0,alpha);
f2=bsget(0,j-1.0,s+1.0,alpha);
f3=bsget(0,j+1.0,s+1.0,alpha);
*f=s*(f2-2.0*alpha*f1+f3);
break;
}
case 2:{
double f0=bsget(0,j,s+1.0,alpha);
double f1=bsget(1,j,s+1.0,alpha);
double f2=bsget(1,j-1.0,s+1.0,alpha);
double f3=bsget(1,j+1.0,s+1.0,alpha);
*f=s*(f2-2.0*alpha*f1-2.0*f0+f3);
break;
}
case 3:{
double f0=bsget(1,j,s+1.0,alpha);
double f1=bsget(2,j,s+1.0,alpha);
double f2=bsget(2,j-1.0,s+1.0,alpha);
double f3=bsget(2,j+1.0,s+1.0,alpha);
*f=s*(f2-4.0*f0-2.0*alpha*f1+f3);
break;
}
case 4:{
double f0=bsget(2,j,s+1.0,alpha);
double f1=bsget(3,j,s+1.0,alpha);
double f2=bsget(3,j-1.0,s+1.0,alpha);
double f3=bsget(3,j+1.0,s+1.0,alpha);
*f=s*(f2-6.0*f0-2.0*alpha*f1+f3);
break;
}
}
Bsset(order,j,s,*f);
}
void coefcs()
{
double t1,t2;
double alpha=Alpha;
t1=Time();
df=bsget(0,0.0,0.5,alpha);
df0=bsget(0,1.0,1.5,alpha);
df01=bsget(1,1.0,1.5,alpha);
df02=bsget(2,1.0,1.5,alpha);
df1=bsget(1,0.0,0.5,alpha);
df2=bsget(2,0.0,0.5,alpha);
df3=bsget(3,0.0,0.5,alpha);
df4=bsget(4,0.0,0.5,alpha);
df20=bsget(0,0.0,2.5,alpha);
df22=bsget(0,2.0,2.5,alpha);
df10=bsget(0,1.0,0.5,alpha);
df11=bsget(1,1.0,0.5,alpha);
df12=bsget(2,1.0,0.5,alpha);
df13=bsget(3,1.0,0.5,alpha);
df14=bsget(4,1.0,0.5,alpha);
df31=bsget(1,0.0,1.5,alpha);
df32=bsget(2,0.0,1.5,alpha);
df31b=bsget(1,2.0,1.5,alpha);
df32b=bsget(2,2.0,1.5,alpha);
df100=bsget(0,2.0,0.5,alpha);
df101=bsget(1,2.0,0.5,alpha);
df102=bsget(2,2.0,0.5,alpha);
df103=bsget(3,2.0,0.5,alpha);
df104=bsget(4,2.0,0.5,alpha);
t2=Time();
time=(t2-t1)*1E-6;
#ifdef VERBOSE
Verbose("Alpha = %e\n",alpha);
Verbose("df,df0,df01,df02 = %e %e %e %e\n",df,df0,df01,df02);
Verbose("df1,df2,df3,df4 = %e %e %e %e\n",df1,df2,df3,df4);
Verbose("df20,df22 = %e %e\n",df20,df22);
Verbose("df10,df11,df12,df13,df14 = %e %e %e %e %e\n",df10,df11,df12,df13,df14);
Verbose("df31,df32,df31b,df32b = %e %e %e %e\n",df31,df32,df31b,df32b);
Verbose("df100,df101,df102,df103,df104 = %e %e %e %e %e\n",df100,df101,df102,df103,df104);
#endif
}
};
#define LC LaplaceCoefficients
LC** laplaceAlloc(int n,int m)
{
int i;
LC** M;
M=(LC**)calloc(n,sizeof(LC*));
for(i=0;i<n;i++)
M[i]=(LC*)calloc(m,sizeof(LC));
return M;
}
////////////////////////////////////////////////////////////////////////
//SECULAR VARIATION OF ELEMENTS
////////////////////////////////////////////////////////////////////////
class SecularEvolution
{
public:
int Np;
int Nptid;
double* X;
int* Iplanets;
int* Iplanetstid;
LaplaceCoefficients **Laplaces;
LaplaceCoefficients *laplace;
int set(int np,int iplanets[],int nptid,int iplanetstid[],
double *x,LaplaceCoefficients** ls)
{
Np=np;
Iplanets=iplanets;
Nptid=nptid;
Iplanetstid=iplanetstid;
X=(double*)calloc(NUMVARS*Np,sizeof(double));
for(int i=0;i<NUMVARS*Np;i++) X[i]=x[i];
Laplaces=ls;
#ifdef VERBOSE
Verbose("\tSecular evolution properties:\n");
Verbose("\tNumber of planets: %d\n",Np);
Verbose("\tInitial elements: ");
fprintf_vec(stdout,"%e ",X,NUMVARS*Np);
#endif
return 0;
}
int update(const double *x)
{
for(int i=0;i<NUMVARS*Np;i++){X[i]=x[i];}
}
double f2(double alpha){
return alpha*laplace->df0/8.0;
}
double f3(double alpha){
return -0.5*alpha*laplace->df0;
}
double f4(double alpha){
return (4.0*(alpha*alpha*alpha)*laplace->df3 + (alpha*alpha*alpha*alpha)*laplace->df4)/128.0;
}
double f5(double alpha){
return (4.0*alpha*laplace->df1 + 14.0*(alpha*alpha)*laplace->df2 +
8.0*(alpha*alpha*alpha)*laplace->df3 + (alpha*alpha*alpha*alpha)*laplace->df4)/32.0;
}
double f6(double alpha){
return (24.0*alpha*laplace->df1 + 36.0*(alpha*alpha)*laplace->df2 +
12.0*(alpha*alpha*alpha)*laplace->df3 + (alpha*alpha*alpha*alpha)*laplace->df4)/128.0;
}
double f7(double alpha){
return (-2.0*alpha*laplace->df0 - 4.0*(alpha*alpha)*laplace->df01 -
(alpha*alpha*alpha)*laplace->df02)/8.0;
}
double f8(double alpha){
return (3.0/8.0)*(alpha*alpha)*laplace->df22 +
(3.0/4.0)*(alpha*alpha)*laplace->df20;
}
double f9(double alpha){
return 0.5*alpha*laplace->df0 + (3.0/4.0)*(alpha*alpha)*laplace->df22 +
(15.0/4.0)*(alpha*alpha)*laplace->df20;
}
double f10(double alpha){
return 0.25*(2.0*laplace->df10 - 2.0*alpha*laplace->df11 -
(alpha*alpha)*laplace->df12);
}
double f11(double alpha){
return (-4.0*(alpha*alpha)*laplace->df12 - 6.0*(alpha*alpha*alpha)*laplace->df13 -
(alpha*alpha*alpha*alpha)*laplace->df14)/32.0;
}
double f12(double alpha){
return (4.0*laplace->df10 - 4.0*alpha*laplace->df11 - 22.0*(alpha*alpha)*laplace->df12 -
10.0*(alpha*alpha*alpha)*laplace->df13 - (alpha*alpha*alpha*alpha)*laplace->df14)/32.0;
}
double f13(double alpha){
return 0.5*(alpha*alpha)*(laplace->df31 + laplace->df31b) +
(alpha*alpha*alpha)*(laplace->df32 + laplace->df32b)/8.0;
}
double f14(double alpha){
return alpha*laplace->df0;
}
double f15(double alpha){
return 0.5*alpha*laplace->df0 + (alpha*alpha)*laplace->df01 +
0.25*(alpha*alpha*alpha)*laplace->df02;
}
double f16(double alpha){
return -0.5*alpha*laplace->df0 - 3.0*(alpha*alpha)*laplace->df20 -
1.5*(alpha*alpha)*laplace->df22;
}
double f17(double alpha){
return (12.0*laplace->df100 - 12.0*alpha*laplace->df101 +
6.0*(alpha*alpha)*laplace->df102 +
8.0*(alpha*alpha*alpha)*laplace->df103 + (alpha*alpha*alpha*alpha)*laplace->df104)/64.0;
}
double f18(double alpha){
return 0.75*alpha*laplace->df0 + 0.5*(alpha*alpha)*laplace->df01 +
(alpha*alpha*alpha)*laplace->df02/16.0;
}
double f19(double alpha){
return -0.5*(alpha*alpha)*laplace->df31 - (alpha*alpha*alpha)*laplace->df32/8.0;
}
double f20(double alpha){
return (alpha*alpha*alpha)*laplace->df02/16.0;
}
double f21(double alpha){
return -1.5*alpha*laplace->df0 - (alpha*alpha)*laplace->df01 -
(alpha*alpha*alpha)*laplace->df02/8.0;
}
double f22(double alpha){
return -(alpha*alpha)*laplace->df31 - (alpha*alpha*alpha)*laplace->df32/4.0;
}
double f23(double alpha){
return -(alpha*alpha )*laplace->df31b - (alpha*alpha*alpha)*laplace->df32b/4.0;
}
double f24(double alpha){
return (alpha*alpha)*laplace->df31 + (alpha*alpha*alpha)*laplace->df32/4.0;
}
double f25(double alpha){
return - (alpha*alpha*alpha)*laplace->df02/8.0;
}
double f26(double alpha){
return 0.5*alpha*laplace->df0 + 0.75*(alpha*alpha)*laplace->df20 +
1.5*(alpha*alpha)*laplace->df22;
}
int secular(double *dxdt)
{
/*
//Pert. INTERNA O EXTERNA
//d/dt de e, i, \varpi, \Omega
//4o orden en e / si = sin((1/2)*xi)
*/
double a,e,xi,Om,w;
double aM,eM,xiM,OmM,wM,mp;
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//VARIABLE DECLARATION
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
int i,ki,kk,ikk,j,kj;
double aux,si,sini,Omega;
double dxdtp1,dxdtp2,dxdtp3,dxdtp4;
double wp0;
double Op0;
int nkk;
double wp[16],Op[16],xep[16],xip[16],Rcos[16],Rsin[16];
double mpla;
double apla;
double epla;
double sipla;
double alpha;
double Cn;
double Cwe;
double Cwi;
double Ce;
double Ciw;
double Cin;
double nodo_pla;
double w_pla;
double nodo;
double dc0;
double c0;
double c1;
double dc1;
double a1;
double c2;
double dc2;
double a2;
double c3;
double a3;
double c4;
double dc4;
double a4;
double c5;
double dc5;
double a5;
double dc6;
double a6;
double c7;
double dc7;
double a7;
double c8;
double dc8;
double a8;
double c9;
double dc9;
double a9;
double c10;
double dc10;
double a10;
double dc11;
double a11;
double c12;
double a12;
double c13;
double a13;
double dc15;
double a15;
double facint=1;
for(i=0;i<Np;i++){
ki=NUMVARS*i;
//fprintf(stdout,"Perturbed Planet %d (ki=%d):\n",i,ki);
a=X[0+ki];
e=X[1+ki];
xi=X[2+ki];
Om=X[3+ki];
w=X[4+ki];
//Set to zero xi,Omega,w+Omega
/*
dxdt[2+ki]=0.0;
dxdt[3+ki]=0.0;
dxdt[4+ki]=0.0;
*/
aux = sqrt(1.0-e*e);
si = sin(xi/2.0);
sini = sin(xi);
Omega = sqrt(GPROG*Bodies[0].M/(a*a*a));
for(j=0;j<Np;j++){
if(i==j) continue;
kj=NUMVARS*j;
//fprintf(stdout,"\tPerturber Planet %d (ki=%d):\n",j,kj);
aM=X[0+kj];
eM=X[1+kj];
xiM=X[2+kj];
OmM=X[3+kj];
wM=X[4+kj];
mp=Laplaces[i][j].Mp;
//fprintf(stdout,"\tmp =%e\n",mp);
laplace=&Laplaces[i][j];
wp0 = 0.0;
Op0 = 0.0;
nkk = 15;