LALSimulation  5.4.0.1-c9a8ef6
sgwb.c
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2011 Jolien Creighton
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 /**
21  * @defgroup lalsim_sgwb lalsim-sgwb
22  * @ingroup lalsimulation_programs
23  *
24  * @brief Simulates a stochastic gravitational wave background
25  *
26  * ### Synopsis
27  *
28  * lalsim-sgwb [options]
29  *
30  * ### Description
31  *
32  * The `lalsim-sgwb` utility produces a continuous stream of simulated detector
33  * stochastic gravitational wave background noise for a specified interval of
34  * time, for the specified detectors, and for a specified flat spectral energy
35  * density. The output is written to the standard output in multi-column ascii
36  * format data in which the first column contains the GPS times of each sample
37  * and the remaining columns contain the (correlated) noise strain values
38  * in the various detectors.
39  *
40  * ### Options
41  *
42  * <DL>
43  * <DT>`-h`, `--help`</DT> <DD>print a help message and exit</DD>
44  * <DT>`-G`, `--geo`</DT> <DD>include GEO</DD>
45  * <DT>`-H`, `--hanford`</DT> <DD>include LHO</DD>
46  * <DT>`-L`, `--livingston`</DT> <DD>include LLO</DD>
47  * <DT>`-V`, `--virgo`</DT> <DD>include Virgo</DD>
48  * <DT>`-s`, `--start-time` GPSSTART</DT> <DD>GPS start time (s)</DD>
49  * <DT>`-t`, `--duration` DURATION</DT> <DD>(required) duration of data to produce (s)</DD>
50  * <DT>`-r`, `--sample-rate` SRATE</DT> <DD>sample rate (Hz) [16384]</DD>
51  * <DT>`-W`, `--Omega0` OMEGA0</DT> <DD>(required) flat spectral energy density</DD>
52  * <DT>`-f`, `--low-frequency` FLOW</DT> <DD>low frequency cutoff (Hz) (default = 10 Hz)</DD>
53  * <DT>`-a`, `--alpha`</DT> <DD>sgwb spectrum power law power</DD>
54  * <DT>`-F`, `--reference-frequency` FREF</DT> <DD>reference frequency (Hz) for a powerlaw spectrum</DD>
55  * <DT>`-p`, `--path-to-file`</DT> <DD>path to a file including data for Omega (overrides other options)</DD>
56  * </DL>
57  *
58  * ### Environment
59  *
60  * The `LAL_DEBUG_LEVEL` can used to control the error and warning reporting of
61  * `lalsim-sgwb`. Common values are: `LAL_DEBUG_LEVEL=0` which suppresses
62  * error messages, `LAL_DEBUG_LEVEL=1` which prints error messages alone,
63  * `LAL_DEBUG_LEVEL=3` which prints both error messages and warning messages,
64  * and `LAL_DEBUG_LEVEL=7` which additionally prints informational messages.
65  *
66  * The `GSL_RNG_SEED` and `GSL_RNG_TYPE` environment variables can be used
67  * to set the random number generator seed and type respectively.
68  *
69  * ### Exit Status
70  *
71  * The `lalsim-sgwb` utility exits 0 on success, and >0 if an error occurs.
72  *
73  * ### Example
74  *
75  * The command:
76  *
77  * lalsim-sgwb -H -L -V -W 1e-6 -s 1000000000 -t 1000
78  *
79  * will stream 1000 seconds of stochastic gravitational-wave background
80  * strain noise in the LHO, LLO, and Virgo detectors having
81  * \f$ {\Omega_0=10^{-6}} \f$ .
82  */
83 
84 #include <math.h>
85 #include <stdio.h>
86 #include <stdlib.h>
87 
88 #include <gsl/gsl_rng.h>
89 
90 #include <lal/LALStdlib.h>
91 #include <lal/LALgetopt.h>
92 #include <lal/LALConstants.h>
93 #include <lal/Date.h>
94 #include <lal/Units.h>
95 #include <lal/FrequencySeries.h>
96 #include <lal/TimeSeries.h>
97 #include <lal/LALSimSGWB.h>
98 
99 #include <lal/LALSimReadData.h>
100 
101 double srate = 16384.0; // sampling rate in Hertz
102 double tstart;
103 double duration;
104 double flow = 10.0;
105 double Omega0;
106 double alpha;
107 double fref;
108 int flag_power = 0;
110 const char *file_name;
111 
114 
115 int usage(const char *program);
116 int parseargs(int argc, char **argv);
117 
118 int main(int argc, char *argv[])
119 {
120  char tstr[32]; // string to hold GPS time -- 31 characters is enough
121  const double H0 = 0.72 * LAL_H0FAC_SI; // Hubble's constant in seconds
122  const size_t length = 65536; // number of points in a segment
123  const size_t stride = length / 2; // number of points in a stride
124  size_t i, n;
125  REAL8FrequencySeries *OmegaGW = NULL;
126  REAL8TimeSeries **seg = NULL;
128  gsl_rng *rng;
129 
131 
132  parseargs(argc, argv);
133 
135  gsl_rng_env_setup();
136  rng = gsl_rng_alloc(gsl_rng_default);
137 
138  if (flag_input_file == 0) {
139  if (flag_power == 0) {
140  OmegaGW = XLALSimSGWBOmegaGWFlatSpectrum(Omega0, flow, srate/length, length/2 + 1);
141  }
142  else {
143  OmegaGW = XLALSimSGWBOmegaGWPowerLawSpectrum(Omega0, alpha, fref, flow, srate/length, length/2 + 1);
144  }
145  }
146  else {
148  srate = OmegaGW->deltaF * length;
149  }
150 
151  n = duration * srate;
152  seg = LALCalloc(numDetectors, sizeof(*seg));
153 
154  printf("# parameters:\tsrate=%g\tstart=%g\tduration=%g\tOmega=%g\talpha=%g\tfref=%g\n", srate, tstart, duration, Omega0, alpha, fref);
155 
156  printf("# time (s)");
157  for (i = 0; i < numDetectors; ++i) {
158  char name[LALNameLength];
159  snprintf(name, sizeof(name), "%s:STRAIN", detectors[i].frDetector.prefix);
160  seg[i] = XLALCreateREAL8TimeSeries(name, &epoch, 0.0, 1.0/srate, &lalStrainUnit, length);
161  printf("\t%s (strain)", name);
162  }
163  printf("\n");
164 
165  XLALSimSGWB(seg, detectors, numDetectors, 0, OmegaGW, H0, rng); // first time to initilize
166 
167  while (1) { // infinite loop
168  size_t j;
169  for (j = 0; j < stride; ++j, --n) { // output first stride points
170  LIGOTimeGPS t = seg[0]->epoch;
171  if (n == 0) // check if we're done
172  goto end;
173  printf("%s", XLALGPSToStr(tstr, XLALGPSAdd(&t, j * seg[0]->deltaT)));
174  for (i = 0; i < numDetectors; ++i)
175  printf("\t%.18e", seg[i]->data->data[j]);
176  printf("\n");
177  }
178  XLALSimSGWB(seg, detectors, numDetectors, stride, OmegaGW, H0, rng); // make more data
179  }
180 
181 end:
182  for (i = 0; i < numDetectors; ++i)
184  XLALFree(seg);
187 
188  return 0;
189 }
190 
191 int parseargs( int argc, char **argv )
192 {
193  struct LALoption long_options[] = {
194  { "help", no_argument, 0, 'h' },
195  { "geo", no_argument, 0, 'G' },
196  { "hanford", no_argument, 0, 'H' },
197  { "livingston", no_argument, 0, 'L' },
198  { "virgo", no_argument, 0, 'V' },
199  { "start-time", required_argument, 0, 's' },
200  { "duration", required_argument, 0, 't' },
201  { "sample-rate", no_argument, 0, 'r' },
202  { "Omega0", no_argument, 0, 'W' },
203  { "alpha", no_argument, 0, 'a'},
204  { "reference-frequency", no_argument, 0, 'F'},
205  { "low-frequency", no_argument, 0, 'f' },
206  { "path-to-file", no_argument, 0 , 'p'},
207  { 0, 0, 0, 0 }
208  };
209  char args[] = "hGHLVs:t:r:W:a:F:f:p:";
210  while (1) {
211  int option_index = 0;
212  int c;
213 
214  c = LALgetopt_long_only(argc, argv, args, long_options, &option_index);
215  if (c == -1) /* end of options */
216  break;
217 
218  switch (c) {
219  case 0: /* if option set a flag, nothing else to do */
220  if (long_options[option_index].flag)
221  break;
222  else {
223  fprintf(stderr, "error parsing option %s with argument %s\n", long_options[option_index].name, LALoptarg);
224  exit(1);
225  }
226  case 'h': /* help */
227  usage(argv[0]);
228  exit(0);
229  case 'G': /* geo */
231  break;
232  case 'H': /* hanford */
234  break;
235  case 'L': /* livingston */
237  break;
238  case 'V': /* livingston */
240  break;
241  case 's': /* start-time */
242  tstart = atof(LALoptarg);
243  break;
244  case 't': /* duration */
245  duration = atof(LALoptarg);
246  break;
247  case 'r': /* sample-rate */
248  srate = atof(LALoptarg);
249  break;
250  case 'W': /* Omega0 */
251  Omega0 = atof(LALoptarg);
252  break;
253  case 'a': /* alpha */
254  alpha = atof(LALoptarg);
255  flag_power = 1;
256  break;
257  case 'F': /* reference-frequency */
258  fref = atof(LALoptarg);
259  flag_power = 1;
260  break;
261  case 'f': /* low-frequency */
262  flow = atof(LALoptarg);
263  break;
264  case 'p': /* path-to-file */
266  flag_input_file = 1;
267  break;
268  case '?':
269  default:
270  fprintf(stderr, "unknown error while parsing options\n");
271  exit(1);
272  }
273  }
274 
275  if ( LALoptind < argc ) {
276  fprintf(stderr, "extraneous command line arguments:\n");
277  while (LALoptind < argc)
278  fprintf(stderr, "%s\n", argv[LALoptind++]);
279  exit(1);
280  }
281 
282  if (flag_input_file == 1) {
283  fprintf(stderr, "Using a custom spectrum\n");
284  }
285  else if (flag_power == 1) {
286  fprintf(stderr, "Using a powerlaw spectrum\n");
287 
288  if (duration == 0.0 || Omega0 == 0.0) {
289  fprintf(stderr, "must select a duration and a value of Omega0\n");
290  usage(argv[0]);
291  exit(1);
292  }
293  }
294  else {
295  fprintf(stderr, "Using a flat spectrum\n");
296 
297  if (duration == 0.0 || Omega0 == 0.0) {
298  fprintf(stderr, "must select a duration and a value of Omega0\n");
299  usage(argv[0]);
300  exit(1);
301  }
302  }
303 
304  return 0;
305 }
306 
307 int usage( const char *program )
308 {
309  fprintf(stderr, "usage: %s [options]\n", program);
310  fprintf(stderr, "options:\n" );
311  fprintf(stderr, "\t-h, --help \tprint this message and exit\n");
312  fprintf(stderr, "\t-G, --geo \tinclude GEO\n");
313  fprintf(stderr, "\t-H, --hanford \tinclude LHO\n");
314  fprintf(stderr, "\t-L, --livingston \tinclude LLO\n");
315  fprintf(stderr, "\t-V, --virgo \tinclude Virgo\n");
316  fprintf(stderr, "\t-s, --start-time GPSSTART \tGPS start time (s)\n");
317  fprintf(stderr, "\t-t, --duration DURATION \t(required) duration of data to produce (s)\n");
318  fprintf(stderr, "\t-r, --sample-rate SRATE \tsample rate (Hz) [16384]\n");
319  fprintf(stderr, "\t-W, --Omega0 OMEGA0 \t(required) flat spectral energy density\n");
320  fprintf(stderr, "\t-f, --low-frequency FLOW \tlow frequency cutoff (Hz) (default = 10 Hz)\n");
321  fprintf(stderr, "\t-a, --alpha \tsgwb spectrum power law power\n");
322  fprintf(stderr, "\t-F, --reference-frequency FREF\treference frequency (Hz) for a powerlaw spectrum\n");
323  fprintf(stderr, "\t-p, --path-to-file \tpath to a file including data for Omega (overrides other options)\n");
324  return 0;
325 }
void LALCheckMemoryLeaks(void)
#define LALCalloc(m, n)
#define c
const char * name
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
sigmaKerr data[0]
const LALDetector lalCachedDetectors[LAL_NUM_DETECTORS]
char * XLALGPSToStr(char *s, const LIGOTimeGPS *t)
void XLALDestroyREAL8FrequencySeries(REAL8FrequencySeries *series)
#define LAL_H0FAC_SI
LALNameLength
LAL_NUM_DETECTORS
LAL_LLO_4K_DETECTOR
LAL_VIRGO_DETECTOR
LAL_GEO_600_DETECTOR
LAL_LHO_4K_DETECTOR
void XLALFree(void *p)
REAL8FrequencySeries * XLALSimSGWBOmegaGWPowerLawSpectrum(double Omegaref, double alpha, double fref, double flow, double deltaF, size_t length)
Creates a frequency series that contains a power law SGWB spectrum with the specified power Omegaref ...
Definition: LALSimSGWB.c:178
int XLALSimSGWB(REAL8TimeSeries **h, const LALDetector *detectors, size_t numDetectors, size_t stride, const REAL8FrequencySeries *OmegaGW, double H0, gsl_rng *rng)
Routine that may be used to generate sequential segments of stochastic background gravitational wave ...
Definition: LALSimSGWB.c:276
REAL8FrequencySeries * XLALSimSGWBOmegaGWFlatSpectrum(double Omega0, double flow, double deltaF, size_t length)
Creates a frequency series that contains a flat SGWB spectrum with the specified power Omega0 above s...
Definition: LALSimSGWB.c:147
REAL8FrequencySeries * XLALSimSGWBOmegaGWNumericalSpectrumFromFile(const char *fname, size_t length)
Definition: LALSimSGWB.c:558
REAL8TimeSeries * XLALCreateREAL8TimeSeries(const CHAR *name, const LIGOTimeGPS *epoch, REAL8 f0, REAL8 deltaT, const LALUnit *sampleUnits, size_t length)
void XLALDestroyREAL8TimeSeries(REAL8TimeSeries *series)
const LALUnit lalStrainUnit
void XLALAbortErrorHandler(const char *func, const char *file, int line, int errnum)
XLALErrorHandlerType * XLALSetErrorHandler(XLALErrorHandlerType *newHandler)
LIGOTimeGPS * XLALGPSAdd(LIGOTimeGPS *epoch, REAL8 dt)
LIGOTimeGPS * XLALGPSSetREAL8(LIGOTimeGPS *epoch, REAL8 t)
char * program
Definition: inject.c:87
end
LALDetector detectors[LAL_NUM_DETECTORS]
Definition: sgwb.c:112
int main(int argc, char *argv[])
Definition: sgwb.c:118
double alpha
Definition: sgwb.c:106
int usage(const char *program)
Definition: sgwb.c:307
int parseargs(int argc, char **argv)
Definition: sgwb.c:191
double tstart
Definition: sgwb.c:102
double fref
Definition: sgwb.c:107
double srate
Definition: sgwb.c:101
const char * file_name
Definition: sgwb.c:110
double Omega0
Definition: sgwb.c:105
int flag_power
Definition: sgwb.c:108
double duration
Definition: sgwb.c:103
size_t numDetectors
Definition: sgwb.c:113
int flag_input_file
Definition: sgwb.c:109
double flow
Definition: sgwb.c:104
int * flag
LIGOTimeGPS epoch
LIGOTimeGPS epoch
Definition: unicorn.c:20
double deltaT
Definition: unicorn.c:24