!===============================================================================
!
! hmc_write_h.F90
!
!-------------------------------------------------------------------------------
!
! Copyright (C) 2006-2013 Hinnerk Stueben, Yoshifumi Nakamura 
!
! This file is part of BQCD -- Berlin Quantum ChromoDynamics program
!
! BQCD is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! BQCD is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with BQCD.  If not, see <http://www.gnu.org/licenses/>.
!
!-------------------------------------------------------------------------------
# include "defs.h"

!-------------------------------------------------------------------------------
subroutine hmc_write_h(unit, oldnew, h, hp, hg, hd, hf1, hf2, hf3, hp_qed, hg_qed, hp_axion, h_axion)

  use module_counter
  use module_function_decl
  use module_switches
  implicit none

  integer, intent(in)      :: unit
  character(*), intent(in) :: oldnew
  REAL, intent(in)         :: h, hp, hg, hd, hf1, hf2, hf3, hp_qed, hg_qed, hp_axion, h_axion

  character(6)  :: key
  integer, save :: count = 0
  integer       :: i

  character(5), parameter, dimension(3) :: &
                             keys = (/ "%Hold", "%Hnew", "%Hdif" /)
  character(len=50) :: fmt_h
  character(len=50) :: fmt_b

  i=7
  if (switches%qed) i=i+2
  if (switches%axion) i=i+2
  write(key,"(i2)")i
  fmt_h = "(1x, 2a, a10, 2x, "//trim(key)//"a15)"
  fmt_b = "(2x, a5, i10, 2x, "//trim(key)//"e15.7)"

  count = count + 1
  if (count == 1) then
     do i = 1, 3
        if (my_pe() == 0) then
           if (switches%qed .and. switches%axion)write(unit, fmt_h) "T", keys(i), "traj", &
              "H_total", "H_generator", "H_gauge", "H_det", &
              "H_fermion_1", "H_fermion_2", "H_fermion_3", &
              "H_generatorQED", "H_gaugeQED", &
              "H_mom_axion", "H_axion"

           if (switches%qed .and. .not. switches%axion)write(unit, fmt_h) "T", keys(i), "traj", &
              "H_total", "H_generator", "H_gauge", "H_det", &
              "H_fermion_1", "H_fermion_2", "H_fermion_3", &
              "H_generatorQED", "H_gaugeQED"

           if (.not. switches%qed .and. switches%axion)write(unit, fmt_h) "T", keys(i), "traj", &
              "H_total", "H_generator", "H_gauge", "H_det", &
              "H_fermion_1", "H_fermion_2", "H_fermion_3", &
              "H_mom_axion", "H_axion"


           if (.not. switches%qed .and. .not. switches%axion)write(unit, fmt_h) "T", keys(i), "traj", &
              "H_total", "H_generator", "H_gauge", "H_det", &
              "H_fermion_1", "H_fermion_2", "H_fermion_3"
        endif
     enddo
  endif

  if (oldnew == "old")  key = keys(1)
  if (oldnew == "new")  key = keys(2)
  if (oldnew == "diff") key = keys(3)

  if (my_pe() == 0) then
     if (switches%qed .and. switches%axion)write(unit, fmt_b) key, counter%traj, &
     h, hp, hg, hd, hf1,hf2, hf3, hp_qed, hg_qed, hp_axion, h_axion
     if (switches%qed .and. .not. switches%axion)write(unit, fmt_b) key, counter%traj, &
     h, hp, hg, hd, hf1,hf2, hf3, hp_qed, hg_qed
     if (.not. switches%qed .and. switches%axion)write(unit, fmt_b) key, counter%traj, &
     h, hp, hg, hd, hf1,hf2, hf3, hp_axion, h_axion
     if (.not. switches%qed .and. .not. switches%axion)write(unit, fmt_b) key, counter%traj, &
     h, hp, hg, hd, hf1,hf2, hf3
  endif
end

!===============================================================================
