SingleRun Namespace Reference

Functions

def ComputeInertiaProfiles (nodal_values, duration, N_t)
 
def RealFun (x)
 
def main ()
 

Variables

string SourceDir = "/home/iostanin/Desktop/MercuryGit/mercurydpm"
 
string BuildDir = "/home/iostanin/Desktop/MercuryGit/MercuryBuild"
 
float Timestep = 5.56833e-05 * 50
 
int N = 5
 
bool TEST_MODE = False
 
float QMin = 0.5
 
float QMax = 1.5
 
int I0 = 10
 
int ProgDuration = 100
 
int SymDuration = 105
 
int BaseAngvel = 5
 
int NTimesteps = ProgDuration / Timestep
 
 QInitValues = np.array([1, 1])
 
 QFinalValues = np.array([1, 1])
 
 x0 = np.ones(2*N)
 
int delta = np.pi/1000.
 
 InitialTheta = np.arccos(1/3**0.5)
 
int InitialPhi = np.pi/4
 
int FinalTheta = delta
 
int FinalPhi = delta
 
int FunCount = 0
 
bool FunBatch = False
 

Function Documentation

◆ ComputeInertiaProfiles()

def SingleRun.ComputeInertiaProfiles (   nodal_values,
  duration,
  N_t 
)
51 def ComputeInertiaProfiles(nodal_values, duration, N_t):
52  # pre-computes time evolution of tensor of inertia for MercuryDPM driver file
53  # nodal_values is an array N X 3 equispaced values of three principal moments of inertia
54  N = len(nodal_values)//3
55  n_I1 = nodal_values[:N]
56  n_I2 = nodal_values[N:2*N]
57  n_I3 = nodal_values[2*N:]
58  x = np.arange(0, duration * (N/(N-1)), duration/(N-1))
59  cs1 = CubicSpline(x, n_I1, bc_type = 'clamped')
60  cs2 = CubicSpline(x, n_I2, bc_type = 'clamped')
61  cs3 = CubicSpline(x, n_I3, bc_type = 'clamped')
62  xc = np.arange(0, duration * (N_t/(N_t-1)), duration/(N_t-1))
63  I1 = cs1(xc); dI1 = cs1(xc, 1)
64  I2 = cs2(xc); dI2 = cs2(xc, 1)
65  I3 = cs3(xc); dI3 = cs3(xc, 1)
66  return x, n_I1, n_I2, n_I3, xc, I1, dI1, I2, dI2, I3, dI3
67 
def ComputeInertiaProfiles(nodal_values, duration, N_t)
Definition: SingleRun.py:51

Referenced by RealFun().

◆ main()

def SingleRun.main ( )
142 def main():
143  global SourceDir, BuildDir, initial_phi, InitialTheta, FinalPhi, FinalTheta
144 
145  # Set up terminal output
146  clr = colorClass()
147  clr.set_use_colors(True)
148  clr.def_colors()
149 
150  # Hello tag
151  print(clr.END + clr.BOLD + clr.VIOLET + "-------------------------------------------")
152  print(clr.END + clr.BOLD + clr.VIOLET + " MercuryDPM MultiOpt experiment ")
153  print(clr.END + clr.BOLD + clr.VIOLET + "-------------------------------------------")
154 
155 
156  # If the values for sourceDir and Build dir are provided in the command, then overwrite the default values
157  if (len(sys.argv) == 3): # Full format: "run source_dir build_dir"
158  SourceDir = sys.argv[1]
159  BuildDir = sys.argv[2]
160  print("Source dir: ", SourceDir)
161  print("Build dir: ", BuildDir)
162 
163  # Path to optimization directory
164  OptPath = BuildDir + "/Drivers/Clump/ChangingTOI/opt"
165  if ( not os.path.exists( OptPath )):
166  os.mkdir( OptPath )
167 
168  # Let Mercury code know what is the goal orientation
169  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/init_orientation.txt', (InitialTheta, InitialPhi), delimiter =' ')
170  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/final_orientation.txt', (FinalTheta, FinalPhi), delimiter =' ')
171 
172  print(clr.END)
173 
174  RealFun(x0)
175 
176  print(clr.END + "Single run completed. ")
177 
178 # Main script that runs the maneuver optimization procedure
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet print(const Packet &a)
Definition: GenericPacketMath.h:1166
def main()
Definition: SingleRun.py:142
def RealFun(x)
Definition: SingleRun.py:71

References Eigen::internal.print(), and RealFun().

◆ RealFun()

def SingleRun.RealFun (   x)
71 def RealFun(x):
72  import os
73  import subprocess
74  global SourceDir, BuildDir, N
75  global ProgDuration, SymDuration, NTimesteps, FunCount, FunBatch
76  global QMax, QMin, I0, QInitValues, QFinalValues
77 
78  ret = 0 # Return zero by default
79 
80  # Project the unconstrained vector of optimization parameters x into (QMin,QMax)
81  q = (QMax + QMin) / 2. + (QMax - QMin) / 2. * np.cos(x)
82 
83  # Expand the set of control parameters with initial and final values
84  q_ext_1 = np.hstack((QInitValues[0], q[:N], QFinalValues[0]))
85  q_ext_2 = np.hstack((QInitValues[1], q[N:], QFinalValues[1]))
86 
87  # Compute principal components of inertia
88  I_1 = 0.5 * I0 * (1 + q_ext_2*q_ext_2)
89  I_2 = 0.5 * I0 * (1 + q_ext_1*q_ext_1)
90  I_3 = 0.5 * I0 * (q_ext_1*q_ext_1 + q_ext_2*q_ext_2)
91  nodal_values = np.hstack((I_1, I_2, I_3))
92  print("Nodal values of principal moments of inertia:", nodal_values)
93 
94  # Compute cubic splines approximating the evolution of TOI
95  xx, n_I1, n_I2, n_I3, xc, I1, dI1, I2, dI2, I3, dI3 = ComputeInertiaProfiles(nodal_values, ProgDuration, NTimesteps)
96 
97  arr = np.transpose(np.vstack((xc, I1, I2, I3, dI1, dI2, dI3)))
98 
99  # Expanded evolution incluidng the final piece with constant TOI
100  I1_ext = np.hstack((I1, I1[len(I1)-1] * np.ones((int) (NTimesteps * (SymDuration - ProgDuration) / SymDuration))))
101  I2_ext = np.hstack((I2, I2[len(I2)-1] * np.ones((int) (NTimesteps * (SymDuration - ProgDuration) / SymDuration))))
102  I3_ext = np.hstack((I3, I3[len(I3)-1] * np.ones((int) (NTimesteps * (SymDuration - ProgDuration) / SymDuration))))
103  T_I = np.arange(0, SymDuration, SymDuration / len(I1_ext)) # Timestamp
104 
105  # Save inertia control parameters of the simulation
106  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/inertia_profiles.txt', arr)
107  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/I_1.txt', I1_ext)
108  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/I_2.txt', I2_ext)
109  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/I_3.txt', I3_ext)
110  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/T_I.txt', T_I)
111  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/nodal_values.txt', nodal_values)
112 
113  # Run Mercury Driver file
114  # (feed program duration, simulation duration, and initial angular velocity
115  # as command line arguments)
116  os.chdir(BuildDir + '/Drivers/Clump/ChangingTOI')
117  command = [BuildDir + '/Drivers/Clump/ChangingTOI/ChangingTOI']
118  command.append('-p1')
119  command.append(str(ProgDuration))
120  command.append('-p2')
121  command.append(str(SymDuration))
122  command.append('-p3')
123  command.append(str(BaseAngvel))
124  print(command)
125  subprocess.run(command)
126 
127  # Get the functional value
128  f = open(BuildDir + '/Drivers/Clump/ChangingTOI/opt/functional.txt', "r")
129  ret = float(f.read())
130 
131  # Save the functional value in the history batch
132  if (FunCount == 0):
133  FunBatch = ret
134  else:
135  FunBatch = np.append(FunBatch, ret)
136  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/fun_batch.txt', FunBatch)
137 
138  FunCount+=1 # Number of functional evaluations
139  return ret
140 
141 
str
Definition: compute_granudrum_aor.py:141

References ComputeInertiaProfiles(), Eigen::internal.print(), and compute_granudrum_aor.str.

Referenced by main().

Variable Documentation

◆ BaseAngvel

int SingleRun.BaseAngvel = 5

◆ BuildDir

string SingleRun.BuildDir = "/home/iostanin/Desktop/MercuryGit/MercuryBuild"

◆ delta

int SingleRun.delta = np.pi/1000.

◆ FinalPhi

int SingleRun.FinalPhi = delta

◆ FinalTheta

int SingleRun.FinalTheta = delta

◆ FunBatch

bool SingleRun.FunBatch = False

◆ FunCount

int SingleRun.FunCount = 0

◆ I0

int SingleRun.I0 = 10

◆ InitialPhi

int SingleRun.InitialPhi = np.pi/4

◆ InitialTheta

SingleRun.InitialTheta = np.arccos(1/3**0.5)

◆ N

int SingleRun.N = 5

◆ NTimesteps

int SingleRun.NTimesteps = ProgDuration / Timestep

◆ ProgDuration

int SingleRun.ProgDuration = 100

◆ QFinalValues

SingleRun.QFinalValues = np.array([1, 1])

◆ QInitValues

SingleRun.QInitValues = np.array([1, 1])

◆ QMax

float SingleRun.QMax = 1.5

◆ QMin

float SingleRun.QMin = 0.5

◆ SourceDir

string SingleRun.SourceDir = "/home/iostanin/Desktop/MercuryGit/mercurydpm"

◆ SymDuration

int SingleRun.SymDuration = 105

◆ TEST_MODE

bool SingleRun.TEST_MODE = False

◆ Timestep

float SingleRun.Timestep = 5.56833e-05 * 50

◆ x0

SingleRun.x0 = np.ones(2*N)