ROOT logo
// $Id: SolarSystem.h 2492 2011-06-27 06:33:04Z matevz $

// Copyright (C) 1999-2008, Matevz Tadel. All rights reserved.
// This file is part of GLED, released under GNU General Public License version 2.
// For the licensing terms see $GLEDSYS/LICENSE or http://www.gnu.org/.

#ifndef Var1_SolarSystem_H
#define Var1_SolarSystem_H

#include <Glasses/ZNode.h>
#include <Glasses/ZVector.h>
#include <Glasses/ODECrawler.h>

#include <Stones/TimeMakerClient.h>
#include <Stones/ZColor.h>

#include <Gled/GCondition.h>

#include <TRandom.h>

class CosmicBall;

class SolarSystem : public ZNode,
		    public ODECrawlerMaster,
                    public TimeMakerClient
{
  MAC_RNR_FRIENDS(SolarSystem);

private:
  Bool_t             hWarnTimeOutOfRange; //!
  Double_t           hStepIntegratorDt;   //!

  void _init();

public:
  enum CrawlMode_e { CM_ChunkedStorage, CM_DirectStep };

protected:
  ZLink<ZVector>     mBalls;      // X{GS} L{a} RnrBits{0,0,0,0, 0,0,0,5}
  ZLink<ODECrawler>  mODECrawler; // X{GS} L{a}

  CrawlMode_e        mCrawlMode;  // X{G}    7 PhonyEnum(-const=>1)

  Double_t           mTime;       // X{G}    7 Value(-range=>[0,1e9,  1,100])
  Double_t           mTimeFac;    // X{GS}   7 Value(-range=>[0,1000, 1,10])

  Int_t              mBallHistorySize; // X{GE} 7 Value(-range=>[0,4096, 1])

  ZColor             mColor;      // X{PRGS} 7 ColorButt()
  Double_t           mStarMass;   // X{GS}   7 Value()
  Double_t           mBallKappa;  // X{GS}   7 Value()

  // Planetoid creation parameters

  TRandom            mPlanetRnd;
  Double_t           mPlanetMinR; // X{GS}   7 Value(-range=>[1,100, 1,100], -join=>1)
  Double_t           mPlanetMaxR; // X{GS}   7 Value(-range=>[1,100, 1,100])
  Double_t           mOrbitMinR;  // X{GS}   7 Value(-range=>[1,10000, 1,10], -join=>1)
  Double_t           mOrbitMaxR;  // X{GS}   7 Value(-range=>[1,10000, 1,10])
  Double_t           mMaxTheta;   // X{GS}   7 Value(-range=>[0,90,    1,100])
  Double_t           mMaxEcc;     // X{GS}   7 Value(-range=>[0,1,     1,1000])
  ZColor             mPlanetColor;// X{PRGS} 7 ColorButt()

  // Chunked-storage stuff.

  class Storage : public ODEStorageD
  {
    Int_t mId;
    Int_t mRefCnt;

  public:

    Storage(Int_t order, Int_t capacity=128) :
      ODEStorageD(order, capacity), mId(0), mRefCnt(0)
    {}

    Storage(const Storage& s) :
      ODEStorageD(s.Order(), 12*s.Size()/10), mId(s.mId + 1), mRefCnt(0)
    {}

    Int_t GetId() const { return mId; }

    // RefCnt -- always call under storage-lock!
    void IncRefCnt() { ++mRefCnt; }
    void DecRefCnt() { if(--mRefCnt <= 0) delete this; }
  };

  typedef map<Double_t, Storage*>           mTime2pStorage_t;
  typedef map<Double_t, Storage*>::iterator mTime2pStorage_i;

  mTime2pStorage_t   mStorageMap;     //!
  mTime2pStorage_i   mCurrentStorage; //!

  Double_t           mTimePerChunk;   // X{GS} 7 Value(-range=>[100,1e4, 1,100])
  Double_t           mKeepPast;       // X{GS} 7 Value(-range=>[100,1e6, 1])
  Double_t           mCalcFuture;     // X{GS} 7 Value(-range=>[100,1e6, 1])

  void             clear_storage();

  mTime2pStorage_i find_storage_from_time(Double_t t);
  Storage*         set_current_storage_from_time(Double_t t);
  Double_t         set_time(Double_t t, Bool_t from_timetick);

  // Direct-step stuff

  GMutex             mBallSwitchMutex;  //!
  list<CosmicBall*>  mBallsToAdd;       //!
  list<CosmicBall*>  mBallsToRemove;    //!

  // Common integrator thread stuff.

  GThread           *mIntegratorThread; //!
  GCondition         mStorageCond;      //!

  static void* tl_IntegratorThread(SolarSystem* ss);
  void         ChunkIntegratorThread();
  void         StepIntegratorThread();

  // Hack for orbit switching - in direct-step mode only.
  Bool_t             bDesiredRHack;   // X{GS}   7 Bool();
  Double_t           mDesiredRHackT0; // X{GS}   7 Value(-range=>[0,20, 1,100])

  void hack_desired_r(Double_t dt);

public:
  SolarSystem(const Text_t* n="SolarSystem", const Text_t* t=0);
  virtual ~SolarSystem();

  virtual void AdEnlightenment();
  // ?? Do i need this one?
  // virtual void AdUnfoldment();  // called by Queen after comet unpacking

  // ODE virtuals
  virtual Int_t  ODEOrder();
  virtual void   ODEStart(Double_t y[], Double_t& x1, Double_t& x2);
  virtual void   ODEDerivatives(Double_t x, const Double_t y[], Double_t d[]);

  void CalculateEnergy(Double_t x, const Double_t y[], Double_t& kinetic, Double_t& potential);

  // TimeMakerClient
  virtual void TimeTick(Double_t t, Double_t dt);

  // Propagator for ChunkedStorage mode.
  void StartChunkIntegratorThread(); // X{E} 7 MCWButt()
  void StartStepIntegratorThread();  // X{E} 7 MCWButt()
  void StopIntegratorThread();       // X{E} 7 MCWButt()

  // Visualization
  void SetTime(Double_t t);     // X{E} 7 MCWButt()

  void SetBallHistorySize(Int_t history_size);

  // Builders
  virtual CosmicBall* RandomPlanetoid(const TString& name);

  virtual void MakeStar();      // X{E} 7 MCWButt()
  virtual void MakePlanetoid(); // X{E} 7 MCWButt()

  virtual void AddPlanetoid(CosmicBall* cb);
  virtual void RemovePlanetoid(CosmicBall* cb);

  // Plotters
  void PlotEnergy(); //! X{Ed} 7 MCWButt()

#include "SolarSystem.h7"
  ClassDef(SolarSystem, 1);
}; // endclass SolarSystem

#endif
 SolarSystem.h:1
 SolarSystem.h:2
 SolarSystem.h:3
 SolarSystem.h:4
 SolarSystem.h:5
 SolarSystem.h:6
 SolarSystem.h:7
 SolarSystem.h:8
 SolarSystem.h:9
 SolarSystem.h:10
 SolarSystem.h:11
 SolarSystem.h:12
 SolarSystem.h:13
 SolarSystem.h:14
 SolarSystem.h:15
 SolarSystem.h:16
 SolarSystem.h:17
 SolarSystem.h:18
 SolarSystem.h:19
 SolarSystem.h:20
 SolarSystem.h:21
 SolarSystem.h:22
 SolarSystem.h:23
 SolarSystem.h:24
 SolarSystem.h:25
 SolarSystem.h:26
 SolarSystem.h:27
 SolarSystem.h:28
 SolarSystem.h:29
 SolarSystem.h:30
 SolarSystem.h:31
 SolarSystem.h:32
 SolarSystem.h:33
 SolarSystem.h:34
 SolarSystem.h:35
 SolarSystem.h:36
 SolarSystem.h:37
 SolarSystem.h:38
 SolarSystem.h:39
 SolarSystem.h:40
 SolarSystem.h:41
 SolarSystem.h:42
 SolarSystem.h:43
 SolarSystem.h:44
 SolarSystem.h:45
 SolarSystem.h:46
 SolarSystem.h:47
 SolarSystem.h:48
 SolarSystem.h:49
 SolarSystem.h:50
 SolarSystem.h:51
 SolarSystem.h:52
 SolarSystem.h:53
 SolarSystem.h:54
 SolarSystem.h:55
 SolarSystem.h:56
 SolarSystem.h:57
 SolarSystem.h:58
 SolarSystem.h:59
 SolarSystem.h:60
 SolarSystem.h:61
 SolarSystem.h:62
 SolarSystem.h:63
 SolarSystem.h:64
 SolarSystem.h:65
 SolarSystem.h:66
 SolarSystem.h:67
 SolarSystem.h:68
 SolarSystem.h:69
 SolarSystem.h:70
 SolarSystem.h:71
 SolarSystem.h:72
 SolarSystem.h:73
 SolarSystem.h:74
 SolarSystem.h:75
 SolarSystem.h:76
 SolarSystem.h:77
 SolarSystem.h:78
 SolarSystem.h:79
 SolarSystem.h:80
 SolarSystem.h:81
 SolarSystem.h:82
 SolarSystem.h:83
 SolarSystem.h:84
 SolarSystem.h:85
 SolarSystem.h:86
 SolarSystem.h:87
 SolarSystem.h:88
 SolarSystem.h:89
 SolarSystem.h:90
 SolarSystem.h:91
 SolarSystem.h:92
 SolarSystem.h:93
 SolarSystem.h:94
 SolarSystem.h:95
 SolarSystem.h:96
 SolarSystem.h:97
 SolarSystem.h:98
 SolarSystem.h:99
 SolarSystem.h:100
 SolarSystem.h:101
 SolarSystem.h:102
 SolarSystem.h:103
 SolarSystem.h:104
 SolarSystem.h:105
 SolarSystem.h:106
 SolarSystem.h:107
 SolarSystem.h:108
 SolarSystem.h:109
 SolarSystem.h:110
 SolarSystem.h:111
 SolarSystem.h:112
 SolarSystem.h:113
 SolarSystem.h:114
 SolarSystem.h:115
 SolarSystem.h:116
 SolarSystem.h:117
 SolarSystem.h:118
 SolarSystem.h:119
 SolarSystem.h:120
 SolarSystem.h:121
 SolarSystem.h:122
 SolarSystem.h:123
 SolarSystem.h:124
 SolarSystem.h:125
 SolarSystem.h:126
 SolarSystem.h:127
 SolarSystem.h:128
 SolarSystem.h:129
 SolarSystem.h:130
 SolarSystem.h:131
 SolarSystem.h:132
 SolarSystem.h:133
 SolarSystem.h:134
 SolarSystem.h:135
 SolarSystem.h:136
 SolarSystem.h:137
 SolarSystem.h:138
 SolarSystem.h:139
 SolarSystem.h:140
 SolarSystem.h:141
 SolarSystem.h:142
 SolarSystem.h:143
 SolarSystem.h:144
 SolarSystem.h:145
 SolarSystem.h:146
 SolarSystem.h:147
 SolarSystem.h:148
 SolarSystem.h:149
 SolarSystem.h:150
 SolarSystem.h:151
 SolarSystem.h:152
 SolarSystem.h:153
 SolarSystem.h:154
 SolarSystem.h:155
 SolarSystem.h:156
 SolarSystem.h:157
 SolarSystem.h:158
 SolarSystem.h:159
 SolarSystem.h:160
 SolarSystem.h:161
 SolarSystem.h:162
 SolarSystem.h:163
 SolarSystem.h:164
 SolarSystem.h:165
 SolarSystem.h:166
 SolarSystem.h:167
 SolarSystem.h:168
 SolarSystem.h:169