LALSimulation  5.4.0.1-fe68b98
LALSimIMRSpinEOBHcapExactDerivative.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2010 Craig Robinson, Yi Pan, 2016 Zachariah Etienne,
3 * Caleb Devine
4 * David N. Buch
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with with program; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21 
22 
23 #ifndef _LALSIMIMRSPINEOBHCAPEXACTDERIVATIVE_C
24 #define _LALSIMIMRSPINEOBHCAPEXACTDERIVATIVE_C
25 
26 #ifdef __GNUC__
27 #define UNUSED __attribute__ ((unused))
28 #else
29 #define UNUSED
30 #endif
31 
32 #include <math.h>
33 #include <gsl/gsl_deriv.h>
34 #include <lal/LALSimInspiral.h>
35 #include <lal/LALSimIMR.h>
36 
37 #include "LALSimIMRSpinEOB.h"
38 
40 //#include "LALSimIMRSpinEOBHamiltonianOptimized.c"
41 
42 
43 /*------------------------------------------------------------------------------------------
44  *
45  * Prototypes of functions defined in this code.
46  *
47  *------------------------------------------------------------------------------------------
48  */
49 
50 
52  const INT4 paramIdx,
53  const REAL8 values[],
55  );
56 
58  const INT4 paramIdx, /**<< Index of the parameters */
59  const REAL8 values[], /**<< Dynamical variables */
60  SpinEOBParams *funcParams /**<< EOB Parameters */
61  );
62 
64 
65 static double GSLSpinAlignedHamiltonianWrapper_ExactDeriv( double x, void *params );
66 
68  INT4 which_to_vary, /**<< Take a derivative with respect to "which_to_vary" variable */
69  const REAL8 eta, /**<< Symmetric mass ratio */
70  REAL8Vector * restrict x, /**<< Position vector */
71  REAL8Vector * restrict p, /**<< Momentum vector (tortoise radial component pr*) */
72  REAL8Vector * restrict s1Vec, /**<< Spin vector 1 */
73  REAL8Vector * restrict s2Vec, /**<< Spin vector 2 */
74  REAL8Vector * restrict sigmaKerr, /**<< Spin vector sigma_kerr */
75  REAL8Vector * restrict sigmaStar, /**<< Spin vector sigma_star */
76  INT4 tortoise, /**<< flag to state whether the momentum is the tortoise co-ord */
77  SpinEOBHCoeffs *coeffs /**<< Structure containing various coefficients */
78  );
79 
81  REAL8 output[6], /**<< Output vector (contains all derivatives, though WARNING: known unused derivatives may be set to zero) */
82  const REAL8 eta, /**<< Symmetric mass ratio */
83  REAL8Vector * restrict x, /**<< Position vector */
84  REAL8Vector * restrict p, /**<< Momentum vector (tortoise radial component pr*) */
85  REAL8Vector * restrict s1Vec, /**<< Spin vector 1 */
86  REAL8Vector * restrict s2Vec, /**<< Spin vector 2 */
87  REAL8Vector * restrict sigmaKerr, /**<< Spin vector sigma_kerr */
88  REAL8Vector * restrict sigmaStar, /**<< Spin vector sigma_star */
89  INT4 tortoise, /**<< flag to state whether the momentum is the tortoise co-ord */
90  SpinEOBHCoeffs *coeffs /**<< Structure containing various coefficients */
91  );
92 /*------------------------------------------------------------------------------------------
93  *
94  * Definitions of functions.
95  *
96  *------------------------------------------------------------------------------------------
97  */
98 
100  const INT4 paramIdx, /**<< Index of the parameters */
101  const REAL8 values[], /**<< Dynamical variables */
102  SpinEOBParams *funcParams /**<< EOB Parameters */
103  )
104 {
106  /* Set up pointers for GSL */
107  params.values = values;
108  params.params = funcParams;
109  params.varyParam = paramIdx;
110 
111  REAL8 result;
112  if(paramIdx>=0 && paramIdx<6) {
113  REAL8 output[6];
115  result=output[paramIdx];
116  } else {
118  }
119 
120  return result;
121 }
122 
123 /* Wrapper for computing exact derivatives of the Hamiltonian */
124 static double GSLSpinAlignedHamiltonianWrapper_ExactDeriv( double x, void *params )
125 {
126  HcapDerivParams *dParams = (HcapDerivParams *)params;
127 
128  EOBParams *eobParams = dParams->params->eobParams;
129 
130  REAL8 tmpVec[6];
131 
132  /* These are the vectors which will be used in the call to the Hamiltonian */
133  REAL8Vector r, p;
134  REAL8Vector *s1Vec = dParams->params->s1Vec;
135  REAL8Vector *s2Vec = dParams->params->s2Vec;
136  REAL8Vector *sigmaKerr = dParams->params->sigmaKerr;
137  REAL8Vector *sigmaStar = dParams->params->sigmaStar;
138 
139  /* Use a temporary vector to avoid corrupting the main function */
140  memcpy( tmpVec, dParams->values,
141  sizeof(tmpVec) );
142 
143  /* Set the relevant entry in the vector to the correct value */
144  tmpVec[dParams->varyParam] = x;
145 
146  /* Set the LAL-style vectors to point to the appropriate things */
147  r.length = p.length = 3;
148  r.data = tmpVec;
149  p.data = tmpVec+3;
150 
151  return XLALSimIMRSpinEOBHamiltonian_ExactDeriv(dParams->varyParam, eobParams->eta, &r, &p, s1Vec, s2Vec, sigmaKerr, sigmaStar, dParams->params->tortoise, dParams->params->seobCoeffs ) / eobParams->eta;
152 }
153 
154 /* Wrapper for GSL to call the Hamiltonian function */
156 {
157  HcapDerivParams *dParams = (HcapDerivParams *)params;
158 
159  EOBParams *eobParams = dParams->params->eobParams;
160 
161  REAL8 tmpVec[6];
162  REAL8 returnval=0;
163  /* These are the vectors which will be used in the call to the Hamiltonian */
164  REAL8Vector r, p;
165  REAL8Vector *s1Vec = dParams->params->s1Vec;
166  REAL8Vector *s2Vec = dParams->params->s2Vec;
167  REAL8Vector *sigmaKerr = dParams->params->sigmaKerr;
168  REAL8Vector *sigmaStar = dParams->params->sigmaStar;
169 
170  /* Use a temporary vector to avoid corrupting the main function */
171  memcpy( tmpVec, dParams->values,
172  sizeof(tmpVec) );
173 
174  /* Set the relevant entry in the vector to the correct value */
175  for(int i=0;i<6;i++) tmpVec[i] = input[i];
176  //tmpVec[dParams->varyParam] = x;
177 
178  /* Set the LAL-style vectors to point to the appropriate things */
179  r.length = p.length = 3;
180  r.data = tmpVec;
181  p.data = tmpVec+3;
182 
183  XLALSimIMRSpinEOBHamiltonian_derivs_allatonce(output, eobParams->eta, &r, &p, s1Vec, s2Vec, sigmaKerr, sigmaStar, dParams->params->tortoise, dParams->params->seobCoeffs );
184 
185  for(int i=0;i<6;i++) output[i] /= eobParams->eta;
186 
187  return returnval;
188 }
189 
190 /**
191  * Calculate the derivative of the Hamiltonian w.r.t. a specific parameter
192  * Used by generic spin EOB model, including initial conditions solver.
193  */
195  const INT4 paramIdx, /**<< Index of the parameters */
196  const REAL8 values[], /**<< Dynamical variables */
197  SpinEOBParams *funcParams /**<< SEOB Parameters */
198  )
199 {
200  REAL8 result;
201 
202  REAL8Vector *sigmaKerr = funcParams->sigmaKerr;
203  REAL8Vector *sigmaStar = funcParams->sigmaStar;
204  SpinEOBHCoeffs *coeffs = funcParams->seobCoeffs;
205  REAL8Vector *s1Vec = funcParams->s1Vec;
206  REAL8Vector *s2Vec = funcParams->s2Vec;
207  REAL8Vector *x=NULL;
208  REAL8Vector *p=NULL;
209  /* Note that this function is called a limited number of times in the initial condition setup,
210  * so no worries about the below memory allocations slowing the code... at this point. */
213  memcpy(x->data,&values[0],3*sizeof(REAL8));
214  memcpy(p->data,&values[3],3*sizeof(REAL8));
215 
216  REAL8 eta = funcParams->eobParams->eta;
217  REAL8 e3z = (0.0 <= sigmaKerr->data[2]) - (sigmaKerr->data[2] < 0.0); // This is a modified sign function: e3z = 1 if sigmaKerr->data[2]>=0, -1 otherwise
218 
219  /* Now calculate derivatives w.r.t. the required parameter */
220  if (funcParams->tortoise==1){
221  if (paramIdx==4){
223  result = dHdp1/eta;
224  } else if (paramIdx==3){
226  result = dHdp0/eta;
227  } else if (paramIdx==5){
229  result = dHdp2/eta;
230  } else if (paramIdx==0){
232  result = dHdx0/eta;
233  } else {
235  }
236  }else if(funcParams->tortoise==0) {
237  if (paramIdx==4){
239  result = dHdp1/eta;
240  } else if (paramIdx==3){
242  result = dHdp0/eta;
243  } else if (paramIdx==5){
245  result = dHdp2/eta;
246  } else if (paramIdx==0){
248  result = dHdx0/eta;
249  } else {
251  }
252  } else {
254  }
257 
258  return result;
259 }
260 
261 /* Evaluates derivative of Hamiltonian with respect to py.
262  * We've made it extensible in case we'd like to take other derivs. */
264  INT4 which_to_vary,
265  const REAL8 eta, /**<< Symmetric mass ratio */
266  REAL8Vector * restrict x, /**<< Position vector */
267  REAL8Vector * restrict p, /**<< Momentum vector (tortoise radial component pr*) */
268  REAL8Vector * restrict s1Vec, /**<< Spin vector 1 */
269  REAL8Vector * restrict s2Vec, /**<< Spin vector 2 */
270  REAL8Vector * restrict sigmaKerr, /**<< Spin vector sigma_kerr */
271  REAL8Vector * restrict sigmaStar, /**<< Spin vector sigma_star */
272  INT4 tortoise, /**<< flag to state whether the momentum is the tortoise co-ord */
273  SpinEOBHCoeffs * coeffs /**<< Structure containing various coefficients */
274  )
275 {
276  REAL8 e3z = (0.0 <= sigmaKerr->data[2]) - (sigmaKerr->data[2] < 0.0); // This is a modified sign function: e3z = 1 if sigmaKerr->data[2]>=0, -1 otherwise
277  if(tortoise==1) {
278  switch(which_to_vary) {
279  case 4:
280  {
282  return Hreal; /* Returns dH/dpy */
283  }
284  break;
285  default:
286  {
287  printf("XLALSimIMRSpinEOBHamiltonian_ExactDeriv(): Derivative option not supported: %d!\n",which_to_vary); exit(1);
288  break;
289  }
290  }
291  } else {
292  switch(which_to_vary) {
293  case 4:
294  {
296  return Hreal; /* Returns dH/dpy */
297  }
298  break;
299  default:
300  {
301  printf("XLALSimIMRSpinEOBHamiltonian_ExactDeriv(): Derivative option not supported: %d!\n",which_to_vary); exit(1);
302  break;
303  }
304  }
305  }
306  return -1e-300;
307 }
308 
309 
310 /**
311  * Wrapper for GSL to call the Hamiltonian function
312  */
313 /* static REAL8 GSLSpinHamiltonianWrapperOptimized( REAL8 x, void *params ) */
314 /* { */
315 /* HcapDerivParams *dParams = (HcapDerivParams *)params; */
316 
317 /* EOBParams *eobParams = dParams->params->eobParams; */
318 
319 /* REAL8 tmpVec[12]; */
320 /* REAL8 s1normData[3], s2normData[3], sKerrData[3], sStarData[3]; */
321 
322 /* /\* These are the vectors which will be used in the call to the Hamiltonian *\/ */
323 /* REAL8Vector r, p, spin1, spin2, spin1norm, spin2norm; */
324 /* REAL8Vector sigmaKerr, sigmaStar; */
325 
326 /* int i; */
327 /* REAL8 a; */
328 /* REAL8 m1 = eobParams->m1; */
329 /* REAL8 m2 = eobParams->m2; */
330 /* REAL8 mT2 = (m1+m2)*(m1+m2); */
331 
332 /* /\* Use a temporary vector to avoid corrupting the main function *\/ */
333 /* memcpy( tmpVec, dParams->values, sizeof(tmpVec) ); */
334 
335 /* /\* Set the relevant entry in the vector to the correct value *\/ */
336 /* tmpVec[dParams->varyParam] = x; */
337 
338 /* /\* Set the LAL-style vectors to point to the appropriate things *\/ */
339 /* r.length = p.length = spin1.length = spin2.length = spin1norm.length = spin2norm.length = 3; */
340 /* sigmaKerr.length = sigmaStar.length = 3; */
341 /* r.data = tmpVec; */
342 /* p.data = tmpVec+3; */
343 /* spin1.data = tmpVec+6; */
344 /* spin2.data = tmpVec+9; */
345 /* spin1norm.data = s1normData; */
346 /* spin2norm.data = s2normData; */
347 /* sigmaKerr.data = sKerrData; */
348 /* sigmaStar.data = sStarData; */
349 
350 /* memcpy( s1normData, tmpVec+6, 3*sizeof(REAL8) ); */
351 /* memcpy( s2normData, tmpVec+9, 3*sizeof(REAL8) ); */
352 
353 /* for ( i = 0; i < 3; i++ ) */
354 /* { */
355 /* s1normData[i] /= mT2; */
356 /* s2normData[i] /= mT2; */
357 /* } */
358 
359 /* /\* Calculate various spin parameters *\/ */
360 /* XLALSimIMRSpinEOBCalculateSigmaKerr( &sigmaKerr, eobParams->m1, */
361 /* eobParams->m2, &spin1, &spin2 ); */
362 /* XLALSimIMRSpinEOBCalculateSigmaStar( &sigmaStar, eobParams->m1, */
363 /* eobParams->m2, &spin1, &spin2 ); */
364 /* a = sqrt( sigmaKerr.data[0]*sigmaKerr.data[0] + sigmaKerr.data[1]*sigmaKerr.data[1] */
365 /* + sigmaKerr.data[2]*sigmaKerr.data[2] ); */
366 /* //printf( "a = %e\n", a ); */
367 /* //printf( "aStar = %e\n", sqrt( sigmaStar.data[0]*sigmaStar.data[0] + sigmaStar.data[1]*sigmaStar.data[1] + sigmaStar.data[2]*sigmaStar.data[2]) ); */
368 /* if ( isnan( a ) ) */
369 /* { */
370 /* printf( "a is nan!!\n"); */
371 /* } */
372 /* //XLALSimIMRCalculateSpinEOBHCoeffs( dParams->params->seobCoeffs, eobParams->eta, a ); */
373 /* if (UsePrec) */
374 /* { */
375 /* /\* Set up structures and calculate necessary PN parameters *\/ */
376 /* /\* Due to precession, these need to get calculated in every step *\/ */
377 /* /\* TODO: Only calculate non-spinning parts once *\/ */
378 /* memset( dParams->params->seobCoeffs, 0, sizeof(SpinEOBHCoeffs) ); */
379 
380 /* /\* Update the Z component of the Kerr background spin */
381 /* * Pre-compute the Hamiltonian coefficients *\/ */
382 /* REAL8Vector *delsigmaKerr = dParams->params->sigmaKerr; */
383 /* dParams->params->sigmaKerr = &sigmaKerr; */
384 /* dParams->params->a = a; */
385 /* //tmpsigmaKerr->data[2]; */
386 /* if ( XLALSimIMRCalculateSpinEOBHCoeffs( dParams->params->seobCoeffs, */
387 /* eobParams->eta, a, */
388 /* dParams->params->seobCoeffs->SpinAlignedEOBversion ) == XLAL_FAILURE ) */
389 /* { */
390 /* XLALDestroyREAL8Vector( dParams->params->sigmaKerr ); */
391 /* XLAL_ERROR( XLAL_EFUNC ); */
392 /* } */
393 
394 /* /\* Release the old memory *\/ */
395 /* XLALDestroyREAL8Vector( delsigmaKerr ); */
396 /* } */
397 
398 /* //printf( "Hamiltonian = %e\n", XLALSimIMRSpinEOBHamiltonian( eobParams->eta, &r, &p, &sigmaKerr, &sigmaStar, dParams->params->seobCoeffs ) ); */
399 /* return XLALSimIMRSpinEOBHamiltonianOptimized( eobParams->eta, &r, &p, &spin1norm, &spin2norm, &sigmaKerr, &sigmaStar, dParams->params->tortoise, dParams->params->seobCoeffs ) / eobParams->eta; */
400 /* } */
401 
403  REAL8 output[6],
404  const REAL8 eta, /**<< Symmetric mass ratio */
405  REAL8Vector * restrict x, /**<< Position vector */
406  REAL8Vector * restrict p, /**<< Momentum vector (tortoise radial component pr*) */
407  REAL8Vector * restrict s1Vec, /**<< Spin vector 1 */
408  REAL8Vector * restrict s2Vec, /**<< Spin vector 2 */
409  REAL8Vector * restrict sigmaKerr, /**<< Spin vector sigma_kerr */
410  REAL8Vector * restrict sigmaStar, /**<< Spin vector sigma_star */
411  INT4 tortoise, /**<< flag to state whether the momentum is the tortoise co-ord */
412  SpinEOBHCoeffs *coeffs /**<< Structure containing various coefficients */
413  )
414 {
415  REAL8 returnval=0;
416  REAL8 e3z = (0.0 <= sigmaKerr->data[2]) - (sigmaKerr->data[2] < 0.0); // This is a modified sign function: e3z = 1 if sigmaKerr->data[2]>=0, -1 otherwise
417  if(tortoise==1) {
418  // FASTEST OPTION. Note that it sets g2=[xdata2 derivative]=0.
420  output[0]=g0;
421  output[1]=g1;
422  output[2]=g2;
423  output[3]=g3;
424  output[4]=g4;
425  output[5]=g5;
426  } else {
428  output[0]=g0;
429  output[1]=g1;
430  output[2]=g2;
431  output[3]=g3;
432  output[4]=g4;
433  output[5]=g5;// printf("ERROR: NOT EXPECTING THAT %d.\n",tortoise); exit(1);
434  }
435 
436  return returnval;
437 }
438 #endif /* _LALSIMIMRSPINEOBHCAPEXACTDERIVATIVE_C */
const double g3
const double g2
const double g0
const double g1
static REAL8 XLALSimIMRSpinEOBHamiltonian_derivs_allatonce(REAL8 output[6], const REAL8 eta, REAL8Vector *restrict x, REAL8Vector *restrict p, REAL8Vector *restrict s1Vec, REAL8Vector *restrict s2Vec, REAL8Vector *restrict sigmaKerr, REAL8Vector *restrict sigmaStar, INT4 tortoise, SpinEOBHCoeffs *coeffs)
Wrapper for GSL to call the Hamiltonian function.
static REAL8 XLALSimIMRSpinEOBHamiltonian_ExactDeriv(INT4 which_to_vary, const REAL8 eta, REAL8Vector *restrict x, REAL8Vector *restrict p, REAL8Vector *restrict s1Vec, REAL8Vector *restrict s2Vec, REAL8Vector *restrict sigmaKerr, REAL8Vector *restrict sigmaStar, INT4 tortoise, SpinEOBHCoeffs *coeffs)
static REAL8 XLALSpinHcapExactDerivWRTParam(const INT4 paramIdx, const REAL8 values[], SpinEOBParams *params)
Calculate the derivative of the Hamiltonian w.r.t.
static REAL8 GSLSpinAlignedHamiltonianWrapper_derivs_allatonce(REAL8 output[6], const REAL8 input[6], void *params)
static double GSLSpinAlignedHamiltonianWrapper_ExactDeriv(double x, void *params)
static REAL8 XLALSpinHcapHybDerivWRTParam(const INT4 paramIdx, const REAL8 values[], SpinEOBParams *funcParams)
REAL8 g4
REAL8 g5
REAL8 Hreal
REAL8 dHdp0
REAL8 dHdp1
REAL8 dHdp2
REAL8 dHdx0
double i
Definition: bh_ringdown.c:118
double e
Definition: bh_ringdown.c:117
double REAL8
int32_t INT4
static const INT4 r
REAL8Vector * XLALCreateREAL8Vector(UINT4 length)
void XLALDestroyREAL8Vector(REAL8Vector *vector)
#define XLAL_ERROR(...)
XLAL_EFUNC
list x
list p
Structure containing all the parameters needed for the EOB waveform.
const REAL8 * values
SpinEOBParams * params
REAL8 * data
Parameters for the spinning EOB model, used in calculating the Hamiltonian.
Parameters for the spinning EOB model.
SpinEOBHCoeffs * seobCoeffs
REAL8Vector * sigmaStar
REAL8Vector * s1Vec
EOBParams * eobParams
REAL8Vector * sigmaKerr
REAL8Vector * s2Vec
Definition: burst.c:245
LALDict * params
Definition: inspiral.c:248
char output[FILENAME_MAX]
Definition: unicorn.c:26