27#include <lal/GenerateInspiral.h>
28#include <lal/LALInference.h>
29#include <lal/FrequencySeries.h>
31#include <lal/StringInput.h>
32#include <lal/TimeSeries.h>
34#include <lal/LALInferencePrior.h>
35#include <lal/LALInferenceTemplate.h>
36#include <lal/LALInferenceProposal.h>
37#include <lal/LALInferenceLikelihood.h>
38#include <lal/LALInferenceReadData.h>
39#include <lal/LALInferenceInit.h>
40#include <lal/LALInferenceCalibrationErrors.h>
67 MPI_Bcast(&randomseed, 1, MPI_INT, 0, MPI_COMM_WORLD);
71 printf(
" initialize(): random seed: %u\n", randomseed);
77 for (
i = 0;
i < mpi_rank;
i++)
78 randomseed = gsl_rng_get(run_state->
GSLrandom);
80 gsl_rng_set(run_state->
GSLrandom, randomseed);
87 ----------------------------------------------\n\
88 --- MCMC Algorithm Parameters ----------------\n\
89 ----------------------------------------------\n\
90 (--nsteps n) Maximum number of steps to take (1e7)\n\
91 (--neff N) Number of independent samples to collect (nsteps)\n\
92 (--skip n) Number of steps between writing samples to file (2000)\n\
93 (--adapt-tau) Adaptation decay power, results in adapt length of 10^tau (5)\n\
94 (--no-adapt) Do not adapt run\n\
95 (--randomseed seed) Random seed of sampling distribution (random)\n\
97 ----------------------------------------------\n\
98 --- Parallel Tempering Algorithm Parameters --\n\
99 ----------------------------------------------\n\
100 (--adapt-temps) Adapt the spacing between temperatures for uniform swap acceptance\n\
101 (--temp-skip N) Number of steps between temperature swap proposals (100)\n\
102 (--tempKill N) Iteration number to stop temperature swapping (Niter)\n\
103 (--ntemps N) Number of temperature chains in ladder (as many as needed)\n\
104 (--temp-min T) Lowest temperature for parallel tempering (1.0)\n\
105 (--temp-max T) Highest temperature for parallel tempering (50.0)\n\
106 (--anneal) Anneal hot temperature linearly to T=1.0\n\
107 (--annealStart N) Iteration number to start annealing (5*10^5)\n\
108 (--annealLength N) Number of iterations to anneal all chains to T=1.0 (1*10^5)\n\
110 ----------------------------------------------\n\
111 --- Noise Model ------------------------------\n\
112 ----------------------------------------------\n\
113 (--psd-fit) Run with PSD fitting\n\
114 (--psdNblock) Number of noise parameters per IFO channel (8)\n\
115 (--psdFlatPrior) Use flat prior on psd parameters (Gaussian)\n\
116 (--glitch-fit) Run with glitch fitting\n\
117 (--glitchNmax) Maximum number of glitch basis functions per IFO (20)\n\
119 ----------------------------------------------\n\
120 --- Output -----------------------------------\n\
121 ----------------------------------------------\n\
122 (--data-dump) Output waveforms to file\n\
123 (--adapt-verbose) Output parameter jump sizes and acceptance rate stats to file\n\
124 (--temp-verbose) Output temperature swapping stats to file\n\
125 (--prop-verbose) Output proposal stats to file\n\
126 (--prop-track) Output proposal parameters\n\
127 (--outfile file) Write output files <file>.<chain_number> \n\
128 (PTMCMC.output.<random_seed>.<mpi_thread>)\n\
129 ----------------------------------------------\n\
130 --- Checkpointing-----------------------------\n\
131 ----------------------------------------------\n\
132 (--resume) Allow jobs to resume partially completed run\n\
133 (--checkpoint-exit-code N) Exit with code N when checkpoint is complete.\n\
134 For use with condor's +SuccessCheckpointExitCode option\n\
136 INT4 mpi_rank, mpi_size;
137 INT4 ntemps_per_thread;
140 INT4 count_vectors = 0;
155 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
156 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
165 fprintf(stderr,
"ERROR: MCMCMC sampling hasn't been brought up-to-date since restructuring.\n");
180 INT4 propVerbose = 0;
194 INT4 nsteps = 100000000;
204 printf(
"WARNING: --Neff has been deprecated in favor of --neff\n");
224 INT4 nsteps_until_swap = Tskip;
230 printf(
"WARNING: --ntemp has been deprecated in favor of --ntemps\n");
237 INT4 adapt_temps = 0;
246 tempMin = strtod(
ppt->
value, (
char **)NULL);
249 REAL8 tempMax = 50.0;
252 tempMax = strtod(
ppt->
value, (
char **)NULL);
255 INT4 de_buffer_limit = 1000000;
264 trigSNR = strtod(
ppt->
value, (
char **)NULL);
270 INT4 temp_verbose = 0;
275 INT4 adapt_verbose = 0;
332 thread->
id = mpi_rank*ntemps_per_thread +
i;
355 INT4 adaptLength = 0;
377 REAL8 trigSNR, networkSNRsqrd=0.0;
379 INT4 flexible_tempmax=0;
381 INT4 ntemps, ntemps_per_thread;
382 INT4 mpi_rank, mpi_size;
396 targetHotLike = ndim/2.;
401 fprintf(stdout,
"Using tempMax specified by commandline: %f.\n", tempMax);
402 }
else if (trigSNR > 0) {
403 networkSNRsqrd = trigSNR * trigSNR;
404 tempMax = networkSNRsqrd/(2*targetHotLike);
407 fprintf(stdout,
"Trigger SNR of %f specified, setting max temperature to %f.\n", trigSNR, tempMax);
411 ifo = runState->
data;
412 while (ifo != NULL) {
413 networkSNRsqrd += ifo->
SNR * ifo->
SNR;
417 if (networkSNRsqrd > 0.0) {
418 tempMax = networkSNRsqrd/(2*targetHotLike);
420 fprintf(stdout,
"Injecting SNR of %f, setting tempMax to %f.\n", sqrt(networkSNRsqrd), tempMax);
425 fprintf(stdout,
"No --trigger-snr or --temp-max specified, and ");
426 fprintf(stdout,
"not injecting a signal. Setting max temperature");
427 fprintf(stdout,
"to default of %f.\n", tempMax);
434 if (tempMin > tempMax) {
435 fprintf(stdout,
"WARNING: temp_min > temp_max. Forcing temp_min=1.0.\n");
446 flexible_tempmax = 1;
447 tempDelta = 1. + sqrt(2./(
REAL8)ndim);
451 while (
temp < tempMax) {
453 temp = tempMin * pow(tempDelta, t);
459 ntemps_per_thread = ntemps / mpi_size;
462 if (ntemps % mpi_size != 0.0) {
465 ntemps = mpi_size * ntemps_per_thread;
473 if (flexible_tempmax)
474 tempDelta = 1. + sqrt(2./(
REAL8)ndim);
476 tempDelta = pow(tempMax/tempMin, 1.0/(
REAL8)(ntemps-1));
479 for (t=0; t<ntemps; ++t)
480 ladder[t] = tempMin * pow(tempDelta, t);
485 ladder[ntemps-1] = INFINITY;
496 UINT4 i=0,
j=0, nCols=0, nPar=0, par=0, col=0;
500 const char *non_params[] = {
"cycle",
"logpost",
"logprior",
"logl",
"loglH1",
"loglL1",
"loglV1",
"",NULL};
502 FILE *infile = fopen(infileName,
"r");
504 fgets(
str, 999, infile);
506 word = strtok(
header,
" \t");
508 while (strcmp(word,
"cycle")) {
509 fgets(
str, 999, infile);
511 word = strtok(
header,
" \t");
516 word = strtok(
header,
" \t");
517 while (word != NULL) {
519 word = strtok(NULL,
" \t");
524 UINT4 is_param[nCols];
527 word = strtok(
header,
" \t");
528 for (
i=0;
i<nCols;
i++) {
532 while (non_params[
j] != NULL) {
533 if (!strcmp(non_params[
j],word)) {
540 word = strtok(NULL,
" \t");
543 char** in_params =
XLALMalloc((nPar)*
sizeof(
char *));
545 word = strtok(
str,
" \t");
548 for (
i=1;
i<nCols;
i++) {
556 printf(
"Reading the following params from %s:\n", infileName);
557 for (par=0; par<nPar; par++)
558 printf(
"\t%s\n",in_params[par]);
563 while (cycle <= burnin) {
564 fscanf(infile,
"%i", &cycle);
565 for (
j=1;
j<nCols;
j++)
566 fscanf(infile,
"%lg", &val);
571 while (ch !=
'\n') ch = getc(infile);
575 unsigned long startPostBurnin = ftell(infile);
578 while ( (ch = getc(infile)) != EOF) {
582 fseek(infile,startPostBurnin,SEEK_SET);
583 printf(
"%i samples read from %s.\n", nSamples, infileName);
589 for (
i = 0;
i < nSamples;
i++) {
592 nread =
fscanf(infile,
"%i", &cycle);
594 fprintf(stderr,
"Cannot read sample from file (in %s, line %d)\n",
600 for (col = 1; col < nCols; col++) {
601 nread =
fscanf(infile,
"%lg", &val);
603 fprintf(stderr,
"Cannot read sample from file (in %s, line %d)\n",
609 sampleArray[
i][par] = val;
617 *nInSamps = nSamples;
626 char *fileargv[99],
str[999], buffer[99];
629 infile = fopen(infileName,
"r");
631 fprintf(stderr,
"Cannot read %s/n", infileName);
635 n = sprintf(buffer,
"lalinference_mcmcmpi_from_file_%s", infileName);
636 fileargv[0] = (
char*)
XLALCalloc(
n+1,
sizeof(
char*));
637 fileargv[0] = buffer;
639 fgets(
str, 999, infile);
640 fgets(
str, 999, infile);
643 pch = strtok (
str,
" ");
644 while (pch != NULL) {
645 if (strcmp(pch,
"Command") != 0 && strcmp(pch,
"line:") != 0) {
647 fileargv[fileargc] = (
char*)
XLALCalloc(
n+1,
sizeof(
char*));
648 fileargv[fileargc] = pch;
652 fprintf(stderr,
"Too many arguments in file %s\n",infileName);
656 pch = strtok (NULL,
" ");
660 fileargv[fileargc-1][strlen(fileargv[fileargc-1])-1] =
'\0';
667int main(
int argc,
char *argv[]){
673 MPI_Init(&argc, &argv);
674 MPI_Comm_rank(MPI_COMM_WORLD, &mpirank);
676 if (mpirank == 0)
fprintf(stdout,
" ========== LALInference_MCMC ==========\n");
691 if (runState == NULL) {
693 fprintf(stderr,
"run_state not allocated (%s, line %d).\n",
727 if (runState == NULL)
731 if (mpirank == 0) printf(
"sampling...\n");
734 if (mpirank == 0) printf(
" ========== main(): finished. ==========\n");
ProcessParamsTable * LALInferenceParseCommandLine(int argc, char *argv[])
void LALInferenceApplyCalibrationErrors(LALInferenceIFOData *IFOdata, ProcessParamsTable *commandLine)
LALInferenceModel * LALInferenceInitCBCModel(LALInferenceRunState *state)
Initialise state variables needed for LALInferenceNest or LALInferenceMCMC to run on a CBC signal.
LALInferenceRunState * LALInferenceInitRunState(ProcessParamsTable *command_line)
void LALInferenceInitCBCThreads(LALInferenceRunState *run_state, INT4 nthreads)
void LALInferenceDrawThreads(LALInferenceRunState *run_state)
void initializeMCMC(LALInferenceRunState *runState)
int main(int argc, char *argv[])
ProcessParamsTable * LALInferenceContinueMCMC(char *infileName)
REAL8 ** parseMCMCoutput(char ***params, UINT4 *nInPar, UINT4 *nInSamps, char *infilename, UINT4 burnin)
void init_mpi_randomstate(LALInferenceRunState *run_state)
REAL8 * LALInferenceBuildHybridTempLadder(LALInferenceRunState *runState, INT4 ndim)
INT4 init_ptmcmc(LALInferenceRunState *runState)
void LALInferencePTswap(LALInferenceRunState *runState, FILE *swapfile)
void PTMCMCAlgorithm(struct tagLALInferenceRunState *runState)
Implements the parallel tempered MCMC algorithm.
void LALInferenceAddPTMCMCMetaInfo(LALInferenceRunState *runState)
Markov-Chain Monte Carlo sampler written for LALInference.
void LALInferenceInjectInspiralSignal(LALInferenceIFOData *IFOdata, ProcessParamsTable *commandLine)
INT4 LALInferenceGetVariableDimensionNonFixedChooseVectors(LALInferenceVariables *vars, INT4 count_vectors)
Get number of dimensions in vars which are not fixed to a certain value, with a flag for skipping cou...
REAL8 LALInferenceGetREAL8Variable(LALInferenceVariables *vars, const char *name)
void LALInferenceAddREAL8Variable(LALInferenceVariables *vars, const char *name, REAL8 value, LALInferenceParamVaryType vary)
INT4 LALInferenceGetINT4Variable(LALInferenceVariables *vars, const char *name)
ProcessParamsTable * LALInferenceGetProcParamVal(ProcessParamsTable *procparams, const char *name)
Returns the element of the process params table with "name".
void LALInferenceAddINT4Variable(LALInferenceVariables *vars, const char *name, INT4 value, LALInferenceParamVaryType vary)
int LALInferenceCheckVariable(LALInferenceVariables *vars, const char *name)
Checks for name being present in vars returns 1(==true) or 0.
void LALInferenceSetVariable(LALInferenceVariables *vars, const char *name, const void *value)
Set a variable named name in vars with a value.
@ LALINFERENCE_PARAM_OUTPUT
A parameter that never changes, functions should respect this.
void LALInferenceInitLikelihood(LALInferenceRunState *runState)
Initialisation function which reads runState->commaneLine and sets up the likelihood function accordi...
void LALInferenceInitCBCPrior(LALInferenceRunState *runState)
Initialize the prior based on command line arguments.
LALInferenceProposalCycle * LALInferenceSetupDefaultInspiralProposalCycle(LALInferenceVariables *propArgs)
A reasonable default proposal.
LALInferenceVariables * LALInferenceParseProposalArgs(LALInferenceRunState *runState)
Go through all logic for deciding proposals to use.
void LALInferenceRandomizeProposalCycle(LALInferenceProposalCycle *cycle, gsl_rng *rng)
Randomizes the order of the proposals in the proposal cycle.
REAL8 LALInferenceCyclicProposal(LALInferenceThreadState *thread, LALInferenceVariables *currentParams, LALInferenceVariables *proposedParams)
Proposes a jump from the next proposal in the proposal cycle.
void * XLALMalloc(size_t n)
void * XLALCalloc(size_t m, size_t n)
Structure to contain IFO data.
REAL8 SNR
The epoch of this observation (the time of the first sample)
struct tagLALInferenceIFOData * next
ROQ data.
Structure to constain a model and its parameters.
LALInferenceVariables * params
Structure containing inference run state This includes pointers to the function types required to run...
ProcessParamsTable * commandLine
LALInferenceVariables * proposalArgs
The data from the interferometers.
LALInferenceVariables * algorithmParams
Any special arguments for the prior function.
INT4 nthreads
Array of live points for Nested Sampling.
struct tagLALInferenceIFOData * data
Log sample, i.e.
LALInferenceThreadState * threads
LALInferenceAlgorithm algorithm
A function that returns a new set of variables for the model.
LALInferenceSwapRoutine parallelSwap
Number of threads stored in threads.
Structure containing chain-specific variables.
LALInferenceProposalFunction proposal
Step counter for this thread.
LALInferenceProposalCycle * cycle
The proposal cycle.
LALInferenceVariables * proposalArgs
REAL8 temperature
Array containing multiple proposal densities.
The LALInferenceVariables structure to contain a set of parameters Implemented as a linked list of LA...
CHAR value[LIGOMETA_VALUE_MAX]