MultiOpt Namespace Reference

Functions

def ComputeInertiaProfiles (nodal_values, duration, N_t)
 
def RealFun (x)
 
def ToyFun (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
 
dictionary TEST_OPTIONS = {'xtol':1e-3, 'ftol':1e-4, 'maxiter':3}
 
dictionary REAL_OPTIONS = {'xtol':1e-3, 'ftol':1e-3, 'maxiter':20}
 
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)
 
 xc = np.random.rand(3*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 MultiOpt.ComputeInertiaProfiles (   nodal_values,
  duration,
  N_t 
)
102 def ComputeInertiaProfiles(nodal_values, duration, N_t):
103  # pre-computes time evolution of tensor of inertia for MercuryDPM driver file
104  # nodal_values is an array N X 3 equispaced values of three principal moments of inertia
105  N = len(nodal_values)//3
106  n_I1 = nodal_values[:N]
107  n_I2 = nodal_values[N:2*N]
108  n_I3 = nodal_values[2*N:]
109  x = np.arange(0, duration * (N/(N-1)), duration/(N-1))
110  cs1 = CubicSpline(x, n_I1, bc_type = 'clamped')
111  cs2 = CubicSpline(x, n_I2, bc_type = 'clamped')
112  cs3 = CubicSpline(x, n_I3, bc_type = 'clamped')
113  xc = np.arange(0, duration * (N_t/(N_t-1)), duration/(N_t-1))
114  I1 = cs1(xc); dI1 = cs1(xc, 1)
115  I2 = cs2(xc); dI2 = cs2(xc, 1)
116  I3 = cs3(xc); dI3 = cs3(xc, 1)
117  return x, n_I1, n_I2, n_I3, xc, I1, dI1, I2, dI2, I3, dI3
118 
def ComputeInertiaProfiles(nodal_values, duration, N_t)
Definition: MultiOpt.py:102

Referenced by RealFun().

◆ main()

def MultiOpt.main ( )
201 def main():
202  global SourceDir, BuildDir, initial_phi, InitialTheta, FinalPhi, FinalTheta
203 
204  # Set up terminal output
205  clr = colorClass()
206  clr.set_use_colors(True)
207  clr.def_colors()
208 
209  # Hello tag
210  print(clr.END + clr.BOLD + clr.VIOLET + "-------------------------------------------")
211  print(clr.END + clr.BOLD + clr.VIOLET + " MercuryDPM MultiOpt experiment ")
212  print(clr.END + clr.BOLD + clr.VIOLET + "-------------------------------------------")
213 
214 
215  # If the values for sourceDir and Build dir are provided in the command, then overwrite the default values
216  if (len(sys.argv) == 3): # Full format: "run source_dir build_dir"
217  SourceDir = sys.argv[1]
218  BuildDir = sys.argv[2]
219  print("Source dir: ", SourceDir)
220  print("Build dir: ", BuildDir)
221 
222  # Path to optimization directory
223  OptPath = BuildDir + "/Drivers/Clump/ChangingTOI/opt"
224  if ( not os.path.exists( OptPath )):
225  os.mkdir( OptPath )
226 
227  # Let Mercury code know what is the goal orientation
228  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/init_orientation.txt', (InitialTheta, InitialPhi), delimiter =' ')
229  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/final_orientation.txt', (FinalTheta, FinalPhi), delimiter =' ')
230 
231  print(clr.END)
232 
233  if (TEST_MODE):
234  res = minimize(ToyFun, x0, method='Powell', options = TEST_OPTIONS)
235  else:
236  res = minimize(RealFun, x0, method='Powell', options = REAL_OPTIONS)
237 
238  # Alternative optimization tools
239  #res = minimizeSPSA(real_fun, x0=x0, niter=1000, paired=False)
240  #res = minimizeCompass(real_fun, x0=x0, deltatol=0.01, paired=False)
241 
242  print(res)
243 
244  if (TEST_MODE): print(clr.END + "Converged to the known solution: ", np.allclose(res["x"], xc))
245 
246 # 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: MultiOpt.py:201

References Eigen::internal.print().

◆ RealFun()

def MultiOpt.RealFun (   x)
122 def RealFun(x):
123  import os
124  import subprocess
125  global SourceDir, BuildDir, N
126  global ProgDuration, SymDuration, NTimesteps, FunCount, FunBatch
127  global QMax, QMin, I0, QInitValues, QFinalValues
128 
129  ret = 0 # Return zero by default
130 
131  # Project the unconstrained vector of optimization parameters x into (QMin,QMax)
132  q = (QMax + QMin) / 2. + (QMax - QMin) / 2. * np.cos(x)
133 
134  # Expand the set of control parameters with initial and final values
135  q_ext_1 = np.hstack((QInitValues[0], q[:N], QFinalValues[0]))
136  q_ext_2 = np.hstack((QInitValues[1], q[N:], QFinalValues[1]))
137 
138  # Compute principal components of inertia
139  I_1 = 0.5 * I0 * (1 + q_ext_2*q_ext_2)
140  I_2 = 0.5 * I0 * (1 + q_ext_1*q_ext_1)
141  I_3 = 0.5 * I0 * (q_ext_1*q_ext_1 + q_ext_2*q_ext_2)
142  nodal_values = np.hstack((I_1, I_2, I_3))
143  print("Nodal values of principal moments of inertia:", nodal_values)
144 
145  # Compute cubic splines approximating the evolution of TOI
146  xx, n_I1, n_I2, n_I3, xc, I1, dI1, I2, dI2, I3, dI3 = ComputeInertiaProfiles(nodal_values, ProgDuration, NTimesteps)
147 
148  arr = np.transpose(np.vstack((xc, I1, I2, I3, dI1, dI2, dI3)))
149 
150  # Expanded evolution incluidng the final piece with constant TOI
151  I1_ext = np.hstack((I1, I1[len(I1)-1] * np.ones((int) (NTimesteps * (SymDuration - ProgDuration) / SymDuration))))
152  I2_ext = np.hstack((I2, I2[len(I2)-1] * np.ones((int) (NTimesteps * (SymDuration - ProgDuration) / SymDuration))))
153  I3_ext = np.hstack((I3, I3[len(I3)-1] * np.ones((int) (NTimesteps * (SymDuration - ProgDuration) / SymDuration))))
154  T_I = np.arange(0, SymDuration, SymDuration / len(I1_ext)) # Timestamp
155 
156  # Save inertia control parameters of the simulation
157  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/inertia_profiles.txt', arr)
158  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/I_1.txt', I1_ext)
159  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/I_2.txt', I2_ext)
160  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/I_3.txt', I3_ext)
161  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/T_I.txt', T_I)
162  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/nodal_values.txt', nodal_values)
163 
164  # Run Mercury Driver file
165  # (feed program duration, simulation duration, and initial angular velocity
166  # as command line arguments)
167  os.chdir(BuildDir + '/Drivers/Clump/ChangingTOI')
168  command = [BuildDir + '/Drivers/Clump/ChangingTOI/ChangingTOI']
169  command.append('-p1')
170  command.append(str(ProgDuration))
171  command.append('-p2')
172  command.append(str(SymDuration))
173  command.append('-p3')
174  command.append(str(BaseAngvel))
175  print(command)
176  subprocess.run(command)
177 
178  # Get the functional value
179  f = open(BuildDir + '/Drivers/Clump/ChangingTOI/opt/functional.txt', "r")
180  ret = float(f.read())
181 
182  # Save the functional value in the history batch
183  if (FunCount == 0):
184  FunBatch = ret
185  else:
186  FunBatch = np.append(FunBatch, ret)
187  np.savetxt(BuildDir + '/Drivers/Clump/ChangingTOI/opt/fun_batch.txt', FunBatch)
188 
189  FunCount+=1 # Number of functional evaluations
190  return ret
191 
192 # Toy functional necessary for testing purposes
def RealFun(x)
Definition: MultiOpt.py:122
str
Definition: compute_granudrum_aor.py:141

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

◆ ToyFun()

def MultiOpt.ToyFun (   x)
193 def ToyFun(x):
194  global xc
195  ret = 0
196  for m in range(len(x)):
197  ret = ret + (x[m] - xc[m])**2
198  return ret
199 
200 
def ToyFun(x)
Definition: MultiOpt.py:193

Variable Documentation

◆ BaseAngvel

int MultiOpt.BaseAngvel = 5

◆ BuildDir

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

◆ delta

◆ FinalPhi

int MultiOpt.FinalPhi = delta

◆ FinalTheta

int MultiOpt.FinalTheta = delta

◆ FunBatch

bool MultiOpt.FunBatch = False

◆ FunCount

int MultiOpt.FunCount = 0

◆ I0

int MultiOpt.I0 = 10

◆ InitialPhi

int MultiOpt.InitialPhi = np.pi/4

◆ InitialTheta

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

◆ N

int MultiOpt.N = 5

◆ NTimesteps

int MultiOpt.NTimesteps = ProgDuration / Timestep

◆ ProgDuration

int MultiOpt.ProgDuration = 100

◆ QFinalValues

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

◆ QInitValues

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

◆ QMax

float MultiOpt.QMax = 1.5

◆ QMin

float MultiOpt.QMin = 0.5

◆ REAL_OPTIONS

dictionary MultiOpt.REAL_OPTIONS = {'xtol':1e-3, 'ftol':1e-3, 'maxiter':20}

◆ SourceDir

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

◆ SymDuration

int MultiOpt.SymDuration = 105

◆ TEST_MODE

bool MultiOpt.TEST_MODE = False

◆ TEST_OPTIONS

dictionary MultiOpt.TEST_OPTIONS = {'xtol':1e-3, 'ftol':1e-4, 'maxiter':3}

◆ Timestep

float MultiOpt.Timestep = 5.56833e-05 * 50

◆ x0

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

◆ xc