Read the config file that defines the problem to be solved and set all the global variables.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(my_solver_type), | intent(inout) | :: | me | |||
| character(len=*), | intent(in) | :: | filename |
the JSON config file to read |
subroutine read_config_file(me, filename) implicit none class(my_solver_type),intent(inout) :: me character(len=*),intent(in) :: filename !! the JSON config file to read type(config_file) :: f logical :: found, found_jc, found_period, found_initial_r real(wp):: jc !! jacobii constant from the file character(len=9) :: sec_str !! seconds of the reference epoch, for printing real(wp):: p !! period from file (normalized) type(patch_point),dimension(3) :: pp !! patch points logical :: use_json_file !! if true, use the JSON file. !! if false, use the CSV file. real(wp) :: lstar, tstar real(wp),dimension(:),allocatable :: jcvec, normalized_period real(wp),dimension(:),allocatable :: x0, z0, ydot0 real(wp) :: pp_period, pp_x0, pp_z0, pp_ydot0 real(wp) :: tolx !! nlesolver_tolx from the file write(*,*) '* Loading config file: '//trim(filename) call f%open(filename) write(*,*) '* Reading config file' call f%get('jc', jc, found_jc) ! one of these two must be present call f%get('period', p, found_period) ! call f%get('solve', me%mission%solve, found) call f%get('fix_initial_time', me%mission%fix_initial_time, found) call f%get('fix_initial_r', me%mission%fix_initial_r, found) call f%get('fix_ry_at_end_of_rev', me%mission%fix_ry_at_end_of_rev, found) call f%get('fix_final_ry_and_vx', me%mission%fix_final_ry_and_vx, found) call f%get('generate_plots', me%mission%generate_plots, found) call f%get('generate_trajectory_files', me%mission%generate_trajectory_files, found) call f%get('generate_json_trajectory_file', me%mission%generate_json_trajectory_file, found) call f%get('generate_guess_and_solution_files', me%mission%generate_guess_and_solution_files, found) call f%get('generate_kernel', me%mission%generate_kernel, found) call f%get('generate_defect_file', me%mission%generate_defect_file, found) call f%get('generate_solution_plots', me%mission%generate_solution_plots, found) call f%get('generate_eclipse_files', me%mission%generate_eclipse_files, found) call f%get('run_pyvista_script', me%mission%run_pyvista_script, found) call f%get('generate_rp_ra_file', me%mission%generate_rp_ra_file, found) ! if generating the kernel: have to generate the trajectory files if (me%mission%generate_kernel) me%mission%generate_trajectory_files = .true. call f%get('constrain_initial_rdot', me%mission%constrain_initial_rdot, found) call f%get('r_eclipse_bubble', me%mission%r_eclipse_bubble, found) call f%get('eclipse_dt_step', me%mission%eclipse_dt_step, found) call f%get('eclipse_optimization', me%mission%eclipse_optimization, found) call f%get('eclipse_opt_phi_min', me%mission%eclipse_opt_phi_min, found) call f%get('eclipse_opt_bubble', me%mission%eclipse_opt_bubble, found) call f%get('eclipse_opt_detect_phi', me%mission%eclipse_opt_detect_phi, found) call f%get('eclipse_opt_detect_step', me%mission%eclipse_opt_detect_step, found) call f%get('eclipse_opt_sample_step', me%mission%eclipse_opt_sample_step, found) call f%get('eclipse_opt_max_outer', me%mission%eclipse_opt_max_outer, found) call f%get('eclipse_opt_polish', me%mission%eclipse_opt_polish, found) call f%get('eclipse_opt_newton_finish', me%mission%eclipse_opt_newton_finish, found) call f%get('eclipse_opt_newton_active_tol', me%mission%eclipse_opt_newton_active_tol, found) call f%get('eclipse_opt_newton_max_iter', me%mission%eclipse_opt_newton_max_iter, found) call f%get('eclipse_opt_newton_solver_mode', me%mission%eclipse_opt_newton_solver_mode, found) call f%get('eclipse_opt_newton_damp', me%mission%eclipse_opt_newton_damp, found) call f%get('eclipse_opt_newton_damp_iter', me%mission%eclipse_opt_newton_damp_iter, found) call f%get('eclipse_opt_newton_max_active', me%mission%eclipse_opt_newton_max_active, found) call f%get('sqpopt_solve', me%mission%sqpopt_solve, found) call f%get('eclipse_opt_objective', me%mission%eclipse_opt_objective, found) call f%get('eclipse_opt_newton_damp_min', me%mission%eclipse_opt_newton_damp_min, found) call f%get('eclipse_opt_fd_step', me%mission%eclipse_opt_fd_step, found) call f%get('eclipse_opt_ks_rho', me%mission%eclipse_opt_ks_rho, found) call f%get('eclipse_opt_single_row', me%mission%eclipse_opt_single_row, found) call f%get('eclipse_opt_equilibrate', me%mission%eclipse_opt_equilibrate, found) call f%get('eclipse_opt_max_step_epoch', me%mission%eclipse_opt_max_step_epoch, found) call f%get('eclipse_opt_max_step_position', me%mission%eclipse_opt_max_step_position, found) call f%get('eclipse_opt_max_step_velocity', me%mission%eclipse_opt_max_step_velocity, found) call f%get('eclipse_opt_max_iter', me%mission%eclipse_opt_max_iter, found) call f%get('eclipse_opt_print_level', me%mission%eclipse_opt_print_level, found) call f%get('eclipse_opt_hessian_mode', me%mission%eclipse_opt_hessian_mode, found) call f%get('eclipse_opt_direct_qp', me%mission%eclipse_opt_direct_qp, found) call f%get('eclipse_opt_direct_least_squares', me%mission%eclipse_opt_direct_least_squares, found) call f%get('eclipse_opt_direct_max_changes', me%mission%eclipse_opt_direct_max_changes, found) call f%get('eclipse_opt_direct_tol', me%mission%eclipse_opt_direct_tol, found) call f%get('eclipse_opt_linear_solver', me%mission%eclipse_opt_linear_solver, found) call f%get('eclipse_opt_factorization_threads', me%mission%eclipse_opt_factorization_threads, found) call f%get('eclipse_opt_basis_rel_tol', me%mission%eclipse_opt_basis_rel_tol, found) call f%get('eclipse_opt_basis_repair', me%mission%eclipse_opt_basis_repair, found) call f%get('eclipse_opt_basis_eq_slack_weight', me%mission%eclipse_opt_basis_eq_slack_weight, found) call f%get('eclipse_opt_direct_relative_tol', me%mission%eclipse_opt_direct_relative_tol, found) call f%get('eclipse_opt_direct_accept_inexact', me%mission%eclipse_opt_direct_accept_inexact, found) call f%get('eclipse_opt_qp_solver_mode', me%mission%eclipse_opt_qp_solver_mode, found) call f%get('eclipse_opt_linesearch_mode', me%mission%eclipse_opt_linesearch_mode, found) call f%get('eclipse_opt_lbfgs_memory', me%mission%eclipse_opt_lbfgs_memory, found) call f%get('eclipse_opt_elastic_multiplier_limit', me%mission%eclipse_opt_elastic_multiplier_limit, found) call f%get('eclipse_opt_scaling', me%mission%eclipse_opt_scaling, found) call f%get('eclipse_opt_scaling_max_gradient', me%mission%eclipse_opt_scaling_max_gradient, found) call f%get('eclipse_opt_hessian_scale0', me%mission%eclipse_opt_hessian_scale0, found) call f%get('eclipse_opt_inertia_control', me%mission%eclipse_opt_inertia_control, found) call f%get('eclipse_opt_merit_mode', me%mission%eclipse_opt_merit_mode, found) call f%get('eclipse_opt_restoration_mode', me%mission%eclipse_opt_restoration_mode, found) call f%get('eclipse_opt_qp_max_step', me%mission%eclipse_opt_qp_max_step, found) call f%get('eclipse_opt_max_time', me%mission%eclipse_opt_max_time, found) call f%get('eclipse_opt_epoch_scale', me%mission%eclipse_opt_epoch_scale, found) call f%get('eclipse_opt_ctol', me%mission%eclipse_opt_ctol, found) call f%get('eclipse_opt_ktol', me%mission%eclipse_opt_ktol, found) call f%get('eclipse_filetype', me%mission%eclipse_filetype, found) call f%get('eclipse_file_se_positions', me%mission%eclipse_file_se_positions, found) call f%get('use_splined_ephemeris', me%mission%use_splined_ephemeris, found) call f%get('dt_spline_sec', me%mission%dt_spline_sec, found) call f%get('pointmass_central_body', me%mission%pointmass_central_body, found) call f%get('include_pointmass_earth', me%mission%include_pointmass_earth, found) call f%get('include_pointmass_sun', me%mission%include_pointmass_sun, found) call f%get('include_pointmass_jupiter', me%mission%include_pointmass_jupiter, found) call f%get('initial_guess_from_file', me%mission%initial_guess_from_file, found) call f%get('solver_mode', me%mission%solver_mode, found) if (.not. found) me%mission%solver_mode = 1 call f%get('jacobian_mode', me%mission%jacobian_mode, found) if (.not. found) me%mission%jacobian_mode = 1 if (me%mission%jacobian_mode<1 .or. me%mission%jacobian_mode>2) error stop 'error: jacobian_mode must be 1 or 2.' call f%get('stm_rtol', me%mission%stm_rtol, found) call f%get('stm_atol', me%mission%stm_atol, found) call f%get('func_with_stm', me%mission%func_with_stm, found) call f%get('epoch_variable_mode', me%mission%epoch_variable_mode, found) if (.not. found) me%mission%epoch_variable_mode = 1 if (me%mission%epoch_variable_mode<1 .or. me%mission%epoch_variable_mode>2) & error stop 'error: epoch_variable_mode must be 1 or 2.' call f%get('epoch_scale', me%mission%epoch_scale, found) if (.not. found) me%mission%epoch_scale = 10.0_wp if (me%mission%epoch_scale <= 0.0_wp) error stop 'error: epoch_scale must be > 0.' call f%get('xscale_mode', me%mission%xscale_mode, found) if (.not. found) me%mission%xscale_mode = 1 if (me%mission%xscale_mode<1 .or. me%mission%xscale_mode>2) error stop 'error: xscale_mode must be 1 or 2.' call f%get('epoch_scale_factor', me%mission%epoch_scale_factor, found) if (.not. found) me%mission%epoch_scale_factor = 1.0_wp if (me%mission%epoch_scale_factor <= 0.0_wp) error stop 'error: epoch_scale_factor must be > 0.' !optional global parameter inputs: call f%get('mu_earth' , mu_earth, found) call f%get('mu_moon' , mu_moon, found) call f%get('mu_sun' , mu_sun, found) call f%get('mu_jupiter', mu_jupiter, found) call f%get('rad_moon' , rad_moon, found) call f%get('rad_sun' , rad_sun, found) call f%get('rad_earth' , rad_earth, found) call f%get('maxnum' , maxnum, found) call f%get('grav_n' , grav_n, found) call f%get('grav_m' , grav_m, found) call f%get('grav_frame', grav_frame,found) call f%get('xscale_x0' , xscale_x0, found) ! scale values (defaults are good values for the 4500 Rp case) if (.not. found) xscale_x0 = [1.0e+05_wp,1.0e+05_wp,1.0e+05_wp,2.0e+00_wp,2.0e+00_wp,2.0e+00_wp] if (size(xscale_x0) /= 6) error stop 'error: xscale_x0 must be a 6 element vector' call f%get('fscale_xf', fscale_xf, found) if (.not. found) fscale_xf = [1.0e+04_wp,1.0e+04_wp,1.0e+04_wp,1.0_wp,1.0_wp,1.0_wp] if (size(fscale_xf) /= 6) error stop 'error: fscale_xf must be a 6 element vector' call f%get('fscale_rdot' , fscale_rdot, found) ! rdot function scale value call f%get('use_battin_gravity', use_battin_gravity, found) call f%get('object_id', object_id, found) call f%get('object_name', object_name, found); if (.not. found) object_name = 'HALO' call f%get('leapseconds_file', leapseconds_file, found); if (.not. found) leapseconds_file = 'kernel/naif0012.tls' call f%get('mkspk_path', mkspk_path, found); if (.not. found) mkspk_path = 'kernel/mkspk' call f%get('polynom_degree', polynom_degree, found) call f%get('output_spk_type', output_spk_type, found) call f%get('segment_id', segment_id, found); if (.not. found) segment_id = 'SPK_STATES_09' call f%get('min_export_time_step', min_export_time_step, found) min_export_time_step = abs(min_export_time_step) ! only non-negative values allowed ! required inputs: call f%get('N_or_S', me%mission%N_or_S ) call f%get('L1_or_L2', me%mission%L1_or_L2 ) ! reach the epoch. can specify either calendar date or et: call read_epoch(f, me%mission%epoch_mode, & me%mission%year, & me%mission%month, & me%mission%day, & me%mission%hour, & me%mission%minute, & me%mission%sec, & me%mission%et_ref) ! tolerances: call f%get('rtol', me%mission%rtol , found ) call f%get('atol', me%mission%atol , found ) call f%get('nlesolver_tol', me%mission%nlesolver_tol , found ) call f%get('nlesolver_tolx', tolx, found ) if (found) me%mission%nlesolver_tolx = tolx ! if fix_initial_r is true, can specify the r to use ! [otherwise, the initial guess is used from the patch points] call f%get('initial_r', me%mission%initial_r , found_initial_r ) call f%get('n_revs', me%mission%n_revs ) call f%get('ephemeris_file', me%mission%ephemeris_file ) call f%get('gravfile', me%mission%gravfile ) call f%get('patch_point_file',me%mission%patch_point_file ) call f%get('patch_point_file_is_periapsis',me%mission%patch_point_file_is_periapsis, found ) call f%get('moon_pa_file', me%mission%moon_pa_file, found) if (.not. found) me%mission%moon_pa_file = 'data/moon_pa_2000_2100.csv' call f%close() ! cleanup --------------------------------------------------- use_json_file = index(me%mission%patch_point_file, '.json') > 0 if (.not. use_json_file) error stop 'error: patch point file must be a JSON file.' if (use_json_file .and. (found_jc .eqv. found_period)) & error stop 'error: must either specify period or jc in the config file to use JSON patch point file.' ! csv file no longer supported ! set the epoch: call me%mission%update_epoch() ! read the patch point file and load it: ! read the JSON file: write(*,*) '* Reading JSON patch point file: '//trim(me%mission%patch_point_file) call f%open(me%mission%patch_point_file) ! all inputs are required: write(*,*) '* getting data... ' call f%get('lstar', lstar ) call f%get('tstar', tstar ) call f%get('jc', jcvec ) call f%get('period',normalized_period) call f%get('x0', x0 ) call f%get('z0', z0 ) call f%get('ydot0', ydot0 ) call f%close() ! write(*,'(A, *(F6.2,",",1X))') 'period range in database (days): ', & ! normalized_period * tstar * sec2day ! which is the independant variable: if (found_jc) then pp_period = interpolate_point(jcvec, normalized_period, jc) pp_x0 = interpolate_point(jcvec, x0, jc) pp_z0 = interpolate_point(jcvec, z0, jc) pp_ydot0 = interpolate_point(jcvec, ydot0, jc) else pp_period = p pp_x0 = interpolate_point(normalized_period, x0, p) pp_z0 = interpolate_point(normalized_period, z0, p) pp_ydot0 = interpolate_point(normalized_period, ydot0, p) end if ! the CR3BP orbit of the initial guess (normalized units; the state is at the ! table's reference point, x0, 0, z0, 0, ydot0, 0, relative to the barycenter): write(*,'(A)') ' * CR3BP orbit of the initial guess (normalized units):' write(*,'(A,1X,F18.6)') ' lstar (km): ', lstar write(*,'(A,1X,F18.8)') ' tstar (sec): ', tstar write(*,'(A,1X,F18.12)') ' mu: ', compute_crtpb_parameter(mu_earth,mu_moon) write(*,'(A,1X,F18.12)') ' period: ', pp_period write(*,'(A,1X,F18.12)') ' x0: ', pp_x0 write(*,'(A,1X,F18.12)') ' z0: ', pp_z0 write(*,'(A,1X,F18.12)') ' ydot0: ', pp_ydot0 write(*,'(A,1X,F18.12)') ' Jacobi constant: ', & compute_jacobi_constant(compute_crtpb_parameter(mu_earth,mu_moon), & [pp_x0, zero, pp_z0, zero, pp_ydot0, zero]) call me%mission%generate_patch_points(lstar, tstar, pp_period, & pp_x0, pp_z0, pp_ydot0, pp) me%mission%periapsis = pp(1) me%mission%quarter = pp(2) me%mission%apoapsis = pp(3) ! write(*,'(a15,1x,F10.6,1x,a)') 'periapsis t: ' , me%mission%periapsis%t, 'days' ! write(*,'(a15,1x,F10.6,1x,a)') 'quarter t: ' , me%mission%quarter%t, 'days' ! write(*,'(a15,1x,F10.6,1x,a)') 'apoapsis t: ' , me%mission%apoapsis%t, 'days' ! compute some time variables: me%mission%period = me%mission%apoapsis%t * 2.0_wp ! Halo period [days] me%mission%period8 = me%mission%period / 8.0_wp ! 1/8 of Halo period [days] write(*,*) '' write(sec_str,'(f9.6)') me%mission%sec if (sec_str(1:1)==' ') sec_str(1:1) = '0' ! (zero-padded seconds: 00.000000) write(*,'(A30,1x,f18.6,a,i4,a,i2.2,a,i2.2,a,i2.2,a,i2.2,a,a,a)') ' Reference ephemeris time: ', & me%mission%et_ref, & ' (', me%mission%year,'-',& me%mission%month,'-',& me%mission%day,' ',& me%mission%hour,':',& me%mission%minute,':',& sec_str, ' TDB)' write(*,'(a30,1x,f21.17)') ' Orbit period (days): ', me%mission%period contains !***************************************************************************************** !********************************************** !> ! Iterpolate if necessary to compute `fvec(x)`. ! We will allow data sets that are not strictly increasing/decreasing ! (`jc` isn't, but `period` is for L2 file), ! so, first the input `x` is checked to see if that is a point in `xvec`. ! If not, the bspline interpolation is used. function interpolate_point(xvec, fvec, x) result(y) implicit none real(wp),dimension(:),intent(in) :: xvec real(wp),dimension(:),intent(in) :: fvec real(wp),intent(in) :: x real(wp) :: y !! `fvec(x)` type(bspline_1d) :: spline integer :: i, iflag, irow integer,parameter :: kx = 4 !! spline order (cubic bspline) integer,parameter :: idx = 0 !! interpolate value only ! so, first just look for the exact point: irow = 0 do i=1,size(xvec) if (xvec(i)==x) then irow = i exit end if end do if (irow/=0) then ! if found, then use it: y = fvec(irow) else call spline%initialize(xvec,fvec,kx,iflag, extrap=.true.) if (iflag/=0) then select case (iflag) case(2); error stop 'bspline error: iknot out of range.' case(3); error stop 'bspline error: nx out of range.' case(4); error stop 'bspline error: kx out of range.' case(5); error stop 'bspline error: x not strictly increasing.' end select end if call spline%evaluate(x,idx,y,iflag) if (iflag/=0) error stop 'error evaluating bspline.' end if end function interpolate_point !********************************************** end subroutine read_config_file