COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
fast_propagator.cpp File Reference
#include "support/configCosmos.h"
#include "physics/physicslib.h"
#include "math/mathlib.h"
#include "agent/agentclass.h"
#include "support/jsonlib.h"
#include "support/datalib.h"
#include "support/convertlib.h"
#include <sys/stat.h>
#include <iostream>
#include <iomanip>
Include dependency graph for fast_propagator.cpp:

Macros

#define POLLBUFSIZE   20000
 

Functions

void endline ()
 
int main (int argc, char *argv[])
 

Variables

gj_handle gjh
 
Agentagent
 
std::vector< eventstruceventdict
 
std::vector< eventstrucevents
 
std::string mainjstring
 

Macro Definition Documentation

#define POLLBUFSIZE   20000

Function Documentation

void endline ( )
47  {
48  std::cout << std::endl;
49 }
int main ( int  argc,
char *  argv[] 
)
52 {
53  string fname;
54  std::string node;
55  int32_t order = 6;
56  int32_t mode = 1; // attitude mode (0 - propagate?, 1-LVLH, ...)
57  double mjdnow;
58  double mjdstart = -1.;
59  double mjdend = -1.;
60  double dt = 1.;
61  double logperiod = 1.;
62  double logstride = 3600./86400.;
63  int32_t iretn;
64  vector <tlestruc> lines;
65  bool tleinit = false;
66  // bool master_timer = false;
67 
68  switch (argc)
69  {
70  case 5:
71  mjdend = atof(argv[4]);
72  case 4:
73  logperiod = atof(argv[3]);
74  case 3:
75  if (strchr(argv[2], ':') != nullptr)
76  {
77  fname = argv[2];
78  mjdstart = stof(fname.substr(fname.find(':')+1));
79  if (mjdstart == 0.)
80  {
81  mjdstart = currentmjd();
82  }
83  iretn = load_lines(fname.substr(0, fname.find(':')), lines);
84  tleinit = true;
85  }
86  else
87  {
88  mjdstart = atof(argv[2]);
89  }
90  case 2:
91  node = argv[1];
92  break;
93  default:
94  std::cout << "Usage: fast_propagator nodename [mjdstart|0 [logperiod [mjdend]]]" << std::endl;
95  exit (-1);
96  break;
97  }
98 
99  if (!(agent = new Agent(node)))
100  {
101  printf("Failed to setup client for node %s: %d\n", node.c_str(), AGENT_ERROR_JSON_CREATE);
103  }
104 
105  agent->cinfo->node.phys.mode = mode;
106 
107  load_dictionary(eventdict, agent->cinfo, (char *)"events.dict");
108 
109  // Set initial state
110  locstruc iloc;
111 
112  pos_clear(iloc);
113 
114  if (!tleinit)
115  {
116  struct stat fstat;
117  FILE* fdes;
118  fname = get_nodedir((node.c_str()));
119  fname += "/state.ini";
120  if ((iretn=stat(fname.c_str(), &fstat)) == 0 && (fdes=fopen(fname.c_str(),"r")) != NULL)
121  {
122  char* ibuf = (char *)calloc(1,fstat.st_size+1);
123  size_t nbytes = fread(ibuf, 1, fstat.st_size, fdes);
124  if (nbytes)
125  {
126  json_parse(ibuf, agent->cinfo);
127  }
128  free(ibuf);
129  std::cout << "Initial State Vector Vel: [" << iloc.pos.eci.v.col[0] << ", " << iloc.pos.eci.v.col[1] << ", " << iloc.pos.eci.v.col[2] << "] km" << std::endl;
130  std::cout << "Initial MJD: " << std::setprecision(10) << iloc.utc << std::endl;
131  }
132  else
133  {
134  printf("Unable to open state.ini\n");
135  exit (-1);
136  }
137  }
138  else
139  {
140  lines2eci(mjdstart, lines, agent->cinfo->node.loc.pos.eci);
141  ++agent->cinfo->node.loc.pos.eci.pass;
142  }
143 
145  iloc = agent->cinfo->node.loc;
146 
147 #define POLLBUFSIZE 20000
148  std::string pollbuf;
149  Agent::messstruc mess;
150 
151  iretn = agent->readring(mess, Agent::AgentMessage::ALL,1);
152  switch ((Agent::AgentMessage)iretn)
153  {
154  case Agent::AgentMessage::SOH:
155  case Agent::AgentMessage::BEAT:
156  {
157  std::string tbuf = json_convert_string(json_extract_namedmember(pollbuf, "agent_name"));
158  if (!tbuf.empty() && tbuf == "physics")
159  {
160  tbuf = json_convert_string(json_extract_namedmember(pollbuf, "node_utcoffset"));
161  if (!tbuf.empty())
162  {
163  agent->cinfo->node.utcoffset = atof(tbuf.c_str());
164  printf("slave utcoffset: %f\n", agent->cinfo->node.utcoffset);
165  }
166  }
167  else
168  {
169  if (mjdstart == -1.)
170  {
172  }
173  else if (mjdstart == 0.)
174  {
175  agent->cinfo->node.utcoffset = 0.;
176  }
177  else
178  {
179  agent->cinfo->node.utcoffset = mjdstart - currentmjd(0.);
180  }
181  //printf("master utcoffset: %f\n", agent->cinfo->node.utcoffset);
182  std::cout << "master utcoffset: " << std::setprecision(5) << agent->cinfo->node.utcoffset << std::endl;
183  // master_timer = true;
184  }
185  break;
186  }
187  default:
188  if (mjdstart == -1.)
189  {
191  }
192  else if (mjdstart == 0.)
193  {
194  agent->cinfo->node.utcoffset = 0.;
195  }
196  else
197  {
198  agent->cinfo->node.utcoffset = mjdstart - currentmjd(0.);
199  }
200  //printf("master utcoffset: %f\n", agent->cinfo->node.utcoffset);
201  std::cout << "master utcoffset: " << agent->cinfo->node.utcoffset << std::endl;
202  // master_timer = true;
203  break;
204  }
205 
206  mjdnow = currentmjd(agent->cinfo->node.utcoffset);
207 
208  if (mjdnow < iloc.utc)
209  {
210  hardware_init_eci(agent->cinfo, iloc);
211  gauss_jackson_init_eci(gjh, order ,mode, -dt, iloc.utc,iloc.pos.eci, iloc.att.icrf, agent->cinfo->node.phys, agent->cinfo->node.loc);
212 
213  //printf("Initialize backwards %f days\n", (agent->cinfo->node.loc.utc-mjdnow));
214  std::cout << "Initialize backwards " << agent->cinfo->node.loc.utc-mjdnow << "days" << std::endl;
215 
218  iloc.utc = agent->cinfo->node.loc.utc;
219  iloc.pos.eci = agent->cinfo->node.loc.pos.eci;
220  iloc.att.icrf = agent->cinfo->node.loc.att.icrf;
221  }
222 
223  double step = 8.64 * (mjdnow-iloc.utc);
224  if (step > 60.)
225  {
226  step = 60.;
227  }
228  if (step < .1)
229  {
230  step = .1;
231  }
232 
233  //printf("Initialize forwards %f days, steps of %f\n", (mjdnow-iloc.utc), step);
234  std::cout << "Initialize forwards " << (mjdnow-iloc.utc) << " days, steps of " << step << std::endl;
235 
236 // gj_kernel gjk = gauss_jackson_kernel(gjh, order,step);
237 // gj_instance3d gji = gauss_jackson_instance(&gjk, 3, acceleration);
238 // gauss_jackson_preset(&gji);
239 // gauss_jackson_extrapolate(&gji, mjdnow);
240 
241  hardware_init_eci(agent->cinfo, iloc);
242  gauss_jackson_init_eci(gjh, order, mode, step, iloc.utc ,iloc.pos.eci, iloc.att.icrf, agent->cinfo->node.phys, agent->cinfo->node.loc);
246  pos_clear(iloc);
247  iloc.pos.eci = agent->cinfo->node.loc.pos.eci;
248  iloc.att.icrf = agent->cinfo->node.loc.att.icrf;
249  iloc.utc = agent->cinfo->node.loc.pos.eci.utc;
250  hardware_init_eci(agent->cinfo, iloc);
251  gauss_jackson_init_eci(gjh, order, mode, dt, iloc.utc ,iloc.pos.eci, iloc.att.icrf, agent->cinfo->node.phys, agent->cinfo->node.loc);
253  mjdnow = currentmjd(agent->cinfo->node.utcoffset);
254 
255  std::vector <gj_handle> tgjh(agent->cinfo->target.size());
256  std::vector <cosmosstruc *> tcinfo(agent->cinfo->target.size());
257 
258  for (uint16_t i=0; i<agent->cinfo->target.size(); ++i)
259  {
260  tcinfo[i] = json_init();
262  gauss_jackson_init_eci(tgjh[i], order, 0, dt, agent->cinfo->target[i].loc.utc, agent->cinfo->target[i].loc.pos.eci, agent->cinfo->target[i].loc.att.icrf, tcinfo[i]->node.phys, tcinfo[i]->node.loc);
264  }
265 
266  std::string sohstring = json_list_of_soh(agent->cinfo);
267  agent->set_sohstring(sohstring.c_str());
268  std::vector<jsonentry*> logtable;
269  json_table_of_list(logtable, sohstring.c_str(), agent->cinfo);
270 
271  double logdate = floor(mjdnow/logstride)*logstride;
272 
273  while (mjdend < 0. || mjdend-mjdstart > 0)
274  {
275  mjdnow += logperiod/86400.;
276  vector <locstruc> locvec = gauss_jackson_propagate(gjh, agent->cinfo->node.phys, agent->cinfo->node.loc, mjdnow);
277  simulate_hardware(agent->cinfo, locvec);
278  agent->cinfo->node.loc = locvec[locvec.size()-1];
279  if (agent->cinfo->node.loc.utc > agent->cinfo->node.utc)
280  {
282  }
283 
284  double dtemp;
285  if ((dtemp=floor(mjdnow/logstride)*logstride) > logdate)
286  {
287  logdate = dtemp;
288  log_move(agent->cinfo->node.name, "soh");
289  }
290 
291  for (uint16_t i=0; i<agent->cinfo->target.size(); ++i)
292  {
293  gauss_jackson_propagate(tgjh[i], tcinfo[i]->node.phys, tcinfo[i]->node.loc, mjdnow);
294  simulate_hardware(tcinfo[i], tcinfo[i]->node.loc);
295  }
298  for (uint32_t k=0; k<events.size(); ++k)
299  {
300  memcpy(&agent->cinfo->event[0],&events[k],sizeof(eventstruc));
301  strcpy(agent->cinfo->event[0].condition, agent->cinfo->emap[events[k].handle.hash][events[k].handle.index].text);
303  }
304 
305  if (agent->cinfo->node.utc != 0. && sohstring.size())
306  {
308  }
309  // agent->post(Agent::AgentMessage::SOH,json_of_table(mainjstring, agent->sohtable, agent->cinfo));
310  }
311  agent->shutdown();
312 }
std::vector< eventstruc > eventdict
Definition: fast_propagator.cpp:43
string json_list_of_soh(cosmosstruc *cinfo)
Definition: jsonlib.cpp:9252
#define DATA_LOG_TYPE_EVENT
Definition: datalib.h:109
AgentMessage
Type of Agent Message. Types > 127 are binary.
Definition: agentclass.h:216
size_t calc_events(vector< eventstruc > &dictionary, cosmosstruc *cinfo, vector< eventstruc > &events)
Calculate current Events.
Definition: jsonlib.cpp:11356
std::vector< eventstruc > events
Definition: fast_propagator.cpp:44
ElapsedTime dt
Definition: agent_file3.cpp:183
double utc
UTC of Position.
Definition: convertdef.h:161
int i
Definition: rw_test.cpp:37
int32_t json_parse(string jstring, cosmosstruc *cinfo)
Parse JSON using Name Space.
Definition: jsonlib.cpp:4799
char ibuf[1000]
Definition: razor_imu.cpp:43
const char * json_of_table(string &jstring, vector< jsonentry * > table, cosmosstruc *cinfo)
Create JSON stream from entries.
Definition: jsonlib.cpp:8733
void simulate_hardware(cosmosstruc *cinfo, vector< locstruc > &locvec)
Simulate Hardware data - multiple.
Definition: physicslib.cpp:937
uint32_t pass
pass indicator: allows synchronization with other attitude and position values.
Definition: convertdef.h:170
int32_t update_target(cosmosstruc *cinfo)
Update Track list.
Definition: jsonlib.cpp:11262
int iretn
Definition: rw_test.cpp:37
void gauss_jackson_init_eci(gj_handle &gjh, uint32_t order, int32_t mode, double dt, double utc, cartpos ipos, qatt iatt, physicsstruc &physics, locstruc &loc)
Initialize Gauss-Jackson orbit using ECI state vector.
Definition: physicslib.cpp:2479
void log_move(string oldpath, string newpath, bool compress)
Move log file - path version.
Definition: datalib.cpp:200
static vector< jsonentry * > logtable
Definition: agent_exec-2-0.cpp:114
double utc
Master time for location, in Modified Julian Day.
Definition: convertdef.h:879
double utc
Overall Node time.
Definition: jsondef.h:3592
string json_extract_namedmember(string json, string token)
Extract JSON value matching name.
Definition: jsonlib.cpp:4417
vector< vector< jsonequation > > emap
JSON Equation Map matrix.
Definition: jsondef.h:4211
cosmosstruc * json_init()
Initialize JSON pointer map.
Definition: jsonlib.cpp:454
int32_t set_sohstring(string list)
Set Limited SOH string.
Definition: agentclass.cpp:641
int32_t json_table_of_list(vector< jsonentry * > &table, string tokens, cosmosstruc *cinfo)
Output a vector of JSON entries.
Definition: jsonlib.cpp:3086
nodestruc node
Structure for summary information in node.
Definition: jsondef.h:4220
static string sohstring
Definition: agent_cpu-2-0.cpp:72
int32_t pos_clear(locstruc *loc)
Initialize posstruc.
Definition: convertlib.cpp:77
attstruc att
attstruc for this time.
Definition: convertdef.h:883
double utcoffset
MJD Offset between system UT and simulated UT.
Definition: jsondef.h:3590
static char fname[100]
Definition: geomag.cpp:89
char name[40+1]
Node Name.
Definition: jsondef.h:3556
Definition: agentclass.h:139
qatt icrf
Definition: convertdef.h:830
locstruc loc
Location structure.
Definition: jsondef.h:3596
size_t load_dictionary(vector< eventstruc > &dict, cosmosstruc *cinfo, const char *file)
Load Event Dictionary.
Definition: jsonlib.cpp:11313
int32_t shutdown()
Shutdown agent gracefully.
Definition: agentclass.cpp:366
Full COSMOS Event structure.
Definition: jsondef.h:1093
vector< locstruc > gauss_jackson_propagate(gj_handle &gjh, physicsstruc &physics, locstruc &loc, double tomjd)
Definition: physicslib.cpp:2871
double logperiod
Definition: agent_physics.cpp:47
Storage for messages.
Definition: agentclass.h:272
const char * json_of_event(string &jstring, cosmosstruc *cinfo)
Create JSON for an event.
Definition: jsonlib.cpp:9854
gj_handle gjh
Definition: fast_propagator.cpp:41
int32_t loc_update(locstruc *loc)
Synchronize all frames in location structure.
Definition: convertlib.cpp:2294
int32_t load_lines(string fname, vector< tlestruc > &lines)
Load TLE from file. TODO!!! Rename Function to loadTle and create new class for dealing with TLEs...
Definition: convertlib.cpp:3612
void log_write(string node, string agent, double utc, string extra, string type, string record, string location)
Write log entry - full.
Definition: datalib.cpp:75
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
string get_nodedir(string node, bool create_flag)
Get Current Node Directory.
Definition: datalib.cpp:1572
posstruc pos
posstruc for this time.
Definition: convertdef.h:881
#define DATA_LOG_TYPE_SOH
Definition: datalib.h:108
void hardware_init_eci(cosmosstruc *cinfo, locstruc &loc)
Initialize Hardware.
Definition: physicslib.cpp:877
string json_convert_string(string object)
Convert JSON to string.
Definition: jsonlib.cpp:4557
double col[3]
Definition: vector.h:55
cosmosstruc * cinfo
Definition: agentclass.h:346
cartpos eci
Definition: convertdef.h:737
vector< eventstruc > event
Single entry vector for event information.
Definition: jsondef.h:4250
#define AGENT_ERROR_JSON_CREATE
Definition: cosmos-errno.h:105
int32_t mode
Definition: jsondef.h:3436
vector< targetstruc > target
Vector of all targets known to node.
Definition: jsondef.h:4253
static string node
Definition: agent_monitor.cpp:126
std::string mainjstring
Definition: fast_propagator.cpp:45
Definition: convertdef.h:876
int lines2eci(double utc, vector< tlestruc >lines, cartpos &eci)
Return position from TLE set.
Definition: convertlib.cpp:3155
physicsstruc phys
Definition: jsondef.h:3597
int32_t readring(messstruc &message, AgentMessage type=Agent::AgentMessage::ALL, float waitsec=1., Where where=Where::TAIL, string proc="", string node="")
Check Ring for message.
Definition: agentclass.cpp:2395
rvector v
Velocity.
Definition: convertdef.h:165
Agent * agent
Definition: fast_propagator.cpp:42

Variable Documentation

gj_handle gjh
Agent* agent
std::vector<eventstruc> eventdict
std::vector<eventstruc> events
std::string mainjstring