def save(self, rec_file): """Pickle the whole `Analyzer` class. Parameters ---------- rec_file : str Directory path to the file where the `Analyzer` is serialized """ self.nlp = None save(self, rec_file)
def save(self, rec_file): """Saves data corresponding to a meta model instance. Parameters ---------- rec_file : str Name of the file in `latom.data.metamodels` where the meta model is stored """ d = {'Isp': self.Isp, 'twr': self.twr, 'm_prop': self.m_prop, 'failures': self.failures} save(d, self.abs_path(rec_file))
def save(self, rec_file): """Saves data corresponding to a surrogate model instance. Parameters ---------- rec_file : str Name of the file in `latom.data.smt` where the surrogate model is stored """ d = { 'limits': self.limits, 'x_samp': self.x_samp, 'm_prop': self.m_prop, 'failures': self.failures } save(d, self.abs_path(rec_file))
twr = np.hstack((d1['twr'], d2['twr'])) isp = d1['Isp'] elif np.isclose(direction, 1): # same thrust/weight ratio bounds and step m_prop = np.hstack((d1['m_prop'], d2['m_prop'])) energy = np.hstack((d1['energy'], d2['energy'])) failures = np.hstack((d1['failures'], d2['failures'])) twr = d1['twr'] isp = np.hstack((d1['Isp'], d2['Isp'])) else: raise ValueError('matrices not compatible') print(f"\nIsp: {isp[0]:.4f}s - {isp[-1]:.4f}s at {(isp[1] - isp[0]):.4f}s step") print(f"twr: {twr[0]:.4f} - {twr[-1]:.4f} at {(twr[1] - twr[0]):.4f} step") print(f"Max spacecraft energy: {np.max(energy):.4f} m^2/s^2\n") if plot == 'mp': rs = RespSurf(isp, twr, m_prop.T, 'propellant fraction', nb_lines=50) rs.plot() elif plot == 'en': rs = RespSurf(isp, twr, energy.T, 'spacecraft energy', nb_lines=50) rs.plot() if fid is not None: d = {'Isp': isp, 'twr': twr, 'm_prop': m_prop, 'energy': energy, 'failures': failures} fid = '/'.join([dirname_metamodels, fid]) save(d, fid)
def run_continuation(self, rec_file=None): """Iterative solution of all optimal control problems corresponding to the different thrust/weight ratios in `twr_list` using a continuation method in which the solution of the problem ``i`` is set as initial guess for the problem ``i + 1``. The first solution is computed using the initial guess provided by `TwoDimLLO2HEOGuess` class. Parameters ---------- rec_file : str or None, optional Directory path for the file in which the thrust/weight ratios, propellant masses, specific energies and solutions are recorded or ``None``. Default is ``None`` """ nlp = None params = { 'ra_heo': self.guess.ht.arrOrb.ra, 'rp_llo': self.guess.ht.depOrb.rp, 'vp_hoh': self.guess.ht.transfer.vp, 'thetaf_pow': self.guess.pow.thetaf } t0_loop = time() for i in range(np.size(self.twr_list)): if i == 0: # first solution nlp = self.nlp else: # subsequent solutions self.sc.update_twr(self.twr_list[i]) nlp = TwoDimLLO2ApoNLP(self.body, self.sc, self.alt, None, None, (-np.pi / 2, np.pi / 2), None, self.nlp.method, self.nlp.nb_seg, self.nlp.order, self.nlp.solver, self.phase_name, snopt_opts=self.nlp.snopt_opts, params=params) # solve NLP print(f"\nIteration {i}\nThrust/weight ratio: {self.sc.twr:.6f}\n") t0_iter = time() failed = nlp.p.run_driver() tf_iter = time() nlp.cleanup() print( f"\nTime to solve the NLP problem: {(tf_iter - t0_iter):.6f} s\n" ) if failed: print('\nNLP solution failed! Exiting loop\n') break # extract and store the NLP solution params['tof'], params['states'], params[ 'controls'] = self.get_discretization_phase( nlp.p, nlp.phase_name) self.sol_list[self.sol_keys[i]] = self.get_solution_dictionary( nlp.p) # compute the specific energy of the spacecraft at the end of the powered phase and the total required # propellant mass including the final insertion burn states_end = params['states'][-1] en, m_prop = self.compute_energy_mprop(states_end[0], states_end[2], states_end[3], states_end[-1]) # store the spacecraft energy and propellant mass self.energy_list[i] = en self.m_prop_list[i] = m_prop tf_loop = time() print( f"\nTotal time for continuation loop: {(tf_loop - t0_loop):.6f} s\n" ) self.nlp = nlp if rec_file is not None: d = { 'twr': self.twr_list, 'm_prop': self.m_prop_list, 'energy': self.energy_list, 'sol': self.sol_list } save(d, rec_file)
m_prop = compute_solutions(moon, isp, twr) elif compute_err: m_prop = d['m_prop'] else: m_prop = None # errors if compute_err: err_mm = np.fabs(m_prop.flatten() - m_prop_mm.flatten()) err_lhs = np.fabs(m_prop.flatten() - m_prop_lhs.flatten()) if m_prop_full is not None: err_full = np.fabs(m_prop.flatten() - m_prop_full.flatten()) else: err_full = None fid.write( f"\n{'mm':4s}\t{im_mm:20s}\t{np.min(err_mm):.16e}\t{np.max(err_mm):.16e}" ) fid.write( f"\n{'lhs':4s}\t{tm_lhs:20s}\t{np.min(err_lhs):.16e}\t{np.max(err_lhs):.16e}" ) fid.write( f"\n{'full':4s}\t{tm_full:20s}\t{np.min(err_full):.16e}\t{np.max(err_full):.16e}" ) if store: d = {'Isp': isp, 'twr': twr, 'm_prop': m_prop} save(d, fid_real) if fid is not None: fid.close()
nb_samp, snopt_opts=snopt_opts, rec_file=rec_file, t=heo_period, rp=heo_rp, log_scale=log_scale) else: mm = TwoDimLLO2ApoMetaModel( distributed=distributed, extrapolate=extrapolate, method=interp_method, training_data_gradients=training_data_gradients, vec_size=vec_size) mm.sampling(moon, twr, isp, llo_alt, None, transcription_method, segments, order, solver, nb_samp, snopt_opts=snopt_opts, rec_file=rec_file, t=heo_period, rp=heo_rp) mm.plot() save(mm, rec_file_obj)