write_unfragmented_json_impl Subroutine

private subroutine write_unfragmented_json_impl(data)

Write unfragmented calculation results to output JSON file

Arguments

Type IntentOptional Attributes Name
type(json_output_data_t), intent(in) :: data

Calls

proc~~write_unfragmented_json_impl~~CallsGraph proc~write_unfragmented_json_impl write_unfragmented_json_impl add add proc~write_unfragmented_json_impl->add create_object create_object proc~write_unfragmented_json_impl->create_object destroy destroy proc~write_unfragmented_json_impl->destroy error error proc~write_unfragmented_json_impl->error info info proc~write_unfragmented_json_impl->info initialize initialize proc~write_unfragmented_json_impl->initialize proc~get_basename get_basename proc~write_unfragmented_json_impl->proc~get_basename proc~get_output_json_filename get_output_json_filename proc~write_unfragmented_json_impl->proc~get_output_json_filename

Called by

proc~~write_unfragmented_json_impl~~CalledByGraph proc~write_unfragmented_json_impl write_unfragmented_json_impl proc~write_json_output write_json_output proc~write_json_output->proc~write_unfragmented_json_impl proc~run_calculation run_calculation proc~run_calculation->proc~write_json_output proc~compute_energy_and_forces compute_energy_and_forces proc~compute_energy_and_forces->proc~run_calculation proc~run_multi_molecule_calculations run_multi_molecule_calculations proc~run_multi_molecule_calculations->proc~run_calculation program~main main program~main->proc~run_calculation program~main->proc~run_multi_molecule_calculations

Variables

Type Visibility Attributes Name Initial
character(len=256), private :: basename
type(json_value), private, pointer :: dipole_obj
integer, private :: io_stat
integer, private :: iunit
type(json_core), private :: json
type(json_value), private, pointer :: main_obj
character(len=256), private :: output_file
type(json_value), private, pointer :: root

Source Code

   subroutine write_unfragmented_json_impl(data)
      !! Write unfragmented calculation results to output JSON file
      type(json_output_data_t), intent(in) :: data

      type(json_core) :: json
      type(json_value), pointer :: root, main_obj, dipole_obj
      integer :: iunit, io_stat
      character(len=256) :: output_file, basename

      output_file = get_output_json_filename()
      basename = get_basename()

      call json%initialize(real_format=JSON_REAL_FORMAT)
      call json%create_object(root, '')
      call json%create_object(main_obj, trim(basename))
      call json%add(root, main_obj)

      if (data%has_energy) call json%add(main_obj, 'total_energy', data%total_energy)

      if (data%has_dipole .and. allocated(data%dipole)) then
         call json%create_object(dipole_obj, 'dipole')
         call json%add(main_obj, dipole_obj)
         call json%add(dipole_obj, 'x', data%dipole(1))
         call json%add(dipole_obj, 'y', data%dipole(2))
         call json%add(dipole_obj, 'z', data%dipole(3))
         call json%add(dipole_obj, 'magnitude_debye', norm2(data%dipole)*AU_TO_DEBYE)
      end if

      if (data%has_gradient .and. allocated(data%gradient)) then
         call json%add(main_obj, 'gradient_norm', sqrt(sum(data%gradient**2)))
      end if
      if (data%has_hessian .and. allocated(data%hessian)) then
         call json%add(main_obj, 'hessian_frobenius_norm', sqrt(sum(data%hessian**2)))
      end if

      call logger%info("Writing JSON output to "//trim(output_file))
      open (newunit=iunit, file=trim(output_file), status='replace', action='write', iostat=io_stat)
      if (io_stat /= 0) then
         call logger%error("Failed to open "//trim(output_file)//" for writing")
         call json%destroy(root)
         return
      end if

      call json%print(root, iunit)
      close (iunit)
      call json%destroy(root)
      call logger%info("JSON output written successfully to "//trim(output_file))

   end subroutine write_unfragmented_json_impl