polyco.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 1999 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/tempo/polyco.h
10 
11 #ifndef __POLY_H
12 #define __POLY_H
13 
14 #include "Pulsar/Predictor.h"
15 #include "Pulsar/Generator.h"
16 
17 #include <string>
18 #include <vector>
19 
20 #ifdef HAVE_MPI
21 #include <mpi.h>
22 #endif
23 
25 class polynomial {
26 
27  friend class polyco;
28 
29 protected:
30 
32  std::string psrname;
33 
35  std::string date;
36 
38  std::string utc;
39 
41  Pulsar::Phase ref_phase;
42 
44  double ref_freq;
45 
47  MJD ref_time;
48 
50  char telescope;
51 
53  double freq;
54 
56  bool binary;
57 
59  double binph;
60 
62  double binfreq;
63 
65  float dm;
66 
68  double nspan_mins;
69 
71  bool tempov11;
72 
74  double doppler_shift;
75 
77  double log_rms_resid;
78 
80  std::vector<double> coefs;
81 
83  void init();
84 
86  mutable bool keep;
87 
88 public:
89 
91  class Expert;
92  friend class Expert;
93 
95  static double precision;
96 
98  polynomial ();
99 
101  polynomial (const polynomial& poly);
102 
104  ~polynomial ();
105 
106  const polynomial& operator = (const polynomial& poly);
107 
108  int load (std::string* instr);
109  int unload (std::string *outstr) const;
110  int unload (FILE* fptr) const;
111 
112  void prettyprint () const;
113 
115  double period (const MJD &t) const;
116 
118  Pulsar::Phase phase (const MJD &t) const;
119 
121  MJD iphase (const Pulsar::Phase& p, const MJD* = 0) const;
122 
124  long double frequency (const MJD &t) const;
125 
127  Pulsar::Phase dispersion (const MJD &t, long double MHz) const;
128 
130  double chirp (const MJD &t) const;
131 
133  double accel (const MJD &t) const { return chirp(t)/frequency(t)*2.9979e8; }
134 
135  bool is_tempov11 () const { return tempov11; }
136  char get_telescope () const { return telescope; }
137  double get_freq () const { return freq; }
138  MJD get_reftime () const { return ref_time; }
139  Pulsar::Phase get_refphase () const { return ref_phase; }
140  double get_reffrequency () const { return ref_freq; }
141  double get_nspan () const { return nspan_mins; }
142  float get_dm () const { return dm; }
143  int get_ncoeff () const { return (int) coefs.size(); }
144  double get_doppler_shift () const { return doppler_shift / 1e4; }
145  std::string get_psrname () const { return psrname; }
146  bool get_binary () const { return binary; }
147  double get_binph () const { return binph; }
148  double get_log_rms_resid () const { return log_rms_resid; }
149 
150  static double flexibility;
151 
152  MJD start_time( double f = flexibility ) const
153  { return ref_time - nspan_mins * (1.0+f) * 60.0/2.0; };
154  MJD end_time( double f = flexibility ) const
155  { return ref_time + nspan_mins * (1.0+f) * 60.0/2.0; };
156 
157  Pulsar::Phase start_phase( double f = flexibility ) const
158  { return phase (start_time(f)); };
159  Pulsar::Phase end_phase( double f = flexibility ) const
160  { return phase (end_time(f)); };
161 
162  friend bool operator == (const polynomial &, const polynomial &);
163  friend bool operator != (const polynomial &, const polynomial &);
164 
165  // MPI functions
166 #ifdef HAVE_MPI
167  friend int mpiPack_size (const polynomial&, MPI_Comm comm, int* size);
168  friend int mpiPack (const polynomial&, void* outbuf, int outcount,
169  int* position, MPI_Comm comm);
170  friend int mpiUnpack (void* inbuf, int insize, int* position,
171  polynomial*, MPI_Comm comm);
172 #endif
173 
174 };
175 
178 
179 public:
180  Expert (polynomial* p) { instance = p; }
181 
182  void set_tempov11 (bool v) { instance->tempov11 = v; }
183  void set_telescope (char v) { instance->telescope = v; }
184  void set_freq (double v) { instance->freq = v; }
185  void set_reftime (const MJD& v) { instance->ref_time = v; }
186  void set_refphase (const Pulsar::Phase& v) { instance->ref_phase = v; }
187  void set_reffrequency (double v) { instance->ref_freq = v; }
188  void set_nspan (double v) { instance->nspan_mins = v; }
189  void set_dm (float v) { instance->dm = v; }
190  void set_ncoef (unsigned v) { instance->coefs.resize(v); }
191  void set_doppler_shift (double v) { instance->doppler_shift = v * 1e4; }
192  void set_psrname (const std::string& v) { instance->psrname = v; }
193  void set_binary (bool v) { instance->binary = v; }
194  void set_binph (double v) { instance->binph = v; }
195 
196  double* get_coefs () { return &(instance->coefs[0]); }
197 
198  polynomial* instance;
199 };
200 
202 class polyco : public Pulsar::Predictor {
203 
204 public:
205  static bool debug;
206 
208  std::vector<polynomial> pollys;
209 
211  polyco ();
212 
214  polyco (const polyco&);
215 
217  const polyco& operator = (const polyco&);
218 
220  ~polyco ();
221 
222  //
223  // Pulsar::Predictor implementation
224  //
225 
227  Predictor* clone () const;
228 
230  Pulsar::Generator* generator () const;
231 
233  void insert (const Predictor*);
234 
236  void keep (const std::vector<MJD>& epochs);
237 
239  bool matches (const Predictor*) const;
240 
242  void set_observing_frequency (long double MHz);
243 
245  long double get_observing_frequency () const;
246 
248  Pulsar::Phase phase (const MJD& t) const
249  { return best(t).phase(t); }
250 
252  Pulsar::Phase phase (const MJD& t, long double MHz) const
253  { const polynomial& b = best(t); return b.phase(t) + b.dispersion(t,MHz); }
254 
256  MJD iphase (const Pulsar::Phase& phase, const MJD* guess = 0) const
257  { return best(phase).iphase(phase, guess); }
258 
260  long double frequency (const MJD& t) const
261  { return best(t).frequency(t); }
262 
264  Pulsar::Phase dispersion (const MJD &t, long double MHz) const
265  { return best(t).dispersion(t,MHz); }
266 
268  void load (FILE*);
269 
271  void unload (FILE*) const;
272 
273  //
274  // the rest
275  //
276 
278  polyco (const std::string& id);
279 
281  int load (const std::string& filename);
282  int load (std::string* instr);
283 
284  // these functions return -1 upon error
285  int unload (const std::string& filename) const;
286 
287  // these functions return the number of bytes unloaded (-1 on error)
288  int unload (std::string *outstr) const;
289 
290  void append (const polyco& poly);
291 
292  void prettyprint () const;
293 
294  const polynomial* nearest (const MJD &t) const;
295 
296  const polynomial& best (const MJD &t) const;
297  const polynomial& best (const Pulsar::Phase &p) const;
298 
299  int i_nearest (const MJD &t, bool throw_exception = false) const;
300  int i_nearest (const Pulsar::Phase &p, bool throw_exception = false) const;
301 
302  double doppler_shift (const MJD& t) const
303  { return best(t).get_doppler_shift(); };
304 
305  double period(const MJD& t) const
306  { return best(t).period(t); };
307 
308  double chirp(const MJD& t) const
309  { return best(t).chirp(t); };
310 
311  double accel(const MJD& t) const
312  { return best(t).accel(t); };
313 
314  char get_telescope () const;
315  double get_freq () const;
316  MJD get_reftime () const;
317  double get_refperiod () const;
318  double get_nspan () const;
319  float get_dm () const;
320  int get_ncoeff () const;
321  std::string get_psrname () const;
322 
323  bool is_tempov11() const;
324 
325  MJD start_time () const { return pollys.front().start_time(); };
326  MJD end_time () const { return pollys.back().end_time(); };
327 
328  Pulsar::Phase start_phase () const { return pollys.front().start_phase(); };
329  Pulsar::Phase end_phase () const { return pollys.back().end_phase(); };
330 
331  friend bool operator == (const polyco &, const polyco &);
332  friend bool operator != (const polyco &, const polyco &);
333 
334 #ifdef HAVE_MPI
335  friend int mpiPack_size (const polyco&, MPI_Comm comm, int* size);
336  friend int mpiPack (const polyco&, void* outbuf, int outcount,
337  int* position, MPI_Comm comm);
338  friend int mpiUnpack (void* inbuf, int insize, int* position,
339  polyco*, MPI_Comm comm);
340 #endif
341 
342 private:
343 
344  // these attributes enable optimized i_nearest
345  mutable int last_index;
346 
347  mutable MJD last_epoch;
348  mutable double last_span_epoch;
349 
350  mutable Pulsar::Phase last_phase;
351  mutable double last_span_phase;
352 
353  void init();
354  void set_last (int i) const;
355 };
356 
357 inline std::ostream& operator<< (std::ostream& ostr, const polyco& p)
358 {
359  std::string out; p.unload(&out); return ostr << out;
360 }
361 
362 #endif
363 
@ Pulsar
A pulsar.
Definition: Types.h:63
double accel(const MJD &t) const
Returns the apparent acceleration toward observer (m s^-2)
Definition: polyco.h:138
std::vector< polynomial > pollys
The polynomial sets.
Definition: polyco.h:208
Pulsar::Phase ref_phase
The pulsar phase at reftime.
Definition: polyco.h:46
MJD iphase(const Pulsar::Phase &p, const MJD *=0) const
Returns the time at the given pulse phase.
Definition: polyco.C:411
void set_ncoef(unsigned ncoef)
Set the number of coefficients per polynomial ephemeris.
Definition: Predict.C:121
char telescope
TEMPO telescope id code.
Definition: polyco.h:55
void set_nspan(unsigned minutes)
Set the time spanned by each polynomial ephemeris.
Definition: Predict.C:104
~polyco()
Destructor.
Definition: polyco.C:568
MJD iphase(const Pulsar::Phase &phase, const MJD *guess=0) const
Return the epoch, given the phase.
Definition: polyco.h:256
void set_observing_frequency(long double MHz)
Set the observing frequency at which predictions will be made.
Definition: polyco.C:620
double freq
Observing frequency.
Definition: polyco.h:58
double binfreq
if binary, binary frequency
Definition: polyco.h:67
A convenient exception handling class.
Definition: Error.h:54
Pulsar::Generator * generator() const
Return a new Generator set up to produce a new Predictor like self.
Definition: polyco.C:588
bool binary
true if the pulsar is in a binary system
Definition: polyco.h:61
char code(const std::string &telescope_name)
Convert a telescope name to a single-character tempo code.
Definition: tempo++.C:167
long double frequency(const MJD &t) const
Return the spin frequency, given the epoch.
Definition: polyco.h:260
Pulsar::Phase phase(const MJD &t) const
Returns the pulse phase (in turns) at the given time.
Definition: polyco.C:391
bool tempov11
Flag that the polyco was created using TEMPO v.11 or greater.
Definition: polyco.h:76
Pulsar::Phase dispersion(const MJD &t, long double MHz) const
Return the phase correction for dispersion delay.
Definition: polyco.C:459
std::string date
The calendar date string.
Definition: polyco.h:40
void init()
intializes all values to null
Definition: polyco.C:47
std::vector< double > coefs
polynomial coefficients
Definition: polyco.h:85
polyco()
Default constructor.
Definition: polyco.C:530
double doppler_shift
Doppler shift due to Earth's motion along line of sight.
Definition: polyco.h:79
Uses tempo orediction mode to produce polynomial ephemerides (polyco)
Definition: Predict.h:26
double chirp(const MJD &t) const
Returns the spin frequency derivative (in Hz/s) at the given time.
Definition: polyco.C:436
Predictor * clone() const
Return a new, copy constructed instance of self.
Definition: polyco.C:583
Describes a single Tempo polyco set.
Definition: polyco.h:25
@ Phase
Pulsar Phase, for instance.
Definition: Types.h:39
std::string psrname
The pulsar name.
Definition: polyco.h:37
double period(const MJD &t) const
Returns the pulse period (in seconds) at the given time.
Definition: polyco.C:454
Generator of functions that predict pulse phase.
Definition: Generator.h:31
void load(FILE *)
Load from an open stream.
Definition: polyco.C:728
Pulsar::Phase dispersion(const MJD &t, long double MHz) const
Return the phase correction for dispersion delay.
Definition: polyco.h:264
double binph
if binary, binary phase
Definition: polyco.h:64
double nspan_mins
number of minutes spanned by polynomial
Definition: polyco.h:73
Pulsar::Phase phase(const MJD &t, long double MHz) const
Return the phase plus the dispersion delay.
Definition: polyco.h:252
void unload(FILE *) const
Unload to an open stream.
Definition: polyco.C:802
std::string utc
The UTC string.
Definition: polyco.h:43
bool matches(const Predictor *) const
Return true if the Predictor configuration matches this.
Definition: polyco.C:607
Functions that predict pulse phase.
Definition: Predictor.h:33
long double frequency(const MJD &t) const
Returns the spin frequency (in Hz) at the given time.
Definition: polyco.C:419
static bool verbose
Verbosity flag.
Definition: Predictor.h:52
void insert(const Predictor *)
Add the information from the supplied predictor to self.
Definition: polyco.C:597
static double precision
Determines the precision of iphase (stopping threshold) in seconds.
Definition: polyco.h:100
~polynomial()
destructor
Definition: polyco.C:100
Class for representing modified Julian dates with high precision.
Definition: MJD.h:27
long double get_observing_frequency() const
Get the observing frequency at which phase and epoch are related.
Definition: polyco.C:624
polynomial()
default constructor
Definition: polyco.C:62
Implements Predictor class for Tempo.
Definition: polyco.h:202
double ref_freq
Reference rotation frequency, F0 (Hz)
Definition: polyco.h:49
bool keep
used by polyco::keep to determine if this polynomial is used
Definition: polyco.h:91
Expert interface to polynomial attributes.
Definition: polyco.h:177
float dm
dispersion measure
Definition: polyco.h:70
double log_rms_resid
log of the r.m.s residual between polynomial and model
Definition: polyco.h:82
void keep(const std::vector< MJD > &epochs)
Keep only the components required to span the given epochs.
Definition: polyco.C:863
Pulsar::Phase phase(const MJD &t) const
Return the phase, given the epoch.
Definition: polyco.h:248
MJD ref_time
The epoch to which the polynomial is referenced.
Definition: polyco.h:52
const polyco & operator=(const polyco &)
Assignment operator.
Definition: polyco.C:547

Generated using doxygen 1.8.17