Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALSimulation 6.2.0.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ns-mass-radius.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 J. Creighton, B. Lackey
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20#include <math.h>
21#include <stdio.h>
22#include <stdlib.h>
23
24#include <lal/LALConstants.h>
25#include <lal/LALgetopt.h>
26#include <lal/LALStdlib.h>
27#include <lal/LALSimNeutronStar.h>
28
29int usage(const char *program);
30int parseargs(int argc, char *argv[]);
31int output(const char *fmt, double c, double m, double r, double w, double z, double k2);
32
33/* global variables */
34
36
37const char *const default_fmt = "%r\\t%m\\n";
38const char *const default_fmt_U = "%r\\t%m\\t%w\\t%z\\n";
39const char *global_fmt;
40
41double global_epsrel = 0;
42
45
46const long default_npts = 100;
48
49int main(int argc, char *argv[])
50{
51 double logpmin = 75.5;
52 double logpmax;
53 double dlogp;
54 long i;
55
57
60 parseargs(argc, argv);
61
62 if (global_rho_c == 0 && global_e_c != 0)
63 {
65 }
66
67 if (global_rho_c != 0 && global_e_c == 0)
68 {
70 }
71
73 dlogp = (logpmax - logpmin) / global_npts;
74
75 for (i = 0; i < global_npts; ++i) {
76 double pc = exp(logpmin + (0.5 + i) * dlogp);
77 double c, m, r, k2, I1, I2, I3, J1, J2, J3, w, z;
78 if (global_virial == 0)
79 {
80 if (global_epsrel == 0)
81 {
83 }
84 else
85 {
87 }
88 /* convert units */
89 m /= LAL_MSUN_SI; /* mass in solar masses */
90 c = m * LAL_MRSUN_SI / r; /* compactness (dimensionless) */
91 r /= 1000.0; /* radius in km */
92 w = z = 0.0 / 0.0; /* GRV2 and GRV3 (dimesionless) NaN since -U is not used */
93
94 output(global_fmt, c, m, r, w, z, k2);
95 }
96 else if (global_virial == 1)
97 {
98 if (global_epsrel == 0)
99 {
100 XLALSimNeutronStarVirialODEIntegrate(&r, &m, &I1, &I2, &I3, &J1, &J2, &J3, &k2, pc, global_eos);
101 }
102 else
103 {
105 }
106 /* convert units */
107 m /= LAL_MSUN_SI; /* mass in solar masses */
108 c = m * LAL_MRSUN_SI / r; /* compactness (dimensionless) */
109 r /= 1000.0; /* radius in km */
110 w = fabs(I1/(I2 + I3) - 1.0); /* GRV2 (dimesionless) */
111 z = fabs(J1/(J2 + J3) - 1.0); /* GRV2 (dimesionless) */
112
113 output(global_fmt, c, m, r, w, z, k2);
114 }
115 }
116
119 return 0;
120}
121
122int output(const char *fmt, double c, double m, double r, double w, double z, double k2)
123{
124 int i;
125 for (i = 0; fmt[i]; ++i) {
126 switch (fmt[i]) {
127 case '%': /* conversion character */
128 switch (fmt[i + 1]) {
129 case '%':
130 fputc('%', stdout);
131 break;
132 case 'c':
133 fprintf(stdout, "%.18e", c);
134 break;
135 case 'k':
136 fprintf(stdout, "%.18e", k2);
137 break;
138 case 'l':
139 fprintf(stdout, "%.18e", (2.0 / 3.0) * k2 / pow(c, 5));
140 break;
141 case 'm':
142 fprintf(stdout, "%.18e", m);
143 break;
144 case 'r':
145 fprintf(stdout, "%.18e", r);
146 break;
147 case 'w':
148 fprintf(stdout, "%.5e", w);
149 break;
150 case 'z':
151 fprintf(stdout, "%.5e", z);
152 break;
153 default:
154 fprintf(stderr,
155 "unrecognized conversion %%%c in output format\n",
156 fmt[i + 1]);
157 exit(1);
158 break;
159 }
160 ++i;
161 break;
162 case '\\': /* escaped character */
163 switch (fmt[i + 1]) {
164 case '\\':
165 fputc('\\', stdout);
166 break;
167 case 'a':
168 fputc('\a', stdout);
169 break;
170 case 'b':
171 fputc('\b', stdout);
172 break;
173 case 'f':
174 fputc('\f', stdout);
175 break;
176 case 'n':
177 fputc('\n', stdout);
178 break;
179 case 'r':
180 fputc('\r', stdout);
181 break;
182 case 't':
183 fputc('\t', stdout);
184 break;
185 case 'v':
186 fputc('\v', stdout);
187 break;
188 case '\0': /* impossible! */
189 fprintf(stderr,
190 "impossible escaped NUL character in format\n");
191 exit(1);
192 default:
193 fputc(fmt[i + 1], stdout);
194 break;
195 }
196 ++i;
197 break;
198 default:
199 fputc(fmt[i], stdout);
200 break;
201 }
202 }
203 return 0;
204}
205
206int parseargs(int argc, char **argv)
207{
208 struct LALoption long_options[] = {
209 {"help", no_argument, 0, 'h'},
210 {"mass-radius-virial", no_argument, 0, 'U'},
211 {"epsilon_relative", required_argument, 0, 'E'},
212 {"eos-file", required_argument, 0, 'f'},
213 {"eos-name", required_argument, 0, 'n'},
214 {"central-rest-mass-density", required_argument, 0, 'd'},
215 {"central-energy-density", required_argument, 0, 'e'},
216 {"format", required_argument, 0, 'F'},
217 {"npts", required_argument, 0, 'N'},
218 {"polytrope", no_argument, 0, 'P'},
219 {"gamma", required_argument, 0, 'G'},
220 {"pressure", required_argument, 0, 'p'},
221 {"density", required_argument, 0, 'r'},
222 {"piecewisepolytrope", no_argument, 0, 'Q'},
223 {"logp1", required_argument, 0, 'q'},
224 {"gamma1", required_argument, 0, '1'},
225 {"gamma2", required_argument, 0, '2'},
226 {"gamma3", required_argument, 0, '3'},
227 {"4parameterspectraldecomposition", no_argument, 0, 'S'},
228 {"SDgamma0", required_argument, 0, 'w'},
229 {"SDgamma1", required_argument, 0, 'x'},
230 {"SDgamma2", required_argument, 0, 'y'},
231 {"SDgamma3", required_argument, 0, 'z'},
232 {0, 0, 0, 0}
233 };
234 char args[] = "hUE:f:n:d:e:F:N:PG:p:r:Qq:1:2:3:Sw:x:y:z:";
235
236 /* quantities for 1-piece polytrope: */
237 int polytropeFlag = 0;
238 double Gamma = 0, reference_pressure_si = 0, reference_density_si = 0;
239
240 /* quantities for 4-parameter piecewise polytrope: */
241 int piecewisePolytropeFlag = 0;
242 double logp1_si = 0, gamma1 = 0, gamma2 = 0, gamma3 = 0;
243
244 /* quatities for 4-coeff. spectral decompositions */
245 int spectralFlag = 0;
246 double SDgamma0 = 0, SDgamma1 = 0, SDgamma2 = 0, SDgamma3 = 0;
247
248 while (1) {
249 int option_index = 0;
250 int c;
251
252 c = LALgetopt_long_only(argc, argv, args, long_options, &option_index);
253 if (c == -1) /* end of options */
254 break;
255
256 switch (c) {
257 case 0: /* if option set a flag, nothing else to do */
258 if (long_options[option_index].flag)
259 break;
260 else {
261 fprintf(stderr, "error parsing option %s with argument %s\n",
262 long_options[option_index].name, LALoptarg);
263 exit(1);
264 }
265 case 'h': /* help */
266 usage(argv[0]);
267 exit(0);
268 case 'U':
269 if (global_fmt == default_fmt)
271 global_virial = 1;
272 break;
273 case 'E':
274 global_epsrel = atof(LALoptarg);
275 if (global_epsrel < 0 || global_epsrel == 0 || global_epsrel > 1) {
276 fprintf(stderr, "invalid value of TOV solver routine relative error\n");
277 exit(1);
278 }
279 break;
280 case 'f': /* eos-file */
282 break;
283 case 'n': /* eos-name */
285 break;
286 case 'd':
287 global_rho_c = atof(LALoptarg);
288 if (global_rho_c < 0) {
289 fprintf(stderr, "invalid value of central rest-mass density\n");
290 exit(1);
291 }
292 break;
293 case 'e':
294 global_e_c = atof(LALoptarg);
295 if (global_e_c < 0) {
296 fprintf(stderr, "invalid value of central energy density\n");
297 exit(1);
298 }
299 break;
300 case 'F': /* format */
302 break;
303 case 'N': /* npts */
304 global_npts = strtol(LALoptarg, NULL, 0);
305 if (global_npts < 1) {
306 fprintf(stderr, "invalid number of points\n");
307 exit(1);
308 }
309 break;
310
311 /* using a 1-piece polytrope */
312 case 'P':
313 polytropeFlag = 1;
314 break;
315 case 'G':
316 Gamma = atof(LALoptarg);
317 break;
318 case 'p':
319 reference_pressure_si = atof(LALoptarg);
320 break;
321 case 'r':
322 reference_density_si = atof(LALoptarg);
323 break;
324
325 /* using a 4-piece polytrope */
326 case 'Q':
327 piecewisePolytropeFlag = 1;
328 break;
329 case 'q':
330 logp1_si = atof(LALoptarg);
331 break;
332 case '1':
333 gamma1 = atof(LALoptarg);
334 break;
335 case '2':
336 gamma2 = atof(LALoptarg);
337 break;
338 case '3':
339 gamma3 = atof(LALoptarg);
340 break;
341
342 /* using 4-coeff. spectral decomposition */
343 case 'S':
344 spectralFlag = 1;
345 break;
346 case 'w':
347 SDgamma0 = atof(LALoptarg);
348 break;
349 case 'x':
350 SDgamma1 = atof(LALoptarg);
351 break;
352 case 'y':
353 SDgamma2 = atof(LALoptarg);
354 break;
355 case 'z':
356 SDgamma3 = atof(LALoptarg);
357 break;
358
359 default:
360 fprintf(stderr, "unknown error while parsing options\n");
361 exit(1);
362 }
363 }
364
365 /* set eos to 1-piece polytrope */
366 if (polytropeFlag == 1)
367 global_eos =
368 XLALSimNeutronStarEOSPolytrope(Gamma, reference_pressure_si,
369 reference_density_si);
370
371 /* set eos to 4-parameter piecewise polytrope */
372 if (piecewisePolytropeFlag == 1)
373 global_eos =
375 gamma1, gamma2, gamma3);
376
377 /* set eos to 4-coeff. spectral decomposition */
378 if (spectralFlag == 1)
379 global_eos =
381 SDgamma1, SDgamma2, SDgamma3);
382
383 if (LALoptind < argc) {
384 fprintf(stderr, "extraneous command line arguments:\n");
385 while (LALoptind < argc)
386 fprintf(stderr, "%s\n", argv[LALoptind++]);
387 exit(1);
388 }
389
390 if (!global_eos) {
391 fprintf(stderr, "error: no equation of state selected\n");
392 usage(argv[0]);
393 exit(1);
394 }
395
396 return 0;
397}
398
399int usage(const char *program)
400{
401 int e, c;
402 fprintf(stderr, "usage: %s [options]\n", program);
403 fprintf(stderr,
404 "\t-h, --help \tprint this message and exit\n");
405 fprintf(stderr,
406 "\t-f FILE, --eos-file=FILE \tuse EOS with from data filename FILE\n");
407 fprintf(stderr,
408 "\t-n NAME, --eos-name=NAME \tuse EOS with name NAME\n");
409 fprintf(stderr, "\n");
410 fprintf(stderr,
411 "\t-U, --calculate-virial-theorem \tcalculate and print Virial theorem besides M, R\n");
412 fprintf(stderr, "\n");
413 fprintf(stderr,
414 "\t-E, --choose-relative-error \tchoose a value for the TOV solver routine relative error (default epsrel=1e-6)\n");
415 fprintf(stderr, "\n");
416 fprintf(stderr,
417 "\tYou may choose a central value for the rest-mass density (in kg/m^3) or the energy density (in J/m^3) as follows\n");
418 fprintf(stderr,
419 "\t-d, --central-rest-mass-density \tcentral rest-mass density\n");
420 fprintf(stderr,
421 "\t-e, --central-energy-density \tcentral energy density\n");
422 fprintf(stderr,
423 "\tIf none are chosen the the program will use a standard fixed value\n");
424 fprintf(stderr, "\n");
425 fprintf(stderr,
426 "\t-P, --polytrope \tuse single polytrope\n");
427 fprintf(stderr, "\t-G GAMMA, --gamma=GAMMA \tadiabatic index\n");
428 fprintf(stderr,
429 "\t-p PRESSURE, --pressure=PRESSURE \tpressure at reference density\n");
430 fprintf(stderr,
431 "\t-r DENSITY, --density=DENSITY \treference density\n");
432 fprintf(stderr, "\n");
433 fprintf(stderr,
434 "\t-Q, --piecewisepolytrope \tuse 4-parameter piecewise polytrope (PRD 79, 124032 (2009))\n");
435 fprintf(stderr,
436 "\t-q log(p_1), --logp1=log(p_1) \tlog of pressure at rho_1=10^17.7 kg/m^3\n");
437 fprintf(stderr,
438 "\t-1 Gamma_1, --gamma1=Gamma_1 \tadiabatic index <10^17.7 kg/m^3\n");
439 fprintf(stderr,
440 "\t-2 Gamma_2, --gamma2=Gamma_2 \tadiabatic index 10^17.7--10^18 kg/m^3\n");
441 fprintf(stderr,
442 "\t-3 Gamma_3, --gamma3=Gamma_3 \tadiabatic index >10^18.0 kg/m^3\n");
443 fprintf(stderr, "\n");
444 fprintf(stderr,
445 "\t-S --4paramspectraldecomp \tuse 4-parameter spectral decomposition (PRD 82, 103011 (2010))\n");
446 fprintf(stderr,
447 "\t-w SDgamma0, --SDgamma0=SDgamma0 \tadiabatic index spectral decomposition coefficient 1 0.2--2.0\n");
448 fprintf(stderr,
449 "\t-x SDgamma1, --SDgamma1=SDgamma1 \tadiabatic index spectral decomposition coefficient 2 -1.6--1.7\n");
450 fprintf(stderr,
451 "\t-y SDgamma2, --SDgamma2=SDgamma2 \tadiabatic index spectral decomposition coefficient 3 -0.8--0.6\n");
452 fprintf(stderr,
453 "\t-z SDgamma3, --SDgamma3=SDgamma3 \tadiabatic index spectral decomposition coefficient 4 -0.2--0.2\n");
454 fprintf(stderr, "\n");
455 fprintf(stderr,
456 "\t-N NPTS, --npts=NPTS \toutput NPTS points [%ld]\n",
458 fprintf(stderr,
459 "\t-F FORMAT, --format=FORMAT \toutput format FORMAT [\"%s\"]\n",
461 fprintf(stderr, "format string conversions:\n");
462 fprintf(stderr,
463 "\t%%c\t is replaced by the dimensionless compactness M/R\n");
464 fprintf(stderr,
465 "\t%%l\t is replaced by the dimensionless tidal parameter\n");
466 fprintf(stderr, "\t%%k\t is replaced by the tidal love number k2\n");
467 fprintf(stderr, "\t%%m\t is replaced by the mass in solar masses\n");
468 fprintf(stderr, "\t%%r\t is replaced by the radius in kilometers\n");
469 fprintf(stderr, "recognized tabulated equations of state:");
470 for (e = 0, c = 0; e < (int)(sizeof(lalSimNeutronStarEOSNames)/sizeof(*lalSimNeutronStarEOSNames)); ++e) {
471 c += fprintf(stderr, "%s%s", c ? ", " : "\n\t", lalSimNeutronStarEOSNames[e]);
472 if (c > 50)
473 c = 0;
474 }
475 fprintf(stderr, "\n");
476 return 0;
477}
void LALCheckMemoryLeaks(void)
#define pc
#define c
const char * name
struct tagLALSimNeutronStarEOS LALSimNeutronStarEOS
Incomplete type for the neutron star Equation of State (EOS).
int LALgetopt_long_only(int argc, char *const *argv, const char *options, const struct LALoption *long_options, int *opt_index)
int LALoptind
char * LALoptarg
#define no_argument
#define required_argument
#define fprintf
double i
Definition: bh_ringdown.c:118
double e
Definition: bh_ringdown.c:117
const double w
#define LAL_MSUN_SI
#define LAL_MRSUN_SI
LALSimNeutronStarEOS * XLALSimNeutronStarEOSPolytrope(double Gamma, double reference_pressure_si, double reference_density_si)
Creates a polytrope Equation of State defined by p = K rho^Gamma.
double XLALSimNeutronStarEOSPressureOfRestMassDensity(double rho, LALSimNeutronStarEOS *eos)
Returns the pressure in Pa at a given rest-mass density in kg/m^3.
LALSimNeutronStarEOS * XLALSimNeutronStarEOSByName(const char *name)
Creates an equation of state structure from tabulated equation of state data of a known name.
const char *const lalSimNeutronStarEOSNames[111]
Recognised names of equations of state.
LALSimNeutronStarEOS * XLALSimNeutronStarEOS4ParameterSpectralDecomposition(double SDgamma0, double SDgamma1, double SDgamma2, double SDgamma3)
Reads 4 spectral decomposition eos parameters to make an eos.
double XLALSimNeutronStarEOSPressureOfEnergyDensity(double e, LALSimNeutronStarEOS *eos)
Returns the pressure in Pa at a given energy density in J/m^3.
LALSimNeutronStarEOS * XLALSimNeutronStarEOSFromFile(const char *fname)
Reads a data file containing a tabulated equation of state.
double XLALSimNeutronStarEOSMaxPressure(LALSimNeutronStarEOS *eos)
Returns the maximum pressure of the EOS in Pa.
LALSimNeutronStarEOS * XLALSimNeutronStarEOS4ParameterPiecewisePolytrope(double logp1_si, double gamma1, double gamma2, double gamma3)
Creates a 4-parameter piecewise-polytrope Equation of State.
void XLALDestroySimNeutronStarEOS(LALSimNeutronStarEOS *eos)
Frees the memory associated with a pointer to an EOS structure.
int XLALSimNeutronStarTOVODEIntegrate(double *radius, double *mass, double *love_number_k2, double central_pressure_si, LALSimNeutronStarEOS *eos)
int XLALSimNeutronStarVirialODEIntegrateWithTolerance(double *radius, double *mass, double *int1, double *int2, double *int3, double *int4, double *int5, double *int6, double *love_number_k2, double central_pressure_si, LALSimNeutronStarEOS *eos, double epsrel)
Integrates the Tolman-Oppenheimer-Volkov stellar structure equations and the Virial Equations.
int XLALSimNeutronStarTOVODEIntegrateWithTolerance(double *radius, double *mass, double *love_number_k2, double central_pressure_si, LALSimNeutronStarEOS *eos, double epsrel)
Integrates the Tolman-Oppenheimer-Volkov stellar structure equations.
int XLALSimNeutronStarVirialODEIntegrate(double *radius, double *mass, double *int1, double *int2, double *int3, double *int4, double *int5, double *int6, double *love_number_k2, double central_pressure_si, LALSimNeutronStarEOS *eos)
static const INT4 r
static const INT4 m
void XLALAbortErrorHandler(const char *func, const char *file, int line, int errnum)
XLALErrorHandlerType * XLALSetErrorHandler(XLALErrorHandlerType *newHandler)
char * program
Definition: inject.c:87
int main(int argc, char *argv[])
int usage(const char *program)
const char *const default_fmt_U
long global_npts
double global_rho_c
int global_virial
const char *const default_fmt
int output(const char *fmt, double c, double m, double r, double w, double z, double k2)
int parseargs(int argc, char *argv[])
double global_epsrel
const long default_npts
const char * global_fmt
LALSimNeutronStarEOS * global_eos
double global_e_c
int * flag