Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInference 4.1.9.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LALInferencePriorVolumes.c
Go to the documentation of this file.
1
2/*
3 * LALInferencePriorVolumes.c
4 *
5 * Copyright (C) 2016 Salvatore Vitale
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with with program; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301 USA
22 */
23
24
25#include <stdio.h>
26#include <math.h>
27#include <gsl/gsl_integration.h>
28#include <lal/LALInference.h>
29#include <lal/LALInferencePriorVolumes.h>
30#include <lal/LALInferencePrior.h>
31#include <lal/LALCosmologyCalculator.h>
32
33typedef struct {
34 double mc;
37
38typedef struct {
41
42
43static double chirp_to_comp_jacobian(double mc,double q);
44static double mass_indicator(double mc, double q,LALInferenceVariables *priorParams);
45static double integrand(double q,void *params);
46static double inner_integral(double mc, void *params);
47static double mass_outer_integral(LALInferenceVariables *priorArgs);
48static double loudness_volume(LALInferenceRunState *state);
49
50static double chirp_to_comp_jacobian(double mc,double q){
51
52 double factor = mc * pow(1.0 +q, 0.2);
53 double m1 = factor * pow(q, -0.6);
54 return m1*m1/mc;
55
56}
57
58static double mass_indicator(double mc, double q,LALInferenceVariables *priorParams){
59 /*
60 *
61 * This function gets mchirp and q
62 * Calculates component and total mass and check if all parameters
63 * are within their priors.
64 *
65 * Return 1 or 0
66 *
67 * TODO: make it work with eta
68 *
69 * */
70 double factor = mc * pow(1.0 +q, 0.2);
71 double m1 = factor * pow(q, -0.6);
72 double m2 = factor * pow(q, 0.4);
73
74 /* Check for individual mass priors */
75 if(LALInferenceCheckVariable(priorParams,"mass1_min"))
76 if(LALInferenceGetREAL8Variable(priorParams,"mass1_min") > m1)
77 return 0.0;
78 if(LALInferenceCheckVariable(priorParams,"mass1_max"))
79 if(LALInferenceGetREAL8Variable(priorParams,"mass1_max") < m1)
80 return 0.0;
81 if(LALInferenceCheckVariable(priorParams,"mass2_min"))
82 if(LALInferenceGetREAL8Variable(priorParams,"mass2_min") > m2)
83 return 0.0;
84 if(LALInferenceCheckVariable(priorParams,"mass2_max"))
85 if(LALInferenceGetREAL8Variable(priorParams,"mass2_max") < m2)
86 return 0.0;
87 if(LALInferenceCheckVariable(priorParams,"MTotMax"))
88 if(*(REAL8 *)LALInferenceGetVariable(priorParams,"MTotMax") < m1+m2)
89 return 0.0;
90 if(LALInferenceCheckVariable(priorParams,"MTotMin"))
91 if(*(REAL8 *)LALInferenceGetVariable(priorParams,"MTotMin") > m1+m2)
92 return 0.0;
93 if (LALInferenceCheckVariable(priorParams,"q_min")){
94 double q_min,q_max;
95 LALInferenceGetMinMaxPrior(priorParams, "q", &q_min, &q_max);
96 if (q<q_min || q>q_max) return 0.0;
97 }
98 if (LALInferenceCheckVariable(priorParams,"chirpmass_min")){
99 double mc_min,mc_max;
100 LALInferenceGetMinMaxPrior(priorParams, "chirpmass", &mc_min, &mc_max);
101 if (mc<mc_min || mc>mc_max) return 0.0;
102 }
103 return 1.0;
104 /*
105 if (LALInferenceCheckVariable(priorParams,"eta_min")){
106 double eta_min,eta_max;
107 LALInferenceGetMinMaxPrior(state->priorArgs, "eta", &eta_min, &eta_max);
108 if (eta<eta_min || eta>eta_max) return 0.0;
109 }*/
110}
111
112static double integrand(double q,void *params){
113
114 /* Integrand for the dobule integral over Mc and q
115 *
116 * This is the jacobian within the support of the prior, zero otherwise
117 *
118 * */
119 innerParams iData = *(innerParams *)params;
120 double mc= iData.mc;
121
123
124}
125
126static double inner_integral(double mc, void *params){
127
128 // q comes through params
129 gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
130 double result, error;
131 const double epsabs = 1e-4;
132 const double epsrel = 1e-4;
133 const size_t wsSize = 10000;
134 gsl_function F;
135 F.function = &integrand;
136 innerParams iParams;
137 F.params = &iParams;
138 outerParams oParams=*(outerParams *) params;
139 iParams.mc=mc;
140 iParams.prior= (LALInferenceVariables *) oParams.prior;
141
142 double q_min,q_max;
143 if (LALInferenceCheckVariable(iParams.prior,"q_min")){
144
145 LALInferenceGetMinMaxPrior(iParams.prior, "q", &q_min, &q_max);
146 }
147 else{
148 fprintf(stderr,"ERROR: q doesn't seem to be a valid param. Exiting\n");
149 exit(1);
150 }
151 // TODO: make it work with eta
152 int status = gsl_integration_qags (&F, q_min, q_max,epsabs, epsrel, wsSize,
153 w, &result, &error);
154 if (status)
155 XLAL_ERROR_REAL8(XLAL_EFUNC | XLAL_EDATA, "Bad data; GSL integration failed.");
156
157 gsl_integration_workspace_free(w);
158 return result;
159 }
160
161
163
164 gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
165 double result, error;
166 const double epsabs = 1e-4;
167 const double epsrel = 1e-4;
168 const size_t wsSize = 10000;
169
170 gsl_function F;
171 F.function = &inner_integral;
172 outerParams oParams;
173 F.params = &oParams;
174 oParams.prior=priorArgs;
175
176 double mc_min,mc_max;
177 if (LALInferenceCheckVariable(priorArgs,"chirpmass_min")){
178
179 LALInferenceGetMinMaxPrior(priorArgs, "chirpmass", &mc_min, &mc_max);
180 }
181 else{
182 fprintf(stderr,"ERROR: chirpmass doesn't seem to be a valid param. Exiting\n");
183 exit(1);
184 }
185
186 /* Turn off the error handler - must check return values */
187 gsl_error_handler_t *oldhandler=gsl_set_error_handler_off();
188 /* this integrates on chirpmass */
189 int status = gsl_integration_qags (&F, mc_min, mc_max, epsabs, epsrel, wsSize, w, &result, &error);
190 /* Restore error handler */
191 gsl_set_error_handler(oldhandler);
192 /* Free workspace */
193 gsl_integration_workspace_free(w);
194
195 /* Check for errors */
196 if (status)
197 XLAL_ERROR_REAL8(XLAL_EFUNC | XLAL_EDATA, "GSL integration failed in %s. GSL error:\n%s",__func__,gsl_strerror(status));
198
199 return result;
200 }
201
203
204typedef struct {
209
210static double distance_prior(double d,LALCosmologicalParameters *omega){
211 (void) omega;
212 return d*d;
213}
214static double logdistance_prior(double ld,LALCosmologicalParameters *omega){
215 (void) omega;
216 return ld*ld*ld;
217}
218static double redshift_prior(double z,LALCosmologicalParameters *omega){
219 return XLALUniformComovingVolumeDensity(z,omega);
220}
221
222static double loudness_integrand(double x,void *params){
223
226 LALCosmologicalParameters *omega=lParams.omega;
227 return priorf(x,omega);
228}
229
231
232
233 LALInferenceVariables *priorArgs=state->priorArgs;
234 gsl_integration_workspace * w = gsl_integration_workspace_alloc (10000);
235 double result, error;
236 const double epsabs = 1e-4;
237 const double epsrel = 1e-4;
238 const size_t wsSize = 10000;
239
240 gsl_function F;
241 F.function = &loudness_integrand;
242 loudnessParams lParams;
243 F.params = &lParams;
244 double intmin,intmax;
245 if (LALInferenceCheckVariable(priorArgs,"redshift_min")){
246 lParams.priorfunc=&redshift_prior;
247 // WILL need to enable this after we push the cosmology patch, for the moment set to null
248 //lParams.omega=state->omega;
249 lParams.omega=NULL;
250
251 LALInferenceGetMinMaxPrior(priorArgs, "redshift", &intmin, &intmax);
252 }
253 else if (LALInferenceCheckVariable(priorArgs,"distance_min")){
254 lParams.priorfunc=&distance_prior;
255 lParams.omega=NULL;
256 LALInferenceGetMinMaxPrior(priorArgs, "distance", &intmin, &intmax);
257 }
258 else if (LALInferenceCheckVariable(priorArgs,"logdistance_min")){
260 lParams.omega=NULL;
261 LALInferenceGetMinMaxPrior(priorArgs, "logdistance", &intmin, &intmax);
262 }
263 else XLAL_ERROR_REAL8(XLAL_EINVAL,"No known distance parameter found, unable to proceed\n");
264
265 lParams.priorargs=priorArgs;
266 int status = gsl_integration_qags (&F, intmin, intmax, epsabs, epsrel, wsSize, w, &result, &error);
267
268 if (status)
269 XLAL_ERROR_REAL8(XLAL_EFUNC | XLAL_EDATA, "Bad data; GSL integration failed.");
270
271 gsl_integration_workspace_free(w);
272 return result;
273
274}
275
277
278 LALInferenceVariables *priorArgs=state->priorArgs;
279 return mass_outer_integral(priorArgs);
280}
281
283
284 return LALInferenceMassPriorVolume(state)*loudness_volume(state);
285}
double XLALUniformComovingVolumeDensity(double z, void *omega)
static double loudness_integrand(double x, void *params)
static double loudness_volume(LALInferenceRunState *state)
double LALInferenceMassDistancePriorVolume(LALInferenceRunState *state)
static double redshift_prior(double z, LALCosmologicalParameters *omega)
static double chirp_to_comp_jacobian(double mc, double q)
static double distance_prior(double d, LALCosmologicalParameters *omega)
double LALInferenceMassPriorVolume(LALInferenceRunState *state)
static double mass_outer_integral(LALInferenceVariables *priorArgs)
static double inner_integral(double mc, void *params)
static double integrand(double q, void *params)
static double logdistance_prior(double ld, LALCosmologicalParameters *omega)
static double mass_indicator(double mc, double q, LALInferenceVariables *priorParams)
REAL8(* LALInferenceLoudnessPriorFunction)(double x, LALCosmologicalParameters *omega)
#define fprintf
double e
const double w
double REAL8
REAL8 LALInferenceGetREAL8Variable(LALInferenceVariables *vars, const char *name)
void * LALInferenceGetVariable(const LALInferenceVariables *vars, const char *name)
Return a pointer to the memory the variable vars is stored in specified by name User must cast this p...
Definition: LALInference.c:238
int LALInferenceCheckVariable(LALInferenceVariables *vars, const char *name)
Checks for name being present in vars returns 1(==true) or 0.
Definition: LALInference.c:525
void LALInferenceGetMinMaxPrior(LALInferenceVariables *priorArgs, const char *name, REAL8 *min, REAL8 *max)
Get the minimum and maximum values of the uniform prior from the priorArgs list, given a name.
static const INT4 q
#define XLAL_ERROR_REAL8(...)
XLAL_EDATA
XLAL_EFUNC
XLAL_EINVAL
Structure containing inference run state This includes pointers to the function types required to run...
Definition: LALInference.h:592
LALInferenceVariables * priorArgs
Common arguments needed by proposals, to be copied to thread->cycle.
Definition: LALInference.h:603
The LALInferenceVariables structure to contain a set of parameters Implemented as a linked list of LA...
Definition: LALInference.h:170
LALInferenceVariables * prior
LALInferenceVariables * priorargs
LALInferenceLoudnessPriorFunction priorfunc
LALCosmologicalParameters * omega
LALInferenceVariables * prior