General Purpose Geodetic Library
SgBreakModel.cpp
Go to the documentation of this file.
1 /*
2  *
3  * This file is a part of Space Geodetic Library. The library is used by
4  * nuSolve, a part of CALC/SOLVE system, and designed to make analysis of
5  * geodetic VLBI observations.
6  * Copyright (C) 2013-2020 Sergei Bolotin.
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 3 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 this program. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 
24 #include <iostream>
25 #include <stdlib.h>
26 
27 #include <QtCore/QDataStream>
28 
29 
30 #include <SgBreakModel.h>
31 
32 #include <SgLogger.h>
33 #include <SgParameter.h>
34 
35 
36 
37 
38 /*=======================================================================================================
39 *
40 * METHODS:
41 *
42 *======================================================================================================*/
43 //
44 // static first:
46 {
47  return "SgParameterBreak";
48 };
49 
50 
51 
52 //
54  SgMJD(b),
55  SgAttribute(b),
56  epoch4Export_(b.epoch4Export_)
57 {
58  setA0(b.getA0());
59  setA1(b.getA1());
60  setA2(b.getA2());
61  setS0(b.getS0());
62  setS1(b.getS1());
63  setS2(b.getS2());
64  pA0_ = pA1_ = pA2_ = NULL;
65  if (b.pA0())
66  pA0_ = new SgParameter(*b.pA0());
67  if (b.pA1())
68  pA1_ = new SgParameter(*b.pA1());
69  if (b.pA2())
70  pA2_ = new SgParameter(*b.pA2());
71 };
72 
73 
74 
75 //
76 SgParameterBreak::SgParameterBreak(const SgMJD& t, double a0, double a1, double a2, bool isDynamic) :
77  SgMJD(t),
78  SgAttribute(),
79  epoch4Export_(t)
80 {
81  setA0(a0);
82  setA1(a1);
83  setA2(a2);
84  setS0(0.0);
85  setS1(0.0);
86  setS2(0.0);
87  if (isDynamic)
89  pA0_ = pA1_ = pA2_ = NULL;
90 };
91 
92 
93 
94 // A destructor:
96 {
97  if (pA0_)
98  {
99  delete pA0_;
100  pA0_ = NULL;
101  };
102  if (pA1_)
103  {
104  delete pA1_;
105  pA1_ = NULL;
106  };
107  if (pA2_)
108  {
109  delete pA2_;
110  pA2_ = NULL;
111  };
112 };
113 
114 
115 
116 //
118 {
119  *(SgMJD*)this = b;
120  *(SgAttribute*)this = b;
121  setA0(b.getA0());
122  setA1(b.getA1());
123  setA2(b.getA2());
124  setS0(b.getS0());
125  setS1(b.getS1());
126  setS2(b.getS2());
128  if (pA0_)
129  {
130  delete pA0_;
131  pA0_ = NULL;
132  };
133  if (pA1_)
134  {
135  delete pA1_;
136  pA1_ = NULL;
137  };
138  if (pA2_)
139  {
140  delete pA2_;
141  pA2_ = NULL;
142  };
143  if (b.pA0())
144  pA0_ = new SgParameter(*b.pA0());
145  if (b.pA1())
146  pA1_ = new SgParameter(*b.pA1());
147  if (b.pA2())
148  pA2_ = new SgParameter(*b.pA2());
149  return *this;
150 };
151 
152 
153 
154 //
155 void SgParameterBreak::createParameters(const QString& prefix)
156 {
157  pA0_ = new SgParameter(prefix + "_0");
158  pA1_ = new SgParameter(prefix + "_1");
159  pA2_ = new SgParameter(prefix + "_2");
160 };
161 
162 
163 
164 //
166 {
167  if (pA0_)
168  {
169  delete pA0_;
170  pA0_ = NULL;
171  };
172  if (pA1_)
173  {
174  delete pA1_;
175  pA1_ = NULL;
176  };
177  if (pA2_)
178  {
179  delete pA2_;
180  pA2_ = NULL;
181  };
182 };
183 
184 
185 
186 //
188  const SgMJD& t, double dT, double sign)
189 {
190  if (pA0_)
191  {
192  if (t <= *this)
193  pA0_->setD(0.0);
194  else
195  pA0_->setD(sign);
197  parameters.append(pA0_);
198  };
199  if (pA1_)
200  {
201  if (t <= *this)
202  pA1_->setD(0.0);
203  else
204  pA1_->setD(sign*dT);
206  parameters.append(pA1_);
207  };
208  if (pA2_)
209  {
210  if (t <= *this)
211  pA2_->setD(0.0);
212  else
213  pA2_->setD(sign*dT*dT);
215  parameters.append(pA2_);
216  };
217 };
218 
219 
220 
221 //
223  const SgMJD& t, double dT, double sign)
224 {
225  if (pA1_)
226  {
227  if (t <= *this)
228  pA1_->setD(0.0);
229  else
230  pA1_->setD(sign);
232  parameters.append(pA1_);
233  };
234  if (pA2_)
235  {
236  if (t <= *this)
237  pA2_->setD(0.0);
238  else
239  pA2_->setD(2.0*sign*dT);
241  parameters.append(pA2_);
242  };
243 };
244 
245 
246 
247 //
249 {
250  s << getAttributes() << a0_ << a1_ << a2_ << s0_ << s1_ << s2_;
251  if (s.status() != QDataStream::Ok)
252  {
254  ": saveIntermediateResults(): error writting data");
255  return false;
256  };
258  {
260  ": saveIntermediateResults(): error writting data for the epoch");
261  return false;
262  };
264  {
266  ": saveIntermediateResults(): error writting data for the export epoch");
267  return false;
268  };
269  return s.status() == QDataStream::Ok;
270 };
271 
272 
273 
274 //
276 {
277  double a0, a1, a2;
278  double s0, s1, s2;
279  unsigned int attributes;
280  s >> attributes >> a0 >> a1 >> a2 >> s0 >> s1 >> s2;
281  if (s.status() != QDataStream::Ok)
282  {
284  ": loadIntermediateResults(): error reading data: " +
285  (s.status()==QDataStream::ReadPastEnd?"read past end of the file":"read corrupt data"));
286  return false;
287  };
289  {
291  ": loadIntermediateResults(): error reading data for the epoch: " +
292  (s.status()==QDataStream::ReadPastEnd?"read past end of the file":"read corrupt data"));
293  return false;
294  };
296  {
298  ": loadIntermediateResults(): error writting data for the export epoch");
299  return false;
300  };
301  setAttributes(attributes);
302  a0_ = a0;
303  a1_ = a1;
304  a2_ = a2;
305  s0_ = s0;
306  s1_ = s1;
307  s2_ = s2;
308  return true;
309 };
310 /*=====================================================================================================*/
311 
312 
313 
314 
315 
317 /*=======================================================================================================
318 *
319 * METHODS:
320 *
321 *======================================================================================================*/
322 //
323 // static first:
324 const QString SgBreakModel::className()
325 {
326  return "SgBreakModel";
327 };
328 
329 
330 
331 //
333 {
334  // first, remove all existed breaks:
335  for (int i=0; i<size(); i++)
336  delete at(i);
337  clear();
338 
339  // then, make a copy from one list to *this:
340  for (int i=0; i<m.size(); i++)
341  append(new SgParameterBreak(*m.at(i)));
342 
343  // at last, adjust value of t0:
344  setT0(m.getT0());
345  return *this;
346 };
347 
348 
349 
350 //
351 double SgBreakModel::value(const SgMJD& t) const
352 {
353  double v=0.0;
355  it=constBegin();
356  for (int i=0; i<size(); i++)
357  v += it[i]->value(t, t-t0_);
358  return v;
359 };
360 
361 
362 
363 //
364 double SgBreakModel::rate(const SgMJD& t) const
365 {
366  double r=0.0;
368  it=constBegin();
369  for (int i=0; i<size(); i++)
370  r += it[i]->rate(t, t-t0_);
371  return r;
372 };
373 
374 
375 
376 //
377 bool SgBreakModel::addBreak(const SgMJD& t, double a0, double a1, double a2, bool isDynamic)
378 {
380  for (int i=0; i<size(); i++)
381  if (*it[i]==t)
382  return false;
383 
384  append(new SgParameterBreak(t, a0, a1, a2, isDynamic));
385  return true;
386 };
387 
388 
389 
390 //
392 {
394  for (int i=0; i<size(); i++)
395  if (*it[i]==*((SgMJD*)aBreak))
396  return false;
397 
398  append(aBreak);
399  return true;
400 };
401 
402 
403 
404 //
406 {
407  SgParameterBreak *aBreak=NULL;
408  int idx2Del=-1;
409  bool isOK=false;
411  it=begin();
412  for (int i=0; i<size(); i++)
413  if (fabs(*it[i]-t) < 0.8/DAY2SEC)
414  idx2Del = i;
415  if (idx2Del > -1)
416  {
417  aBreak = takeAt(idx2Del);
418  if (aBreak)
419  {
420  isOK = true;
421  delete aBreak;
422  };
423  }
424  else
425  return false;
426  return isOK;
427 };
428 
429 
430 
431 //
433 {
434  SgParameterBreak *aBreak=NULL;
435  bool isOK=false;
436  if (n>-1 && n<size())
437  {
438  aBreak = takeAt(n);
439  if (aBreak)
440  {
441  isOK = true;
442  delete aBreak;
443  };
444  }
445  else
446  return false;
447  return isOK;
448 };
449 
450 
451 
452 //
453 void SgBreakModel::createParameters(const QString& prefix)
454 {
455  for (int i=0; i<size(); i++)
456  at(i)->createParameters(prefix + QString("").sprintf("#%02d", i));
457 };
458 
459 
460 
461 //
463 {
464  for (int i=0; i<size(); i++)
465  at(i)->releaseParameters();
466 };
467 
468 
469 
470 //
472  const SgMJD& t, double tau, double sign)
473 {
474  for (int i=0; i<size(); i++)
475  at(i)->propagatePartials(parameters, t, tau, sign);
476 };
477 
478 
479 
480 //
482  const SgMJD& t, double tau, double sign)
483 {
484  for (int i=0; i<size(); i++)
485  at(i)->propagatePartials4rates(parameters, t, tau, sign);
486 };
487 
488 
489 
490 //
492 {
493  int n(size());
494  bool b(false);
495  s << n << b;
496  for (int i=0; i<n; i++)
497  {
498  at(i)->saveIntermediateResults(s);
499  if (s.status() != QDataStream::Ok)
500  {
502  ": loadIntermediateResults(): error writting data: idx#" + QString("").setNum(i));
503  return false;
504  };
505  };
506  return s.status()==QDataStream::Ok;
507 };
508 
509 
510 
511 //
513 {
514  int n;
515  bool b;
516  s >> n >> b;
517  //
518  if (s.status() != QDataStream::Ok)
519  {
521  ": loadIntermediateResults(): error reading data (num): " +
522  (s.status()==QDataStream::ReadPastEnd?"read past end of the file":"read corrupt data"));
523  return false;
524  };
525  clear();
526  for (int i=0; i<n; i++)
527  {
528  if (s.status() == QDataStream::Ok)
529  {
532  addBreak(b);
533  }
534  else
535  {
537  ": loadIntermediateResults(): error reading data (idx#" + QString("").setNum(i) + "): " +
538  (s.status()==QDataStream::ReadPastEnd?"read past end of the file":"read corrupt data"));
539  return false;
540  };
541  };
542  if (s.status() == QDataStream::Ok)
543  {
544  return true;
545  };
546  //
548  ": loadIntermediateResults(): error reading data (@end): " +
549  (s.status()==QDataStream::ReadPastEnd?"read past end of the file":"read corrupt data"));
550  return false;
551 };
552 
553 
554 
555 //
557 {
558  qSort(begin(), end(), mjdOrderLessThan);
559 };
560 /*=====================================================================================================*/
561 
562 
563 
564 
565 
566 
567 
568 
569 
570 
571 
572 
573 /*=====================================================================================================*/
574 //
575 // FRIENDS:
576 //
577 /*=====================================================================================================*/
578 //
579 
580 /*=====================================================================================================*/
581 //
582 // aux functions:
583 //
585 {
586  return *pb1 < *pb2;
587 };
588 
589 
590 
591 /*=====================================================================================================*/
592 //
593 // constants:
594 //
595 
596 /*=====================================================================================================*/
bool mjdOrderLessThan(const SgParameterBreak *, const SgParameterBreak *)
SgLogger * logger
Definition: SgLogger.cpp:231
#define DAY2SEC
radians to mas:
Definition: SgMathSupport.h:56
bool isAttr(uint a) const
Definition: SgAttribute.h:226
void setAttributes(unsigned int a)
Definition: SgAttribute.h:191
unsigned int getAttributes() const
Definition: SgAttribute.h:183
void addAttr(uint a)
Definition: SgAttribute.h:202
void setT0(const SgMJD &t)
Definition: SgBreakModel.h:540
SgBreakModel & operator=(const SgBreakModel &m)
double value(const SgMJD &t) const
bool delBreak(const SgMJD &t)
void createParameters(const QString &prefix)
double rate(const SgMJD &t) const
bool addBreak(const SgMJD &t, double a0=0.0, double a1=0.0, double a2=0.0, bool isDynamic=false)
bool loadIntermediateResults(QDataStream &)
const SgMJD & getT0() const
Definition: SgBreakModel.h:532
static const QString className()
void propagatePartials4rates(QList< SgParameter * > &parameters, const SgMJD &t, double tau, double sign)
void propagatePartials(QList< SgParameter * > &parameters, const SgMJD &t, double tau, double sign)
void releaseParameters()
bool saveIntermediateResults(QDataStream &) const
virtual void write(LogLevel, quint32, const QString &, bool=false)
Definition: SgLogger.cpp:88
@ IO_BIN
Definition: SgLogger.h:64
Definition: SgMJD.h:59
bool saveIntermediateResults(QDataStream &) const
Definition: SgMJD.cpp:1314
bool loadIntermediateResults(QDataStream &)
Definition: SgMJD.cpp:1329
double getA2() const
Definition: SgBreakModel.h:379
void propagatePartials(QList< SgParameter * > &parameters, const SgMJD &t, double tau, double sign)
SgParameter * pA1_
Definition: SgBreakModel.h:210
void setA2(double a)
Definition: SgBreakModel.h:435
SgParameter * pA2_
Definition: SgBreakModel.h:211
double getS1() const
Definition: SgBreakModel.h:395
double getA1() const
Definition: SgBreakModel.h:371
SgParameter * pA2()
Definition: SgBreakModel.h:164
void setS0(double a)
Definition: SgBreakModel.h:443
double getS2() const
Definition: SgBreakModel.h:403
double getS0() const
Definition: SgBreakModel.h:387
void setS1(double a)
Definition: SgBreakModel.h:451
SgParameter * pA0()
Definition: SgBreakModel.h:162
bool loadIntermediateResults(QDataStream &)
SgParameter * pA0_
Definition: SgBreakModel.h:209
const SgMJD & getEpoch4Export() const
Definition: SgBreakModel.h:411
double getA0() const
Definition: SgBreakModel.h:363
void propagatePartials4rates(QList< SgParameter * > &parameters, const SgMJD &t, double tau, double sign)
void createParameters(const QString &prefix)
static const QString className()
void setA0(double a)
Definition: SgBreakModel.h:419
SgParameterBreak & operator=(const SgParameterBreak &b)
@ Attr_DYNAMIC
parameters supposed to be estimated during the common solution;
Definition: SgBreakModel.h:60
void setS2(double a)
Definition: SgBreakModel.h:459
bool saveIntermediateResults(QDataStream &) const
void setA1(double a)
Definition: SgBreakModel.h:427
SgParameter * pA1()
Definition: SgBreakModel.h:163
void setEpoch4Export(const SgMJD &)
Definition: SgBreakModel.h:467
void setD(double d)
Definition: SgPartial.h:347
@ Attr_IS_IN_RUN
Definition: SgPartial.h:52