general_aux.F90       coverage:  66.67 %func     33.33 %block


     1) module General_Aux_module
     2) 
     3)   use PFLOTRAN_Constants_module
     4) 
     5)   implicit none
     6)   
     7)   private 
     8) 
     9) #include "petsc/finclude/petscsys.h"
    10) 
    11)   PetscBool, public :: general_analytical_derivatives = PETSC_FALSE
    12)   PetscReal, public :: window_epsilon = 1.d-4
    13)   PetscReal, public :: fmw_comp(2) = [FMWH2O,FMWAIR]
    14)   PetscReal, public :: general_max_pressure_change = 5.d4
    15)   PetscInt, public :: general_max_it_before_damping = UNINITIALIZED_INTEGER
    16)   PetscReal, public :: general_damping_factor = 0.6d0
    17)   PetscReal, public :: general_itol_rel_update = UNINITIALIZED_DOUBLE
    18)   PetscReal, public :: general_itol_scaled_res = 1.d-5
    19)   PetscReal, public :: general_tough2_itol_scaled_res_e1(3,3) = 1.d-5
    20)   PetscReal, public :: general_tough2_itol_scaled_res_e2 = 1.d0
    21)   PetscBool, public :: general_tough2_conv_criteria = PETSC_FALSE
    22)   PetscInt, public :: general_debug_cell_id = UNINITIALIZED_INTEGER
    23) #if defined(MATCH_TOUGH2)
    24)   PetscBool, public :: general_temp_dep_gas_air_diff = PETSC_FALSE
    25)   PetscBool, public :: general_diffuse_xmol = PETSC_FALSE
    26)   PetscBool, public :: general_harmonic_diff_density = PETSC_TRUE
    27) #else
    28)   PetscBool, public :: general_diffuse_xmol = PETSC_TRUE
    29)   PetscBool, public :: general_temp_dep_gas_air_diff = PETSC_TRUE
    30)   PetscBool, public :: general_harmonic_diff_density = PETSC_TRUE
    31) #endif
    32) 
    33)   ! thermodynamic state of fluid ids
    34)   PetscInt, parameter, public :: NULL_STATE = 0
    35)   PetscInt, parameter, public :: LIQUID_STATE = 1
    36)   PetscInt, parameter, public :: GAS_STATE = 2
    37)   PetscInt, parameter, public :: TWO_PHASE_STATE = 3
    38)   PetscInt, parameter, public :: ANY_STATE = 4
    39)   
    40)   PetscInt, parameter, public :: PREV_TS = 1
    41)   PetscInt, parameter, public :: PREV_IT = 2
    42) 
    43)   PetscInt, parameter, public :: GENERAL_LIQUID_PRESSURE_DOF = 1
    44)   PetscInt, parameter, public :: GENERAL_GAS_PRESSURE_DOF = 1
    45)   PetscInt, parameter, public :: GENERAL_2PH_STATE_AIR_PRESSURE_DOF = 3
    46)   PetscInt, parameter, public :: GENERAL_GAS_STATE_AIR_PRESSURE_DOF = 2
    47)   PetscInt, parameter, public :: GENERAL_GAS_SATURATION_DOF = 2
    48)   
    49)   PetscInt, parameter, public :: GENERAL_ENERGY_DOF = 3
    50)   PetscInt, parameter, public :: GENERAL_LIQUID_STATE_X_MOLE_DOF = 2
    51)   
    52)   PetscInt, parameter, public :: GENERAL_STATE_INDEX = 1
    53)   PetscInt, parameter, public :: GENERAL_LIQUID_EQUATION_INDEX = 1
    54)   PetscInt, parameter, public :: GENERAL_GAS_EQUATION_INDEX = 2
    55)   PetscInt, parameter, public :: GENERAL_ENERGY_EQUATION_INDEX = 3
    56)   
    57)   PetscInt, parameter, public :: GENERAL_LIQUID_PRESSURE_INDEX = 2
    58)   PetscInt, parameter, public :: GENERAL_GAS_PRESSURE_INDEX = 3
    59)   PetscInt, parameter, public :: GENERAL_AIR_PRESSURE_INDEX = 4
    60)   PetscInt, parameter, public :: GENERAL_MOLE_FRACTION_INDEX = 5
    61)   PetscInt, parameter, public :: GENERAL_TEMPERATURE_INDEX = 6
    62)   PetscInt, parameter, public :: GENERAL_GAS_SATURATION_INDEX = 7
    63)   PetscInt, parameter, public :: GENERAL_LIQUID_FLUX_INDEX = 8
    64)   PetscInt, parameter, public :: GENERAL_GAS_FLUX_INDEX = 9
    65)   PetscInt, parameter, public :: GENERAL_ENERGY_FLUX_INDEX = 10
    66)   PetscInt, parameter, public :: GENERAL_LIQUID_CONDUCTANCE_INDEX = 11
    67)   PetscInt, parameter, public :: GENERAL_GAS_CONDUCTANCE_INDEX = 12
    68)   PetscInt, parameter, public :: GENERAL_MAX_INDEX = 13
    69)   
    70)   PetscInt, parameter, public :: GENERAL_UPDATE_FOR_DERIVATIVE = -1
    71)   PetscInt, parameter, public :: GENERAL_UPDATE_FOR_FIXED_ACCUM = 0
    72)   PetscInt, parameter, public :: GENERAL_UPDATE_FOR_ACCUM = 1
    73)   PetscInt, parameter, public :: GENERAL_UPDATE_FOR_BOUNDARY = 2
    74) 
    75)   PetscReal, parameter, public :: general_pressure_scale = 1.d0
    76)   
    77)   ! these variables, which are global to general, can be modified
    78)   PetscInt, public :: dof_to_primary_variable(3,3)
    79)   PetscInt, public :: general_2ph_energy_dof = GENERAL_TEMPERATURE_INDEX
    80)   PetscBool, public :: general_isothermal = PETSC_FALSE
    81)   PetscBool, public :: general_no_air = PETSC_FALSE
    82)   
    83)   type, public :: general_auxvar_type
    84)     PetscInt :: istate_store(2) ! 1 = previous timestep; 2 = previous iteration
    85)     PetscReal, pointer :: pres(:)   ! (iphase)
    86)     PetscReal, pointer :: sat(:)    ! (iphase)
    87)     PetscReal, pointer :: den(:)    ! (iphase) kmol/m^3 phase
    88)     PetscReal, pointer :: den_kg(:) ! (iphase) kg/m^3 phase
    89)     PetscReal :: temp
    90)     PetscReal, pointer :: xmol(:,:) ! (icomp,iphase)
    91)     PetscReal, pointer :: H(:) ! MJ/kmol
    92)     PetscReal, pointer :: U(:) ! MJ/kmol
    93) !    PetscReal, pointer :: dsat_dp(:,:)
    94) !    PetscReal, pointer :: dden_dp(:,:)
    95) !    PetscReal, pointer :: dsat_dT(:)
    96) !    PetscReal, pointer :: dden_dT(:)
    97)     PetscReal, pointer :: mobility(:) ! relative perm / kinematic viscosity
    98)     PetscReal :: effective_porosity ! factors in compressibility
    99)     PetscReal :: pert
   100) !    PetscReal, pointer :: dmobility_dp(:)
   101)     type(general_derivative_auxvar_type), pointer :: d
   102)   end type general_auxvar_type
   103)   
   104)   type, public :: general_derivative_auxvar_type
   105)     PetscReal :: por_pl
   106)     PetscReal :: denl_pl
   107)     PetscReal :: denl_T
   108)     PetscReal :: Ul_pl
   109)     PetscReal :: Ul_T
   110)     PetscReal :: Hl_pl
   111)     PetscReal :: Hl_T
   112)     PetscReal :: psat_dT
   113)     PetscReal :: mobilityl_pl
   114)     PetscReal :: mobilityl_T
   115)     PetscReal :: mobilityl_sat
   116)   end type general_derivative_auxvar_type
   117)   
   118)   type, public :: general_parameter_type
   119)     PetscReal, pointer :: diffusion_coefficient(:) ! (iphase)
   120)     PetscReal :: newton_inf_scaled_res_tol
   121)     PetscBool :: check_post_converged
   122)   end type general_parameter_type
   123)   
   124)   type, public :: general_type
   125)     PetscInt :: n_inactive_rows
   126)     PetscInt, pointer :: inactive_rows_local(:), inactive_rows_local_ghosted(:)
   127)     PetscInt, pointer :: row_zeroing_array(:)
   128) 
   129)     PetscBool :: auxvars_up_to_date
   130)     PetscBool :: inactive_cells_exist
   131)     PetscInt :: num_aux, num_aux_bc, num_aux_ss
   132)     type(general_parameter_type), pointer :: general_parameter
   133)     type(general_auxvar_type), pointer :: auxvars(:,:)
   134)     type(general_auxvar_type), pointer :: auxvars_bc(:)
   135)     type(general_auxvar_type), pointer :: auxvars_ss(:)
   136)   end type general_type
   137) 
   138)   interface GeneralAuxVarDestroy
   139)     module procedure GeneralAuxVarSingleDestroy
   140)     module procedure GeneralAuxVarArray1Destroy
   141)     module procedure GeneralAuxVarArray2Destroy
   142)   end interface GeneralAuxVarDestroy
   143)   
   144)   interface GeneralOutputAuxVars
   145)     module procedure GeneralOutputAuxVars1
   146)     module procedure GeneralOutputAuxVars2
   147)   end interface GeneralOutputAuxVars
   148)   
   149)   public :: GeneralAuxCreate, &
   150)             GeneralAuxDestroy, &
   151)             GeneralAuxSetEnergyDOF, &
   152)             GeneralAuxVarCompute, &
   153)             GeneralAuxVarInit, &
   154)             GeneralAuxVarCopy, &
   155)             GeneralAuxVarDestroy, &
   156)             GeneralAuxVarStrip, &
   157)             GeneralAuxVarUpdateState, &
   158)             GeneralAuxVarPerturb, &
   159)             GeneralPrintAuxVars, &
   160)             GeneralOutputAuxVars
   161) 
   162) contains
   163) 
   164) ! ************************************************************************** !
   165) 
   166) function GeneralAuxCreate(option)
   167)   ! 
   168)   ! Allocate and initialize auxiliary object
   169)   ! 
   170)   ! Author: Glenn Hammond
   171)   ! Date: 03/07/11
   172)   ! 
   173) 
   174)   use Option_module
   175) 
   176)   implicit none
   177) 
   178)   type(option_type) :: option
   179)     
   180)   type(general_type), pointer :: GeneralAuxCreate
   181)   
   182)   type(general_type), pointer :: aux
   183) 
   184)   dof_to_primary_variable(1:3,1:3) = &
   185)     reshape([GENERAL_LIQUID_PRESSURE_INDEX, GENERAL_MOLE_FRACTION_INDEX, &
   186)              GENERAL_TEMPERATURE_INDEX, &
   187)              GENERAL_GAS_PRESSURE_INDEX, GENERAL_AIR_PRESSURE_INDEX, &
   188)              GENERAL_TEMPERATURE_INDEX, &
   189)              GENERAL_GAS_PRESSURE_INDEX, GENERAL_GAS_SATURATION_INDEX, &
   190)              general_2ph_energy_dof],shape(dof_to_primary_variable))
   191)   
   192)   allocate(aux) 
   193)   aux%auxvars_up_to_date = PETSC_FALSE
   194)   aux%inactive_cells_exist = PETSC_FALSE
   195)   aux%num_aux = 0
   196)   aux%num_aux_bc = 0
   197)   aux%num_aux_ss = 0
   198)   nullify(aux%auxvars)
   199)   nullify(aux%auxvars_bc)
   200)   nullify(aux%auxvars_ss)
   201)   aux%n_inactive_rows = 0
   202)   nullify(aux%inactive_rows_local)
   203)   nullify(aux%inactive_rows_local_ghosted)
   204)   nullify(aux%row_zeroing_array)
   205) 
   206)   allocate(aux%general_parameter)
   207)   allocate(aux%general_parameter%diffusion_coefficient(option%nphase))
   208)   !geh: there is no point in setting default lquid diffusion coeffcient values 
   209)   !     here as they will be overwritten by the fluid property defaults.
   210)   aux%general_parameter%diffusion_coefficient(LIQUID_PHASE) = &
   211)                                                            UNINITIALIZED_DOUBLE
   212)   aux%general_parameter%diffusion_coefficient(GAS_PHASE) = 2.13d-5
   213)   aux%general_parameter%newton_inf_scaled_res_tol = 1.d-50
   214)   aux%general_parameter%check_post_converged = PETSC_FALSE
   215)   
   216)   GeneralAuxCreate => aux
   217)   
   218) end function GeneralAuxCreate
   219) 
   220) ! ************************************************************************** !
   221) 
   222) subroutine GeneralAuxVarInit(auxvar,allocate_derivative,option)
   223)   ! 
   224)   ! Initialize auxiliary object
   225)   ! 
   226)   ! Author: Glenn Hammond
   227)   ! Date: 03/07/11
   228)   ! 
   229) 
   230)   use Option_module
   231) 
   232)   implicit none
   233)   
   234)   type(general_auxvar_type) :: auxvar
   235)   PetscBool :: allocate_derivative
   236)   type(option_type) :: option
   237) 
   238)   auxvar%istate_store = NULL_STATE
   239)   auxvar%temp = 0.d0
   240)   auxvar%effective_porosity = 0.d0
   241)   auxvar%pert = 0.d0
   242)   
   243)   allocate(auxvar%pres(option%nphase+FOUR_INTEGER))
   244)   auxvar%pres = 0.d0
   245)   allocate(auxvar%sat(option%nphase))
   246)   auxvar%sat = 0.d0
   247)   allocate(auxvar%den(option%nphase))
   248)   auxvar%den = 0.d0
   249)   allocate(auxvar%den_kg(option%nphase))
   250)   auxvar%den_kg = 0.d0
   251)   allocate(auxvar%xmol(option%nflowspec,option%nphase))
   252)   auxvar%xmol = 0.d0
   253)   allocate(auxvar%H(option%nphase))
   254)   auxvar%H = 0.d0
   255)   allocate(auxvar%U(option%nphase))
   256)   auxvar%U = 0.d0
   257)   allocate(auxvar%mobility(option%nphase))
   258)   auxvar%mobility = 0.d0
   259)   if (allocate_derivative) then
   260)     allocate(auxvar%d)
   261)     auxvar%d%por_pl = 0.d0
   262)     auxvar%d%denl_pl = 0.d0
   263)     auxvar%d%denl_T = 0.d0
   264)     auxvar%d%Ul_pl = 0.d0
   265)     auxvar%d%Ul_T = 0.d0
   266)     auxvar%d%Hl_pl = 0.d0
   267)     auxvar%d%Hl_T = 0.d0
   268)     auxvar%d%psat_dT = 0.d0
   269)     auxvar%d%mobilityl_pl = 0.d0
   270)     auxvar%d%mobilityl_T = 0.d0
   271)     auxvar%d%mobilityl_sat = 0.d0
   272)   else
   273)     nullify(auxvar%d)
   274)   endif
   275)   
   276) end subroutine GeneralAuxVarInit
   277) 
   278) ! ************************************************************************** !
   279) 
   280) subroutine GeneralAuxVarCopy(auxvar,auxvar2,option)
   281)   ! 
   282)   ! Copies an auxiliary variable
   283)   ! 
   284)   ! Author: Glenn Hammond
   285)   ! Date: 03/07/11
   286)   ! 
   287) 
   288)   use Option_module
   289) 
   290)   implicit none
   291)   
   292)   type(general_auxvar_type) :: auxvar, auxvar2
   293)   type(option_type) :: option
   294) 
   295)   auxvar2%istate_store = auxvar%istate_store
   296)   auxvar2%pres = auxvar%pres
   297)   auxvar2%temp = auxvar%temp
   298)   auxvar2%sat = auxvar%sat
   299)   auxvar2%den = auxvar%den
   300)   auxvar2%den_kg = auxvar%den_kg
   301)   auxvar2%xmol = auxvar%xmol
   302)   auxvar2%H = auxvar%H
   303)   auxvar2%U = auxvar%U
   304)   auxvar2%mobility = auxvar%mobility
   305)   auxvar2%effective_porosity = auxvar%effective_porosity
   306)   auxvar2%pert = auxvar%pert
   307) 
   308) end subroutine GeneralAuxVarCopy
   309) 
   310) ! ************************************************************************** !
   311) 
   312) subroutine GeneralAuxSetEnergyDOF(energy_keyword,option)
   313)   ! 
   314)   ! Sets the two phase primary dependent variable for energy based on user 
   315)   ! input.
   316)   ! 
   317)   ! Author: Glenn Hammond
   318)   ! Date: 03/25/14
   319)   ! 
   320)   use Option_module
   321)   use String_module
   322) 
   323)   implicit none
   324)   
   325)   character(len=MAXWORDLENGTH) :: energy_keyword
   326)   type(option_type) :: option
   327) 
   328)   call StringToUpper(energy_keyword)
   329)   select case(energy_keyword)
   330)     case('TEMPERATURE')
   331)       general_2ph_energy_dof = GENERAL_TEMPERATURE_INDEX
   332)     case('AIR_PRESSURE')
   333)       general_2ph_energy_dof = GENERAL_AIR_PRESSURE_INDEX
   334)     case default
   335)       option%io_buffer = 'Energy Keyword: ' // trim(energy_keyword) // &
   336)                           ' not recognized in General Mode'    
   337)       call printErrMsg(option)
   338)   end select
   339) 
   340) end subroutine GeneralAuxSetEnergyDOF
   341) 
   342) ! ************************************************************************** !
   343) 
   344) subroutine GeneralAuxVarCompute(x,gen_auxvar,global_auxvar,material_auxvar, &
   345)                                 characteristic_curves,natural_id,option)
   346)   ! 
   347)   ! Computes auxiliary variables for each grid cell
   348)   ! 
   349)   ! Author: Glenn Hammond
   350)   ! Date: 03/07/11
   351)   ! 
   352) 
   353)   use Option_module
   354)   use Global_Aux_module
   355)   use EOS_Water_module
   356)   use EOS_Gas_module
   357)   use Characteristic_Curves_module
   358)   use Material_Aux_class
   359)   use Creep_Closure_module
   360)   use Fracture_module
   361)   
   362)   implicit none
   363) 
   364)   type(option_type) :: option
   365)   class(characteristic_curves_type) :: characteristic_curves
   366)   PetscReal :: x(option%nflowdof)
   367)   type(general_auxvar_type) :: gen_auxvar
   368)   type(global_auxvar_type) :: global_auxvar
   369)   class(material_auxvar_type) :: material_auxvar
   370)   PetscInt :: natural_id
   371) 
   372)   PetscInt :: gid, lid, acid, wid, eid
   373)   PetscReal :: cell_pressure, water_vapor_pressure
   374)   PetscReal :: den_water_vapor, den_kg_water_vapor
   375)   PetscReal :: u_water_vapor, h_water_vapor
   376)   PetscReal :: den_air, h_air, u_air
   377)   PetscReal :: xmol_air_in_gas, xmol_water_in_gas
   378)   PetscReal :: krl, visl, dkrl_dsat, dvis_dp, dvis_dT
   379)   PetscReal :: krg, visg, dkrg_dsat
   380)   PetscReal :: K_H_tilde
   381)   PetscReal :: guess, dummy
   382)   PetscInt :: apid, cpid, vpid, spid
   383)   PetscReal :: NaN
   384)   PetscReal :: creep_closure_time
   385)   PetscReal :: xmass_air_in_gas
   386)   PetscReal :: xmass_air_in_liquid
   387)   PetscReal :: Ugas_J_kg, Hgas_J_kg
   388)   PetscReal :: Uair_J_kg, Hair_J_kg
   389)   PetscReal :: Uvapor_J_kg, Hvapor_J_kg
   390)   PetscReal :: Hg_mixture_fractioned  
   391)   PetscReal :: aux(1)
   392)   PetscReal :: hw, hw_dp, hw_dT
   393)   PetscReal :: dpor_dp
   394)   PetscReal :: one_over_dw
   395)   PetscReal :: tempreal
   396)   character(len=8) :: state_char
   397)   PetscErrorCode :: ierr
   398) 
   399)   ! from init.F90
   400) !  option%nphase = 2
   401) !  option%liquid_phase = 1  ! liquid_pressure
   402) !  option%gas_phase = 2     ! gas_pressure
   403) 
   404) !  option%air_pressure_id = 3
   405) !  option%capillary_pressure_id = 4
   406) !  option%vapor_pressure_id = 5
   407) 
   408) !  option%water_id = 1
   409) !  option%air_id = 2
   410) !  option%energy_id = 3
   411) 
   412)   lid = option%liquid_phase
   413)   gid = option%gas_phase
   414)   apid = option%air_pressure_id
   415)   cpid = option%capillary_pressure_id
   416)   vpid = option%vapor_pressure_id
   417)   spid = option%saturation_pressure_id
   418) 
   419)   acid = option%air_id ! air component id
   420)   wid = option%water_id
   421)   eid = option%energy_id
   422)   
   423) #ifdef DEBUG_GENERAL  
   424)   ! create a NaN
   425)   NaN = 0.d0
   426)   NaN = 1.d0/NaN
   427)   NaN = 0.d0*NaN
   428)   gen_auxvar%H = NaN
   429)   gen_auxvar%U = NaN
   430)   gen_auxvar%pres = NaN
   431)   gen_auxvar%sat = NaN
   432)   gen_auxvar%den = NaN
   433)   gen_auxvar%den_kg = NaN
   434)   gen_auxvar%xmol = NaN
   435)   gen_auxvar%effective_porosity = NaN
   436)   select case(global_auxvar%istate)
   437)     case(LIQUID_STATE)
   438)       state_char = 'L'
   439)     case(GAS_STATE)
   440)       state_char = 'G'
   441)     case(TWO_PHASE_STATE)
   442)       state_char = '2P'
   443)   end select
   444) #else
   445)   !geh: do not initialize gen_auxvar%temp a the previous value is used as the
   446)   !     initial guess for two phase.
   447)   gen_auxvar%H = 0.d0
   448)   gen_auxvar%U = 0.d0
   449)   gen_auxvar%pres = 0.d0
   450)   gen_auxvar%sat = 0.d0
   451)   gen_auxvar%den = 0.d0
   452)   gen_auxvar%den_kg = 0.d0
   453)   gen_auxvar%xmol = 0.d0
   454)   gen_auxvar%effective_porosity = 0.d0
   455) #endif  
   456)   gen_auxvar%mobility = 0.d0
   457) 
   458) #if 0
   459)   if (option%iflag >= GENERAL_UPDATE_FOR_ACCUM) then
   460)     if (option%iflag == GENERAL_UPDATE_FOR_ACCUM) then
   461)       write(*,'(a,i3,3es17.8,a3)') 'before: ', &
   462)         natural_id, x(1:3), trim(state_char)
   463)     else
   464) !      write(*,'(a,i3,3es17.8,a3)') 'before: ', &
   465) !        -1*natural_id, x(1:3), trim(state_char)
   466)     endif
   467)   endif
   468) #endif
   469)   
   470)   select case(global_auxvar%istate)
   471)     case(LIQUID_STATE)
   472)       gen_auxvar%pres(lid) = x(GENERAL_LIQUID_PRESSURE_DOF)
   473)       gen_auxvar%xmol(acid,lid) = x(GENERAL_LIQUID_STATE_X_MOLE_DOF)
   474)       gen_auxvar%temp = x(GENERAL_ENERGY_DOF)
   475) 
   476)       gen_auxvar%xmol(wid,lid) = 1.d0 - gen_auxvar%xmol(acid,lid)
   477)       ! with the gas state, we must calculate the mole fraction of air in 
   478)       ! in the liquid phase, even though the liquid phase does not exist
   479)       ! due to air diffusion between neighboring GAS and LIQUID cells (this
   480)       ! is more of an issue on a boundary condition).  this is not 
   481)       ! necessary for water since we do not calculate water diffusion 
   482)       ! explicitly.  set mole fractions to zero in gas phase.
   483)       gen_auxvar%xmol(:,gid) = 0.d0
   484)       gen_auxvar%sat(lid) = 1.d0
   485)       gen_auxvar%sat(gid) = 0.d0
   486) 
   487)       call EOSWaterSaturationPressure(gen_auxvar%temp, &
   488)                                       gen_auxvar%pres(spid),ierr)
   489)       !geh: Henry_air_xxx returns K_H in units of Pa, but I am not confident
   490)       !     that K_H is truly K_H_tilde (i.e. p_g * K_H).
   491)       call EOSGasHenry(gen_auxvar%temp,gen_auxvar%pres(spid),K_H_tilde)
   492)       gen_auxvar%pres(gid) = max(gen_auxvar%pres(lid),gen_auxvar%pres(spid))
   493)       gen_auxvar%pres(apid) = K_H_tilde*gen_auxvar%xmol(acid,lid)
   494)       ! need vpres for liq -> 2ph check
   495)       
   496)       ! at this point, if the liquid pressure is negative, we have to go to 
   497)       ! two phase or everything blows up:
   498)       if (gen_auxvar%pres(gid) <= 0.d0) then
   499)         write(option%io_buffer,'(''Negative gas pressure at cell '', &
   500)           & i8,'' in GeneralAuxVarCompute(LIQUID_STATE).  Attempting bailout.'')') &
   501)           natural_id
   502) !        call printErrMsgByRank(option)
   503)         call printMsgByRank(option)
   504)         ! set vapor pressure to just under saturation pressure
   505)         gen_auxvar%pres(vpid) = 0.5d0*gen_auxvar%pres(spid)
   506)         ! set gas pressure to vapor pressure + air pressure
   507)         gen_auxvar%pres(gid) = gen_auxvar%pres(vpid) + gen_auxvar%pres(apid)
   508)         ! capillary pressure won't matter here.        
   509)       else
   510)         gen_auxvar%pres(vpid) = gen_auxvar%pres(lid) - gen_auxvar%pres(apid)
   511)       endif
   512)       gen_auxvar%pres(cpid) = 0.d0
   513)       
   514)     case(GAS_STATE)
   515)       gen_auxvar%pres(gid) = x(GENERAL_GAS_PRESSURE_DOF)
   516)       gen_auxvar%pres(apid) = x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF)
   517)       gen_auxvar%temp = x(GENERAL_ENERGY_DOF)
   518) 
   519)       gen_auxvar%sat(lid) = 0.d0
   520)       gen_auxvar%sat(gid) = 1.d0
   521)       gen_auxvar%xmol(acid,gid) = gen_auxvar%pres(apid) / &
   522)                                    gen_auxvar%pres(gid)
   523)       gen_auxvar%xmol(wid,gid) = 1.d0 - gen_auxvar%xmol(acid,gid)
   524)       ! need to set mole fractions in liquid phase in equilibrium with
   525)       ! water saturated with air in order to accommodate air diffusion between
   526)       ! GAS_STATE cell and TWO_PHASE/LIQUID_STATE cells as air should still
   527)       ! diffuse through the liquid phase.
   528)       call EOSWaterSaturationPressure(gen_auxvar%temp, &
   529)                                       gen_auxvar%pres(spid),ierr)
   530)       call EOSGasHenry(gen_auxvar%temp,gen_auxvar%pres(spid),K_H_tilde)
   531)       gen_auxvar%xmol(acid,lid) = gen_auxvar%pres(apid) / K_H_tilde
   532)       ! set water mole fraction to zero as there is no water in liquid phase
   533)       gen_auxvar%xmol(wid,lid) = 0.d0
   534)       
   535)       gen_auxvar%pres(vpid) = gen_auxvar%pres(gid) - gen_auxvar%pres(apid)
   536) 
   537)        
   538)       ! we have to have a liquid pressure to counter a neighboring 
   539)       ! liquid pressure.  Set to gas pressure.
   540) !      gen_auxvar%pres(lid) = gen_auxvar%pres(gid)
   541) !      gen_auxvar%pres(cpid) = 0.d0
   542) 
   543)       call characteristic_curves%saturation_function% &
   544)              CapillaryPressure(gen_auxvar%sat(lid),gen_auxvar%pres(cpid), &
   545)                                option)                             
   546)       gen_auxvar%pres(lid) = gen_auxvar%pres(gid) - &
   547)                              gen_auxvar%pres(cpid)
   548)       
   549)     case(TWO_PHASE_STATE)
   550)       gen_auxvar%pres(gid) = x(GENERAL_GAS_PRESSURE_DOF)
   551)       gen_auxvar%sat(gid) = x(GENERAL_GAS_SATURATION_DOF)
   552)       if (general_2ph_energy_dof == GENERAL_TEMPERATURE_INDEX) then
   553)         gen_auxvar%temp = x(GENERAL_ENERGY_DOF)
   554)         call EOSWaterSaturationPressure(gen_auxvar%temp, &
   555)                                         gen_auxvar%pres(spid),ierr)
   556)         gen_auxvar%pres(vpid) = gen_auxvar%pres(spid)
   557)         gen_auxvar%pres(apid) = gen_auxvar%pres(gid) - gen_auxvar%pres(vpid)
   558)       
   559)       else
   560)         gen_auxvar%pres(apid) = x(GENERAL_ENERGY_DOF)
   561)         gen_auxvar%pres(vpid) = gen_auxvar%pres(gid) - gen_auxvar%pres(apid)
   562)       
   563)         gen_auxvar%pres(spid) = gen_auxvar%pres(vpid)
   564)         guess = gen_auxvar%temp
   565)         call EOSWaterSaturationTemperature(gen_auxvar%temp, &
   566)                                            gen_auxvar%pres(spid),dummy, &
   567)                                            guess,ierr)
   568)       endif
   569) 
   570)       gen_auxvar%sat(lid) = 1.d0 - gen_auxvar%sat(gid)
   571)       
   572)       call characteristic_curves%saturation_function% &
   573)              CapillaryPressure(gen_auxvar%sat(lid),gen_auxvar%pres(cpid), &
   574)                                option)                             
   575) !      gen_auxvar%pres(cpid) = 0.d0
   576)  
   577)       gen_auxvar%pres(lid) = gen_auxvar%pres(gid) - &
   578)                               gen_auxvar%pres(cpid)
   579) 
   580)       call EOSGasHenry(gen_auxvar%temp,gen_auxvar%pres(spid),K_H_tilde)
   581)       gen_auxvar%xmol(acid,lid) = gen_auxvar%pres(apid) / K_H_tilde
   582)       ! immiscible.
   583) !      gen_auxvar%xmol(acid,lid) = 1.d-10
   584)       
   585)       gen_auxvar%xmol(wid,lid) = 1.d0 - gen_auxvar%xmol(acid,lid)
   586)       gen_auxvar%xmol(acid,gid) = gen_auxvar%pres(apid) / &
   587)                                    gen_auxvar%pres(gid)
   588)       gen_auxvar%xmol(wid,gid) = 1.d0 - gen_auxvar%xmol(acid,gid)
   589) 
   590)     case default
   591)       write(option%io_buffer,*) global_auxvar%istate
   592)       option%io_buffer = 'State (' // trim(adjustl(option%io_buffer)) // &
   593)         ') not recognized in GeneralAuxVarCompute.'
   594)       call printErrMsgByRank(option)
   595) 
   596)   end select
   597) 
   598)   cell_pressure = max(gen_auxvar%pres(lid),gen_auxvar%pres(gid), &
   599)                       gen_auxvar%pres(spid))
   600)         
   601)   ! calculate effective porosity as a function of pressure
   602)   if (option%iflag /= GENERAL_UPDATE_FOR_BOUNDARY) then
   603)     dpor_dp = 0.d0
   604)     gen_auxvar%effective_porosity = material_auxvar%porosity_base
   605) #if 0
   606) !geh this code is no longer valid
   607)     if (associated(material_auxvar%fracture) .and. & 
   608)       material_auxvar%fracture%setup) then
   609)       ! The initiating pressure and maximum pressure must be calculated
   610)       ! before fracture function applies - Heeho
   611)       call FractureInitialSetup(material_auxvar,cell_pressure)
   612)     endif
   613)     if (soil_compressibility_index > 0 .and. &
   614)       material_auxvar%setup_reference_pressure) then
   615)       call MaterialReferencePressureSetup(material_auxvar,cell_pressure)
   616)     endif
   617) #endif
   618)     if (associated(creep_closure)) then
   619)       if (creep_closure%imat == material_auxvar%id) then
   620)         ! option%time here is the t time, not t + dt time.
   621)         creep_closure_time = option%time
   622)         if (option%iflag /= GENERAL_UPDATE_FOR_FIXED_ACCUM) then
   623)           creep_closure_time = creep_closure_time + option%flow_dt
   624)         endif
   625)         gen_auxvar%effective_porosity = &
   626)           creep_closure%Evaluate(creep_closure_time,cell_pressure)
   627)       else if (associated(material_auxvar%fracture)) then
   628)           call FracturePoroEvaluate(material_auxvar,cell_pressure, &
   629)                                 gen_auxvar%effective_porosity,dpor_dp)
   630)       else if (soil_compressibility_index > 0) then
   631)           call MaterialCompressSoil(material_auxvar,cell_pressure, &
   632)                                 gen_auxvar%effective_porosity,dpor_dp)
   633)       endif
   634)     else if (associated(material_auxvar%fracture)) then
   635)       call FracturePoroEvaluate(material_auxvar,cell_pressure, &
   636)                                 gen_auxvar%effective_porosity,dpor_dp)
   637)     else if (soil_compressibility_index > 0) then
   638)       call MaterialCompressSoil(material_auxvar,cell_pressure, &
   639)                                 gen_auxvar%effective_porosity,dpor_dp)
   640)     endif
   641)     if (option%iflag /= GENERAL_UPDATE_FOR_DERIVATIVE) then
   642)       material_auxvar%porosity = gen_auxvar%effective_porosity
   643)     endif
   644)   endif
   645)   if (associated(gen_auxvar%d)) then
   646)     gen_auxvar%d%por_pl = dpor_dp
   647)   endif
   648) 
   649)   ! ALWAYS UPDATE THERMODYNAMIC PROPERTIES FOR BOTH PHASES!!!
   650) 
   651)   ! Liquid phase thermodynamic properties
   652)   ! must use cell_pressure as the pressure, not %pres(lid)
   653)   if (.not.option%flow%density_depends_on_salinity) then
   654)     if (associated(gen_auxvar%d)) then
   655)       call EOSWaterDensity(gen_auxvar%temp,cell_pressure, &
   656)                            gen_auxvar%den_kg(lid),gen_auxvar%den(lid), &
   657)                            gen_auxvar%d%denl_pl,gen_auxvar%d%denl_T,ierr)
   658)     else
   659)       call EOSWaterDensity(gen_auxvar%temp,cell_pressure, &
   660)                            gen_auxvar%den_kg(lid),gen_auxvar%den(lid),ierr)
   661)     endif
   662)   else
   663)     aux(1) = global_auxvar%m_nacl(1)
   664)     if (associated(gen_auxvar%d)) then
   665)       call EOSWaterDensityExt(gen_auxvar%temp,celL_pressure,aux, &
   666)                               gen_auxvar%den_kg(lid),gen_auxvar%den(lid),ierr)
   667)     else
   668)       call EOSWaterDensityExt(gen_auxvar%temp,celL_pressure,aux, &
   669)                               gen_auxvar%den_kg(lid),gen_auxvar%den(lid), &
   670)                               gen_auxvar%d%denl_pl,gen_auxvar%d%denl_T,ierr)
   671)     endif
   672)   endif
   673)   if (associated(gen_auxvar%d)) then
   674)     call EOSWaterEnthalpy(gen_auxvar%temp,cell_pressure,hw,hw_dp,hw_dT,ierr)
   675)     one_over_dw = 1.d0/gen_auxvar%den(lid)
   676)     !TODO(geh): merge the common terms in dUl_pl and dUl_T equations
   677)     gen_auxvar%d%Ul_pl = hw_dp - &
   678)                          (one_over_dw - &
   679)                           cell_pressure * one_over_dw * one_over_dw * &
   680)                           gen_auxvar%d%denl_pl)
   681)     gen_auxvar%d%Ul_T = hw_dT - &
   682)                         (one_over_dw - &
   683)                          cell_pressure * one_over_dw * one_over_dw * &
   684)                          gen_auxvar%d%denl_T)
   685)     gen_auxvar%d%Hl_pl = hw_dp * 1.d-6
   686)     gen_auxvar%d%Hl_T = hw_dT * 1.d-6
   687)     gen_auxvar%d%Ul_T = gen_auxvar%d%Ul_T * 1.d-6 ! J/kmol-C -> MJ/kmol-C
   688)     gen_auxvar%d%Ul_pl = gen_auxvar%d%Ul_pl * 1.d-6 ! J/kmol-Pa -> MJ/kmol-Pa
   689)   else
   690)     call EOSWaterEnthalpy(gen_auxvar%temp,cell_pressure,hw,ierr)
   691)   endif
   692)   gen_auxvar%H(lid) = hw * 1.d-6 ! J/kmol -> MJ/kmol
   693)   ! MJ/kmol comp
   694)   gen_auxvar%U(lid) = gen_auxvar%H(lid) - &
   695)                         ! Pa / kmol/m^3 * 1.e-6 = MJ/kmol
   696)                         (cell_pressure / gen_auxvar%den(lid) * &
   697)                         1.d-6)
   698) 
   699)   ! Gas phase thermodynamic properties
   700)   ! we cannot use %pres(vpid) as vapor pressre in the liquid phase, since
   701)   ! it can go negative
   702)   if (global_auxvar%istate /= LIQUID_STATE) then
   703)     if (global_auxvar%istate == GAS_STATE) then
   704)       water_vapor_pressure = gen_auxvar%pres(vpid)
   705)     else
   706)       water_vapor_pressure = gen_auxvar%pres(spid)
   707)     endif
   708)     call EOSGasDensityEnergy(gen_auxvar%temp,gen_auxvar%pres(apid),den_air, &
   709)                              h_air,u_air,ierr)
   710)     h_air = h_air * 1.d-6 ! J/kmol -> MJ/kmol
   711)     u_air = u_air * 1.d-6 ! J/kmol -> MJ/kmol
   712)     call EOSWaterSteamDensityEnthalpy(gen_auxvar%temp,water_vapor_pressure, &
   713)                                       den_kg_water_vapor,den_water_vapor, &
   714)                                       h_water_vapor,ierr)
   715)     u_water_vapor = h_water_vapor - &
   716)                     ! Pa / kmol/m^3 = J/kmol
   717)                     water_vapor_pressure / den_water_vapor
   718)     h_water_vapor = h_water_vapor * 1.d-6 ! J/kmol -> MJ/kmol
   719)     u_water_vapor = u_water_vapor * 1.d-6 ! J/kmol -> MJ/kmol
   720)     gen_auxvar%den(gid) = den_water_vapor + den_air
   721)     gen_auxvar%den_kg(gid) = den_kg_water_vapor + den_air*fmw_comp(gid)
   722)     ! if xmol not set for gas phase, as is the case for LIQUID_STATE, 
   723)     ! set based on densities
   724) !    if (gen_auxvar%xmol(acid,gid) < 1.d-40) then
   725) !      xmol_air_in_gas = den_air / gen_auxvar%den(gid)
   726) !      xmol_water_in_gas = 1.d0 - gen_auxvar%xmol(acid,gid)
   727) !    else
   728)       xmol_air_in_gas = gen_auxvar%xmol(acid,gid)
   729)       xmol_water_in_gas = gen_auxvar%xmol(wid,gid)
   730) !    endif
   731)       
   732) #ifdef DEBUG_GENERAL  
   733)     xmass_air_in_gas = xmol_air_in_gas*fmw_comp(gid) / &
   734)                        (xmol_water_in_gas*FMWH2O + &
   735)                         xmol_air_in_gas*fmw_comp(gid))
   736)     Hair_J_kg = h_air*1.d6/fmw_comp(gid)
   737)     Uair_J_kg = u_air*1.d6/fmw_comp(gid)
   738)     Hvapor_J_kg = h_water_vapor*1.d6/FMWH2O
   739)     Uvapor_J_kg = u_water_vapor*1.d6/FMWH2O
   740)     Ugas_J_kg = xmass_air_in_gas*Uair_J_kg + &
   741)                 (1.d0-xmass_air_in_gas)*Uvapor_J_kg
   742)     Hgas_J_kg = Ugas_J_kg + &
   743)                 gen_auxvar%pres(gid)/gen_auxvar%den_kg(gid)
   744) #endif
   745)     
   746)     ! MJ/kmol
   747)     gen_auxvar%U(gid) = xmol_water_in_gas * u_water_vapor + &
   748)                         xmol_air_in_gas * u_air
   749)     Hg_mixture_fractioned = xmol_water_in_gas*h_water_vapor + &
   750)                             xmol_air_in_gas*h_air
   751)     gen_auxvar%H(gid) = gen_auxvar%U(gid) + &
   752)                         ! Pa / kmol/m^3 * 1.e-6 = MJ/kmol
   753)                         gen_auxvar%pres(gid)/gen_auxvar%den(gid) * 1.d-6
   754)   endif ! istate /= LIQUID_STATE
   755)   
   756)   if (global_auxvar%istate == LIQUID_STATE .or. &
   757)       global_auxvar%istate == TWO_PHASE_STATE) then
   758)     ! this does not need to be calculated for LIQUID_STATE (=1)
   759)     call characteristic_curves%liq_rel_perm_function% &
   760)            RelativePermeability(gen_auxvar%sat(lid),krl,dkrl_dsat,option)
   761)     ! dkrl_sat is with respect to liquid pressure, but the primary dependent
   762)     ! variable is gas pressure.  therefore, negate
   763)     dkrl_dsat = -1.d0 * dkrl_dsat
   764)     ! use cell_pressure; cell_pressure - psat calculated internally
   765)     if (.not.option%flow%density_depends_on_salinity) then
   766)       if (associated(gen_auxvar%d)) then
   767)         call EOSWaterViscosity(gen_auxvar%temp,cell_pressure, &
   768)                                gen_auxvar%pres(spid), &
   769)                                gen_auxvar%d%psat_dT,visl, &
   770)                                dvis_dT,dvis_dp,ierr)
   771)       else
   772)         call EOSWaterViscosity(gen_auxvar%temp,cell_pressure, &
   773)                                gen_auxvar%pres(spid),visl,ierr)
   774)       endif
   775)     else
   776)       aux(1) = global_auxvar%m_nacl(1)
   777)       if (associated(gen_auxvar%d)) then
   778)         call EOSWaterViscosityExt(gen_auxvar%temp,cell_pressure, &
   779)                                   gen_auxvar%pres(spid), &
   780)                                   gen_auxvar%d%psat_dT,aux,visl, &
   781)                                   dvis_dT,dvis_dp,ierr)
   782)       else
   783)         call EOSWaterViscosityExt(gen_auxvar%temp,cell_pressure, &
   784)                                   gen_auxvar%pres(spid),aux,visl,ierr)
   785)       endif
   786)     endif
   787)     gen_auxvar%mobility(lid) = krl/visl
   788)     if (associated(gen_auxvar%d)) then
   789)       ! use chainrule for derivative
   790)       tempreal = -1.d0*krl/(visl*visl)
   791)       gen_auxvar%d%mobilityl_pl = tempreal*dvis_dp
   792)       gen_auxvar%d%mobilityl_T = tempreal*dvis_dT
   793)       gen_auxvar%d%mobilityl_sat = dkrl_dsat/visl
   794)     endif
   795)   endif
   796) 
   797)   if (global_auxvar%istate == GAS_STATE .or. &
   798)       global_auxvar%istate == TWO_PHASE_STATE) then
   799)     ! this does not need to be calculated for GAS_STATE (=1)
   800)     call characteristic_curves%gas_rel_perm_function% &
   801)            RelativePermeability(gen_auxvar%sat(lid),krg,dkrg_dsat,option)                            
   802)     ! dkrl_sat is with respect to liquid pressure, but the primary dependent
   803)     ! variable is gas pressure.  therefore, negate
   804)     dkrg_dsat = -1.d0 * dkrg_dsat
   805)     ! STOMP uses separate functions for calculating viscosity of vapor and
   806)     ! and air (WATGSV,AIRGSV) and then uses GASVIS to calculate mixture 
   807)     ! viscosity.
   808)     if (associated(gen_auxvar%d)) then
   809)       option%io_buffer = 'neet to set up gas viscosity derivative in general'
   810)       call printErrMsg(option)
   811)     else    
   812)       call EOSGasViscosity(gen_auxvar%temp,gen_auxvar%pres(apid), &
   813)                            gen_auxvar%pres(gid),den_air,visg,ierr)
   814)     endif
   815)     gen_auxvar%mobility(gid) = krg/visg
   816)     if (associated(gen_auxvar%d)) then
   817)       ! use chainrule for derivative
   818) !      gen_auxvar%d%mobilityg_pg = krg/(visg*visg)*dvisg_dp
   819) !      gen_auxvar%d%mobilityg_satg = dkrg_dsat/visg
   820)     endif    
   821)   endif
   822) 
   823) #if 0
   824)   if (option%iflag == GENERAL_UPDATE_FOR_ACCUM) then
   825)     write(*,'(a,i3,2f13.4,es13.6,f13.4,a3)') 'i/l/g/x[a/l]/t: ', &
   826)       natural_id, gen_auxvar%pres(1:2), gen_auxvar%xmol(acid,lid), &
   827)       gen_auxvar%temp, trim(state_char)
   828)     if (natural_id == 5) then
   829) #if 0
   830)     write(*,'(a,i3,8f13.4,a3)') 'i/l/g/a/c/v/s/t: ', &
   831)       natural_id, gen_auxvar%pres(1:6), gen_auxvar%sat(1), gen_auxvar%temp, &
   832)       trim(state_char)
   833) #endif
   834) #if 0
   835)     if (gen_auxvar%sat(2) > 0.d0) then
   836)       write(*,'(a,7es13.6)') 'kmol/kmol/kmol/MJ/MJ/MJ: ', &
   837)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(1,1) + &
   838)         gen_auxvar%den(2)*gen_auxvar%sat(2)*gen_auxvar%xmol(1,2),  &
   839)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(2,1) + &
   840)         gen_auxvar%den(2)*gen_auxvar%sat(2)*gen_auxvar%xmol(2,2),  &
   841)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(1,1) + &
   842)         gen_auxvar%den(2)*gen_auxvar%sat(2)*gen_auxvar%xmol(1,2) + &
   843)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(2,1) + &
   844)         gen_auxvar%den(2)*gen_auxvar%sat(2)*gen_auxvar%xmol(2,2),  &
   845)         gen_auxvar%sat(1)*gen_auxvar%den(1)*gen_auxvar%U(1),  &
   846)         gen_auxvar%sat(2)*gen_auxvar%den(2)*gen_auxvar%U(2),  &
   847)         gen_auxvar%sat(1)*gen_auxvar%den(1)*gen_auxvar%U(1) + &
   848)         gen_auxvar%sat(2)*gen_auxvar%den(2)*gen_auxvar%U(2)
   849)     else
   850)       write(*,'(a,7es13.6)') 'kmol/kmol/kmol/MJ/MJ/MJ: ', &
   851)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(1,1), &
   852)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(2,1), &
   853)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(1,1) + &
   854)         gen_auxvar%den(1)*gen_auxvar%sat(1)*gen_auxvar%xmol(2,1), &
   855)         gen_auxvar%sat(1)*gen_auxvar%den(1)*gen_auxvar%U(1), 0.d0, &
   856)         gen_auxvar%sat(1)*gen_auxvar%den(1)*gen_auxvar%U(1)
   857)     endif
   858) #endif
   859)     endif
   860)   endif
   861) #endif
   862) 
   863) end subroutine GeneralAuxVarCompute
   864) 
   865) ! ************************************************************************** !
   866) 
   867) subroutine GeneralAuxVarUpdateState(x,gen_auxvar,global_auxvar, &
   868)                                     material_auxvar, &
   869)                                     characteristic_curves,natural_id, &
   870)                                     option)
   871)   ! 
   872)   ! GeneralUpdateState: Updates the state and swaps primary variables
   873)   ! 
   874)   ! Author: Glenn Hammond
   875)   ! Date: 05/25/11
   876)   ! 
   877) 
   878)   use Option_module
   879)   use Global_Aux_module
   880)   use EOS_Water_module
   881) !  use Gas_EOS_module
   882)   use Characteristic_Curves_module
   883)   use Material_Aux_class
   884)   
   885)   implicit none
   886) 
   887)   type(option_type) :: option
   888)   PetscInt :: natural_id
   889)   class(characteristic_curves_type) :: characteristic_curves
   890)   type(general_auxvar_type) :: gen_auxvar
   891)   type(global_auxvar_type) :: global_auxvar
   892)   class(material_auxvar_type) :: material_auxvar
   893) 
   894) ! based on min_pressure in CheckPre set to zero
   895) !  PetscReal, parameter :: epsilon = 1.d-6
   896)   PetscReal, parameter :: epsilon = 0.d0
   897)   PetscReal :: liquid_epsilon
   898)   PetscReal :: two_phase_epsilon
   899)   PetscReal :: x(option%nflowdof)
   900)   PetscInt :: apid, cpid, vpid, spid
   901)   PetscInt :: gid, lid, acid, wid, eid
   902)   PetscReal :: dummy, guess
   903)   PetscReal :: n_air, n_air_in_gas, n_air_in_liquid, RT, K_H_tilde, theta
   904)   PetscBool :: flag
   905)   character(len=MAXSTRINGLENGTH) :: state_change_string, string
   906)   PetscErrorCode :: ierr
   907) 
   908)   lid = option%liquid_phase
   909)   gid = option%gas_phase
   910)   apid = option%air_pressure_id
   911)   cpid = option%capillary_pressure_id
   912)   vpid = option%vapor_pressure_id
   913)   spid = option%saturation_pressure_id
   914) 
   915)   acid = option%air_id ! air component id
   916)   wid = option%water_id
   917)   eid = option%energy_id
   918) 
   919)   flag = PETSC_FALSE
   920)   
   921)   gen_auxvar%istate_store(PREV_IT) = global_auxvar%istate
   922)   select case(global_auxvar%istate)
   923)     case(LIQUID_STATE)
   924)       ! scaling by window_epsilon forces vapor pressure to enter two phase
   925)       ! region a finite amount before phase change can occur
   926)       if (gen_auxvar%pres(vpid) <= &
   927)           gen_auxvar%pres(spid)*(1.d0-window_epsilon)) then
   928) !#ifdef DEBUG_GENERAL
   929) #ifdef DEBUG_GENERAL_INFO
   930)         call GeneralPrintAuxVars(gen_auxvar,global_auxvar,material_auxvar, &
   931)                                  natural_id,'Before Update',option)
   932) #endif
   933)         if (option%iflag == GENERAL_UPDATE_FOR_ACCUM) then
   934)           write(state_change_string,'(''Liquid -> 2 Phase at Cell '',i8)') &
   935)             natural_id
   936)         else if (option%iflag == GENERAL_UPDATE_FOR_DERIVATIVE) then
   937)           write(state_change_string, &
   938)             '(''Liquid -> 2 Phase at Cell (due to perturbation) '',i8)') &
   939)             natural_id
   940)         else
   941)           write(state_change_string,'(''Liquid -> 2 Phase at Boundary Face '', &
   942)                                     & i8)') natural_id
   943)         endif
   944) !#endif      
   945)         global_auxvar%istate = TWO_PHASE_STATE
   946)         liquid_epsilon = epsilon
   947) !TOUGH2 approach
   948)         ! geh: just testing something here
   949)         x(GENERAL_GAS_PRESSURE_DOF) = max(gen_auxvar%pres(gid), &
   950)                                           gen_auxvar%pres(spid))* &
   951)                                          (1.d0 + liquid_epsilon)
   952) !        x(GENERAL_GAS_PRESSURE_DOF) = max(gen_auxvar%pres(gid), &
   953) !                                          gen_auxvar%pres(spid)+ &
   954) !                                          gen_auxvar%pres(apid))* &
   955) !                                         (1.d0 + liquid_epsilon)
   956)         if (general_2ph_energy_dof == GENERAL_TEMPERATURE_INDEX) then
   957)           ! do nothing as the energy dof has not changed
   958)           if (.not.general_isothermal) then
   959)             X(GENERAL_ENERGY_DOF) = X(GENERAL_ENERGY_DOF) * &
   960)                                    (1.d0 + liquid_epsilon)
   961)           endif
   962)         else
   963)           ! pa = pg - ps
   964)           x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) = &
   965)             x(GENERAL_GAS_PRESSURE_DOF) - gen_auxvar%pres(spid)
   966)           if (x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) <= 0.d0) then
   967)             write(string,*) natural_id
   968)             option%io_buffer = 'Negative air pressure during state change ' // &
   969)               'at ' // trim(adjustl(string))
   970)   !          call printErrMsg(option)
   971)             call printMsgByRank(option)
   972)             x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) = &
   973)               0.01d0*x(GENERAL_GAS_PRESSURE_DOF)
   974)           endif
   975)         endif
   976)         x(GENERAL_GAS_SATURATION_DOF) = 1.d-6
   977) !        x(GENERAL_GAS_SATURATION_DOF) = liquid_epsilon
   978)         flag = PETSC_TRUE
   979)       endif
   980)     case(GAS_STATE)
   981)       ! scaling by window_epsilon forces vapor pressure to enter two phase
   982)       ! region a finite amount before phase change can occur
   983)       if (gen_auxvar%pres(vpid) >= &
   984)           gen_auxvar%pres(spid)*(1.d0+window_epsilon)) then
   985) !#ifdef DEBUG_GENERAL
   986) #ifdef DEBUG_GENERAL_INFO
   987)         call GeneralPrintAuxVars(gen_auxvar,global_auxvar,material_auxvar, &
   988)                                  natural_id,'Before Update',option)
   989) #endif
   990)         if (option%iflag == GENERAL_UPDATE_FOR_ACCUM) then
   991)           write(state_change_string,'(''Gas -> 2 Phase at Cell '',i8)') &
   992)             natural_id
   993)         else if (option%iflag == GENERAL_UPDATE_FOR_DERIVATIVE) then
   994)           write(state_change_string, &
   995)             '(''Gas -> 2 Phase at Cell (due to perturbation) '',i8)') &
   996)             natural_id
   997)         else
   998)           write(state_change_string,'(''Gas -> 2 Phase at Boundary Face '', &
   999)                                     & i8)') natural_id
  1000)         endif
  1001) !#endif      
  1002)         global_auxvar%istate = TWO_PHASE_STATE
  1003)         ! first two primary dependent variables do not change
  1004)         if (general_2ph_energy_dof == GENERAL_TEMPERATURE_INDEX) then
  1005)           if (.not.general_isothermal) then
  1006)             X(GENERAL_ENERGY_DOF) = X(GENERAL_ENERGY_DOF) * &
  1007)                                     (1.d0 - epsilon)
  1008)           endif
  1009)         else
  1010)           X(GENERAL_ENERGY_DOF) = gen_auxvar%pres(apid)*(1.d0-epsilon)
  1011)         endif
  1012) !        x(GENERAL_GAS_SATURATION_DOF) = 1.d0 - epsilon
  1013)         x(GENERAL_GAS_SATURATION_DOF) = 1.d0 - 1.d-6
  1014)         flag = PETSC_TRUE
  1015)       endif
  1016)     case(TWO_PHASE_STATE)
  1017)       if (gen_auxvar%sat(gid) < 0.d0) then
  1018) !#ifdef DEBUG_GENERAL
  1019) #ifdef DEBUG_GENERAL_INFO
  1020)         call GeneralPrintAuxVars(gen_auxvar,global_auxvar,material_auxvar, &
  1021)                                  natural_id,'Before Update',option)
  1022) #endif
  1023)         if (option%iflag == GENERAL_UPDATE_FOR_ACCUM) then
  1024)           write(state_change_string,'(''2 Phase -> Liquid at Cell '',i8)') &
  1025)             natural_id
  1026)         else if (option%iflag == GENERAL_UPDATE_FOR_DERIVATIVE) then
  1027)           write(state_change_string, &
  1028)             '(''2 Phase -> Liquid at Cell (due to perturbation) '',i8)') &
  1029)             natural_id        
  1030)         else
  1031)           write(state_change_string,'(''2 Phase -> Liquid at Boundary Face '', &
  1032)                                     & i8)') natural_id
  1033)         endif
  1034) !#endif      
  1035)         two_phase_epsilon = epsilon
  1036)         ! convert to liquid state
  1037)         global_auxvar%istate = LIQUID_STATE
  1038)         x(GENERAL_LIQUID_PRESSURE_DOF) = &
  1039)           gen_auxvar%pres(gid) * (1.d0 + two_phase_epsilon) ! 4.94500E+01, 8800 NI, 10 cuts
  1040) !          gen_auxvar%pres(gid) ! 4.95500E+01, 9119 NI, 7 cuts
  1041)         x(GENERAL_LIQUID_STATE_X_MOLE_DOF) = &
  1042)           gen_auxvar%xmol(acid,lid) * (1.d0 - two_phase_epsilon) ! 4.94500E+01, 8800 NI, 10 cuts
  1043) !          gen_auxvar%xmol(acid,lid) ! 4.95298E+01, 10355 NI, 6 cuts
  1044)         if (x(GENERAL_LIQUID_STATE_X_MOLE_DOF) <= 0.d0) then
  1045)           write(string,*) natural_id
  1046)           option%io_buffer = 'Negative air mole fraction during state change ' // &
  1047)             'at ' // trim(adjustl(string))
  1048)           call printMsgByRank(option)
  1049)           x(GENERAL_LIQUID_STATE_X_MOLE_DOF) = two_phase_epsilon
  1050)         endif
  1051)         if (general_2ph_energy_dof == GENERAL_TEMPERATURE_INDEX) then
  1052)           if (.not.general_isothermal) then
  1053)             X(GENERAL_ENERGY_DOF) = X(GENERAL_ENERGY_DOF) * &
  1054)                                     (1.d0-two_phase_epsilon)
  1055)           endif
  1056)         else
  1057)           X(GENERAL_ENERGY_DOF) = gen_auxvar%temp*(1.d0-two_phase_epsilon)
  1058)         endif
  1059)         flag = PETSC_TRUE
  1060)       else if (gen_auxvar%sat(gid) > 1.d0) then
  1061) !#ifdef DEBUG_GENERAL
  1062) #ifdef DEBUG_GENERAL_INFO
  1063)         call GeneralPrintAuxVars(gen_auxvar,global_auxvar,material_auxvar, &
  1064)                                  natural_id,'Before Update',option)
  1065) #endif
  1066)         if (option%iflag == GENERAL_UPDATE_FOR_ACCUM) then
  1067)           write(state_change_string,'(''2 Phase -> Gas at Cell '',i8)') &
  1068)             natural_id
  1069)         else if (option%iflag == GENERAL_UPDATE_FOR_DERIVATIVE) then
  1070)           write(state_change_string, &
  1071)             '(''2 Phase -> Gas at Cell (due to perturbation) '',i8)') &
  1072)             natural_id       
  1073)         else
  1074)           write(state_change_string,'(''2 Phase -> Gas at Boundary Face '', &
  1075)                                     & i8)') natural_id
  1076)         endif
  1077) !#endif      
  1078)         two_phase_epsilon = epsilon !
  1079)         ! convert to gas state
  1080)         global_auxvar%istate = GAS_STATE
  1081)         if (general_2ph_energy_dof == GENERAL_TEMPERATURE_INDEX) then
  1082)           ! first two primary dependent variables do not change
  1083)           x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) = &
  1084)             gen_auxvar%pres(apid) * (1.d0 + two_phase_epsilon)
  1085)           if (.not.general_isothermal) then
  1086)             X(GENERAL_ENERGY_DOF) = X(GENERAL_ENERGY_DOF) * &
  1087)                                     (1.d0+two_phase_epsilon)
  1088)           endif
  1089)         else
  1090)           X(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) = gen_auxvar%pres(apid)* &
  1091)                                                   (1.d0+two_phase_epsilon)
  1092)           X(GENERAL_ENERGY_DOF) = gen_auxvar%temp*(1.d0+two_phase_epsilon)
  1093)         endif
  1094)         flag = PETSC_TRUE
  1095)       endif
  1096)   end select
  1097)   
  1098)   if (flag) then
  1099)     call GeneralAuxVarCompute(x,gen_auxvar, global_auxvar,material_auxvar, &
  1100)                               characteristic_curves,natural_id,option)
  1101) !#ifdef DEBUG_GENERAL
  1102)     state_change_string = 'State Transition: ' // trim(state_change_string)
  1103)     call printMsgByRank(option,state_change_string)
  1104) #ifdef DEBUG_GENERAL_INFO
  1105)     call GeneralPrintAuxVars(gen_auxvar,global_auxvar,material_auxvar, &
  1106)                              natural_id,'After Update',option)
  1107) #endif
  1108) !#endif
  1109)   endif
  1110) 
  1111) end subroutine GeneralAuxVarUpdateState
  1112) 
  1113) ! ************************************************************************** !
  1114) 
  1115) subroutine GeneralAuxVarPerturb(gen_auxvar,global_auxvar, &
  1116)                                 material_auxvar, &
  1117)                                 characteristic_curves,natural_id, &
  1118)                                 option)
  1119)   ! 
  1120)   ! Calculates auxiliary variables for perturbed system
  1121)   ! 
  1122)   ! Author: Glenn Hammond
  1123)   ! Date: 03/09/11
  1124)   ! 
  1125) 
  1126)   use Option_module
  1127)   use Characteristic_Curves_module
  1128)   use Global_Aux_module
  1129)   use Material_Aux_class
  1130) 
  1131)   implicit none
  1132) 
  1133)   type(option_type) :: option
  1134)   PetscInt :: natural_id
  1135)   type(general_auxvar_type) :: gen_auxvar(0:)
  1136)   type(global_auxvar_type) :: global_auxvar
  1137)   class(material_auxvar_type) :: material_auxvar
  1138)   class(characteristic_curves_type) :: characteristic_curves
  1139)      
  1140)   PetscReal :: x(option%nflowdof), x_pert(option%nflowdof), &
  1141)                pert(option%nflowdof), x_pert_save(option%nflowdof)
  1142) 
  1143)   PetscReal :: res(option%nflowdof), res_pert(option%nflowdof)
  1144)   PetscReal :: tempreal
  1145) !#define LEGACY_PERTURBATION
  1146) #ifdef LEGACY_PERTURBATION
  1147)   PetscReal, parameter :: perturbation_tolerance = 1.d-5
  1148) #else
  1149)   PetscReal, parameter :: perturbation_tolerance = 1.d-8
  1150) ! 1.d-11 works well for Emily's 1D nacl2
  1151) !  PetscReal, parameter :: perturbation_tolerance = 1.d-11
  1152) #endif
  1153)   PetscReal, parameter :: min_mole_fraction_pert = 1.d-12
  1154)   PetscReal, parameter :: min_perturbation = 1.d-10
  1155)   PetscInt :: idof
  1156) 
  1157) #ifdef DEBUG_GENERAL
  1158)   character(len=MAXWORDLENGTH) :: word
  1159)   type(global_auxvar_type) :: global_auxvar_debug
  1160)   type(general_auxvar_type) :: general_auxvar_debug
  1161)   call GlobalAuxVarInit(global_auxvar_debug,option)
  1162)   call GeneralAuxVarInit(general_auxvar_debug,PETSC_FALSE,option)
  1163) #endif
  1164) 
  1165)   select case(global_auxvar%istate)
  1166)     case(LIQUID_STATE)
  1167)        x(GENERAL_LIQUID_PRESSURE_DOF) = &
  1168)          gen_auxvar(ZERO_INTEGER)%pres(option%liquid_phase)
  1169)        x(GENERAL_LIQUID_STATE_X_MOLE_DOF) = &
  1170)          gen_auxvar(ZERO_INTEGER)%xmol(option%air_id,option%liquid_phase)
  1171)        x(GENERAL_ENERGY_DOF) = &
  1172)          gen_auxvar(ZERO_INTEGER)%temp
  1173) #ifdef LEGACY_PERTURBATION
  1174)        ! if the liquid state, the liquid pressure will always be greater
  1175)        ! than zero.
  1176)        pert(GENERAL_LIQUID_PRESSURE_DOF) = &
  1177)          max(perturbation_tolerance*x(GENERAL_LIQUID_PRESSURE_DOF), &
  1178)              perturbation_tolerance)
  1179)        ! if the air mole fraction perturbation is too small, the derivatives
  1180)        ! can be poor.
  1181)        pert(GENERAL_LIQUID_STATE_X_MOLE_DOF) = &
  1182)          -1.d0*max(perturbation_tolerance*x(GENERAL_LIQUID_STATE_X_MOLE_DOF), &
  1183)                    min_mole_fraction_pert)
  1184)        pert(GENERAL_ENERGY_DOF) = &
  1185)          -1.d0*perturbation_tolerance*x(GENERAL_ENERGY_DOF)
  1186) #else
  1187)        pert(GENERAL_LIQUID_PRESSURE_DOF) = &
  1188)          perturbation_tolerance*x(GENERAL_LIQUID_PRESSURE_DOF) + &
  1189)          min_perturbation
  1190)        if (x(GENERAL_LIQUID_STATE_X_MOLE_DOF) > &
  1191)            1.d3 * perturbation_tolerance) then
  1192)          pert(GENERAL_LIQUID_STATE_X_MOLE_DOF) = -1.d0 * perturbation_tolerance
  1193)        else
  1194)          pert(GENERAL_LIQUID_STATE_X_MOLE_DOF) = perturbation_tolerance
  1195)        endif
  1196)        pert(GENERAL_ENERGY_DOF) = -1.d0 * &
  1197)          (perturbation_tolerance*x(GENERAL_ENERGY_DOF) + min_perturbation)
  1198) #endif
  1199)     case(GAS_STATE)
  1200)        x(GENERAL_GAS_PRESSURE_DOF) = &
  1201)          gen_auxvar(ZERO_INTEGER)%pres(option%gas_phase)
  1202)        x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) = &
  1203)          gen_auxvar(ZERO_INTEGER)%pres(option%air_pressure_id)
  1204)        x(GENERAL_ENERGY_DOF) = gen_auxvar(ZERO_INTEGER)%temp
  1205) #ifdef LEGACY_PERTURBATION
  1206)        ! gas pressure [p(g)] must always be perturbed down as p(v) = p(g) - p(a)
  1207)        ! and p(v) >= Psat (i.e. an increase in p(v)) results in two phase.
  1208)        pert(GENERAL_GAS_PRESSURE_DOF) = &
  1209)          -1.d0*perturbation_tolerance*x(GENERAL_GAS_PRESSURE_DOF)
  1210)        ! perturb air pressure towards gas pressure unless the perturbed
  1211)        ! air pressure exceeds the gas pressure
  1212)        if (x(GENERAL_GAS_PRESSURE_DOF) - &
  1213)            x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) > &
  1214)            perturbation_tolerance* &
  1215)            x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF)) then 
  1216)          pert(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) = &
  1217)            perturbation_tolerance*x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF)
  1218)        else
  1219)          pert(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) = &
  1220)            -1.d0*perturbation_tolerance*x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF)
  1221)        endif
  1222)        pert(GENERAL_ENERGY_DOF) = &
  1223)          perturbation_tolerance*x(GENERAL_ENERGY_DOF)
  1224) #else
  1225)        ! gas pressure [p(g)] must always be perturbed down as p(v) = p(g) - p(a)
  1226)        ! and p(v) >= Psat (i.e. an increase in p(v)) results in two phase.
  1227)        pert(GENERAL_GAS_PRESSURE_DOF) = -1.d0 * &
  1228)          (perturbation_tolerance*x(GENERAL_GAS_PRESSURE_DOF) + min_perturbation)
  1229)        ! perturb air pressure towards gas pressure unless the perturbed
  1230)        ! air pressure exceeds the gas pressure
  1231)        tempreal = perturbation_tolerance* &
  1232)                   x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) + min_perturbation
  1233)        if (x(GENERAL_GAS_PRESSURE_DOF) - &
  1234)            x(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) > tempreal) then
  1235)          pert(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) = tempreal
  1236)        else
  1237)          pert(GENERAL_GAS_STATE_AIR_PRESSURE_DOF) = -1.d0 * tempreal
  1238)        endif
  1239)        pert(GENERAL_ENERGY_DOF) = &
  1240)          perturbation_tolerance*x(GENERAL_ENERGY_DOF) + min_perturbation
  1241) #endif
  1242)     case(TWO_PHASE_STATE)
  1243)        x(GENERAL_GAS_PRESSURE_DOF) = &
  1244)          gen_auxvar(ZERO_INTEGER)%pres(option%gas_phase)
  1245) !       x(GENERAL_AIR_PRESSURE_DOF) = &
  1246) !         gen_auxvar(ZERO_INTEGER)%pres(option%air_pressure_id)
  1247)        x(GENERAL_GAS_SATURATION_DOF) = &
  1248)          gen_auxvar(ZERO_INTEGER)%sat(option%gas_phase)
  1249) #ifdef LEGACY_PERTURBATION
  1250)        if (general_2ph_energy_dof == GENERAL_TEMPERATURE_INDEX) then
  1251)          x(GENERAL_ENERGY_DOF) = &
  1252)            gen_auxvar(ZERO_INTEGER)%temp
  1253)          pert(GENERAL_ENERGY_DOF) = &
  1254)            perturbation_tolerance*x(GENERAL_ENERGY_DOF)
  1255)        else
  1256)          ! here GENERAL_2PH_STATE_AIR_PRESSURE_DOF = GENERAL_ENERGY_DOF
  1257)          x(GENERAL_ENERGY_DOF) = &
  1258)            gen_auxvar(ZERO_INTEGER)%pres(option%air_pressure_id)
  1259)          ! perturb air pressure towards gas pressure unless the perturbed
  1260)          ! air pressure exceeds the gas pressure
  1261)          if (x(GENERAL_GAS_PRESSURE_DOF) - &
  1262)              x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) > &
  1263)              perturbation_tolerance*x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF)) then
  1264)            pert(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) = &
  1265)              perturbation_tolerance*x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF)
  1266)          else
  1267)            pert(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) = &
  1268)              -1.d0*perturbation_tolerance*x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF)
  1269)          endif
  1270)        endif
  1271)        pert(GENERAL_GAS_PRESSURE_DOF) = &
  1272)          perturbation_tolerance*x(GENERAL_GAS_PRESSURE_DOF)
  1273)        ! always perturb toward 0.5
  1274)        if (x(GENERAL_GAS_SATURATION_DOF) > 0.5d0) then 
  1275)          pert(GENERAL_GAS_SATURATION_DOF) = &
  1276)            -1.d0*perturbation_tolerance*x(GENERAL_GAS_SATURATION_DOF)
  1277)        else
  1278)          pert(GENERAL_GAS_SATURATION_DOF) = &
  1279)            perturbation_tolerance*x(GENERAL_GAS_SATURATION_DOF)
  1280)        endif
  1281) #else
  1282)        if (general_2ph_energy_dof == GENERAL_TEMPERATURE_INDEX) then
  1283)          x(GENERAL_ENERGY_DOF) = &
  1284)            gen_auxvar(ZERO_INTEGER)%temp
  1285)          pert(GENERAL_ENERGY_DOF) = &
  1286)            perturbation_tolerance*x(GENERAL_ENERGY_DOF)+min_perturbation
  1287)        else
  1288)          ! here GENERAL_2PH_STATE_AIR_PRESSURE_DOF = GENERAL_ENERGY_DOF
  1289)          x(GENERAL_ENERGY_DOF) = &
  1290)            gen_auxvar(ZERO_INTEGER)%pres(option%air_pressure_id)
  1291)          ! perturb air pressure towards gas pressure unless the perturbed
  1292)          ! air pressure exceeds the gas pressure
  1293)          tempreal = perturbation_tolerance* &
  1294)                     x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) + min_perturbation
  1295)          if (x(GENERAL_GAS_PRESSURE_DOF) - &
  1296)              x(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) > tempreal) then
  1297)            pert(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) = tempreal
  1298)          else
  1299)            pert(GENERAL_2PH_STATE_AIR_PRESSURE_DOF) = -1.d0 * tempreal
  1300)          endif
  1301)        endif
  1302)        pert(GENERAL_GAS_PRESSURE_DOF) = &
  1303)          perturbation_tolerance*x(GENERAL_GAS_PRESSURE_DOF)+min_perturbation
  1304)        if (x(GENERAL_GAS_SATURATION_DOF) > 0.5d0) then 
  1305)          pert(GENERAL_GAS_SATURATION_DOF) = -1.d0 * perturbation_tolerance
  1306)        else
  1307)          pert(GENERAL_GAS_SATURATION_DOF) = perturbation_tolerance
  1308)        endif
  1309) #endif
  1310)   end select
  1311)   
  1312)   ! GENERAL_UPDATE_FOR_DERIVATIVE indicates call from perturbation
  1313)   option%iflag = GENERAL_UPDATE_FOR_DERIVATIVE
  1314)   do idof = 1, option%nflowdof
  1315)     gen_auxvar(idof)%pert = pert(idof)
  1316)     x_pert = x
  1317)     x_pert(idof) = x(idof) + pert(idof)
  1318)     x_pert_save = x_pert
  1319)     call GeneralAuxVarCompute(x_pert,gen_auxvar(idof),global_auxvar, &
  1320)                               material_auxvar, &
  1321)                               characteristic_curves,natural_id,option)
  1322) #ifdef DEBUG_GENERAL
  1323)     call GlobalAuxVarCopy(global_auxvar,global_auxvar_debug,option)
  1324)     call GeneralAuxVarCopy(gen_auxvar(idof),general_auxvar_debug,option)
  1325)     call GeneralAuxVarUpdateState(x_pert,general_auxvar_debug, &
  1326)                                   global_auxvar_debug, &
  1327)                                   material_auxvar, &
  1328)                                   characteristic_curves, &
  1329)                                   natural_id,option)
  1330)     if (global_auxvar%istate /= global_auxvar_debug%istate) then
  1331)       write(option%io_buffer, &
  1332)             &'(''Change in state due to perturbation: '',i3,'' -> '',i3, &
  1333)             &'' at cell '',i3,'' for dof '',i3)') &
  1334)         global_auxvar%istate, global_auxvar_debug%istate, natural_id, idof
  1335)       call printMsg(option)
  1336)       write(option%io_buffer,'(''orig: '',6es17.8)') x(1:3)
  1337)       call printMsg(option)
  1338)       write(option%io_buffer,'(''pert: '',6es17.8)') x_pert_save(1:3)
  1339)       call printMsg(option)
  1340)     endif
  1341) #endif
  1342) 
  1343)   enddo
  1344) 
  1345)   select case(global_auxvar%istate)
  1346)     case(LIQUID_STATE)
  1347)       gen_auxvar(GENERAL_LIQUID_PRESSURE_DOF)%pert = &
  1348)         gen_auxvar(GENERAL_LIQUID_PRESSURE_DOF)%pert / general_pressure_scale
  1349)     case(TWO_PHASE_STATE)
  1350)       gen_auxvar(GENERAL_GAS_PRESSURE_DOF)%pert = &
  1351)         gen_auxvar(GENERAL_GAS_PRESSURE_DOF)%pert / general_pressure_scale
  1352)       if (general_2ph_energy_dof == GENERAL_AIR_PRESSURE_INDEX) then
  1353)         gen_auxvar(GENERAL_2PH_STATE_AIR_PRESSURE_DOF)%pert = &
  1354)           gen_auxvar(GENERAL_2PH_STATE_AIR_PRESSURE_DOF)%pert / &
  1355)           general_pressure_scale
  1356)       endif
  1357)     case(GAS_STATE)
  1358)       gen_auxvar(GENERAL_GAS_PRESSURE_DOF)%pert = &
  1359)         gen_auxvar(GENERAL_GAS_PRESSURE_DOF)%pert / general_pressure_scale
  1360)       gen_auxvar(GENERAL_GAS_STATE_AIR_PRESSURE_DOF)%pert = &
  1361)         gen_auxvar(GENERAL_GAS_STATE_AIR_PRESSURE_DOF)%pert / general_pressure_scale
  1362)   end select
  1363)   
  1364) #ifdef DEBUG_GENERAL
  1365)   call GlobalAuxVarStrip(global_auxvar_debug)
  1366)   call GeneralAuxVarStrip(general_auxvar_debug)
  1367) #endif
  1368)  
  1369) end subroutine GeneralAuxVarPerturb
  1370) 
  1371) ! ************************************************************************** !
  1372) 
  1373) subroutine GeneralPrintAuxVars(general_auxvar,global_auxvar,material_auxvar, &
  1374)                                natural_id,string,option)
  1375)   ! 
  1376)   ! Prints out the contents of an auxvar
  1377)   ! 
  1378)   ! Author: Glenn Hammond
  1379)   ! Date: 02/18/13
  1380)   ! 
  1381) 
  1382)   use Global_Aux_module
  1383)   use Material_Aux_class
  1384)   use Option_module
  1385) 
  1386)   implicit none
  1387) 
  1388)   type(general_auxvar_type) :: general_auxvar
  1389)   type(global_auxvar_type) :: global_auxvar
  1390)   class(material_auxvar_type) :: material_auxvar
  1391)   PetscInt :: natural_id
  1392)   character(len=*) :: string
  1393)   type(option_type) :: option
  1394) 
  1395)   PetscInt :: apid, cpid, vpid, spid
  1396)   PetscInt :: gid, lid, acid, wid, eid
  1397)   PetscReal :: liquid_mass, gas_mass
  1398)   PetscReal :: liquid_density, gas_density
  1399)   PetscReal :: liquid_energy, gas_energy
  1400)   PetscReal :: liquid_saturation, gas_saturation
  1401) 
  1402)   lid = option%liquid_phase
  1403)   gid = option%gas_phase
  1404)   apid = option%air_pressure_id
  1405)   cpid = option%capillary_pressure_id
  1406)   vpid = option%vapor_pressure_id
  1407)   spid = option%saturation_pressure_id
  1408) 
  1409)   acid = option%air_id ! air component id
  1410)   wid = option%water_id
  1411)   eid = option%energy_id
  1412) 
  1413)   liquid_density = 0.d0
  1414)   gas_density = 0.d0
  1415)   liquid_energy = 0.d0
  1416)   gas_energy = 0.d0
  1417)   liquid_saturation = 0.d0
  1418)   gas_saturation = 0.d0
  1419) 
  1420)   print *, '--------------------------------------------------------'
  1421)   print *, trim(string)
  1422)   print *, '                 cell id: ', natural_id
  1423)   select case(global_auxvar%istate)
  1424)     case(LIQUID_STATE)
  1425)       print *, '     Thermodynamic state: Liquid phase'
  1426)       liquid_density = general_auxvar%den(lid)
  1427)       liquid_energy = general_auxvar%U(lid)
  1428)       liquid_saturation = general_auxvar%sat(lid)
  1429)     case(GAS_STATE)
  1430)       print *, '     Thermodynamic state: Gas phase'
  1431)       gas_density = general_auxvar%den(gid)
  1432)       gas_energy = general_auxvar%U(gid)
  1433)       gas_saturation = general_auxvar%sat(gid)
  1434)     case(TWO_PHASE_STATE)
  1435)       print *, '     Thermodynamic state: Two phase'
  1436)       liquid_density = general_auxvar%den(lid)
  1437)       gas_density = general_auxvar%den(gid)
  1438)       liquid_energy = general_auxvar%U(lid)
  1439)       gas_energy = general_auxvar%U(gid)
  1440)       liquid_saturation = general_auxvar%sat(lid)
  1441)       gas_saturation = general_auxvar%sat(gid)
  1442)   end select
  1443)   liquid_mass = (liquid_density*general_auxvar%xmol(lid,lid)* & 
  1444)                  liquid_saturation+ &
  1445)                  gas_density*general_auxvar%xmol(lid,gid)* & 
  1446)                  gas_saturation)* & 
  1447)                  general_auxvar%effective_porosity*material_auxvar%volume
  1448)   gas_mass = (liquid_density*general_auxvar%xmol(gid,lid)* & 
  1449)               liquid_saturation+ &
  1450)               gas_density*general_auxvar%xmol(gid,gid)* & 
  1451)               gas_saturation)* & 
  1452)               general_auxvar%effective_porosity*material_auxvar%volume
  1453)   print *, 'tot liq comp mass [kmol]: ', liquid_mass
  1454)   print *, 'tot gas comp mass [kmol]: ', gas_mass
  1455)   print *, '             energy [MJ]: ', liquid_mass*liquid_energy + &
  1456)                                          gas_mass*gas_energy
  1457)   print *, '         liquid pressure: ', general_auxvar%pres(lid)
  1458)   print *, '            gas pressure: ', general_auxvar%pres(gid)
  1459)   print *, '            air pressure: ', general_auxvar%pres(apid)
  1460)   print *, '      capillary pressure: ', general_auxvar%pres(cpid)
  1461)   print *, '          vapor pressure: ', general_auxvar%pres(vpid)
  1462)   print *, '     saturation pressure: ', general_auxvar%pres(spid)
  1463)   print *, '       liquid saturation: ', general_auxvar%sat(lid)
  1464)   print *, '          gas saturation: ', general_auxvar%sat(gid)
  1465)   print *, '   liquid density [kmol]: ', general_auxvar%den(lid)
  1466)   print *, '      gas density [kmol]: ', general_auxvar%den(gid)
  1467)   print *, '     liquid density [kg]: ', general_auxvar%den_kg(lid)
  1468)   print *, '        gas density [kg]: ', general_auxvar%den_kg(gid)
  1469)   print *, '         temperature [C]: ', general_auxvar%temp
  1470)   print *, '      liquid H [MJ/kmol]: ', general_auxvar%H(lid)
  1471)   print *, '         gas H [MJ/kmol]: ', general_auxvar%H(gid)
  1472)   print *, '      liquid U [MJ/kmol]: ', general_auxvar%U(lid)
  1473)   print *, '         gas U [MJ/kmol]: ', general_auxvar%U(gid)
  1474)   print *, '     X (water in liquid): ', general_auxvar%xmol(lid,lid)
  1475)   print *, '       X (air in liquid): ', general_auxvar%xmol(gid,lid)
  1476)   print *, '        X (water in gas): ', general_auxvar%xmol(lid,gid)
  1477)   print *, '          X (air in gas): ', general_auxvar%xmol(gid,gid)
  1478)   print *, '         liquid mobility: ', general_auxvar%mobility(lid)
  1479)   print *, '            gas mobility: ', general_auxvar%mobility(gid)
  1480)   print *, '      effective porosity: ', general_auxvar%effective_porosity
  1481)   print *, '--------------------------------------------------------'
  1482) 
  1483) end subroutine GeneralPrintAuxVars
  1484) 
  1485) ! ************************************************************************** !
  1486) 
  1487) subroutine GeneralOutputAuxVars1(general_auxvar,global_auxvar,material_auxvar, &
  1488)                                  natural_id,string,append,option)
  1489)   ! 
  1490)   ! Prints out the contents of an auxvar to a file
  1491)   ! 
  1492)   ! Author: Glenn Hammond
  1493)   ! Date: 02/18/13
  1494)   ! 
  1495) 
  1496)   use Global_Aux_module
  1497)   use Material_Aux_class
  1498)   use Option_module
  1499) 
  1500)   implicit none
  1501) 
  1502)   type(general_auxvar_type) :: general_auxvar
  1503)   type(global_auxvar_type) :: global_auxvar
  1504)   class(material_auxvar_type) :: material_auxvar
  1505)   PetscInt :: natural_id
  1506)   character(len=*) :: string
  1507)   PetscBool :: append
  1508)   type(option_type) :: option
  1509) 
  1510)   character(len=MAXSTRINGLENGTH) :: string2
  1511)   PetscInt :: apid, cpid, vpid, spid
  1512)   PetscInt :: gid, lid, acid, wid, eid
  1513)   PetscReal :: liquid_mass, gas_mass
  1514)   PetscReal :: liquid_density, gas_density
  1515)   PetscReal :: liquid_energy, gas_energy
  1516)   PetscReal :: liquid_saturation, gas_saturation
  1517) 
  1518)   lid = option%liquid_phase
  1519)   gid = option%gas_phase
  1520)   apid = option%air_pressure_id
  1521)   cpid = option%capillary_pressure_id
  1522)   vpid = option%vapor_pressure_id
  1523)   spid = option%saturation_pressure_id
  1524) 
  1525)   acid = option%air_id ! air component id
  1526)   wid = option%water_id
  1527)   eid = option%energy_id
  1528)   
  1529)   liquid_density = 0.d0
  1530)   gas_density = 0.d0
  1531)   liquid_energy = 0.d0
  1532)   gas_energy = 0.d0
  1533)   liquid_saturation = 0.d0
  1534)   gas_saturation = 0.d0
  1535) 
  1536)   write(string2,*) natural_id
  1537)   string2 = trim(adjustl(string)) // '_' // trim(adjustl(string2)) // '.txt'
  1538)   if (append) then
  1539)     open(unit=86,file=string2,position='append')
  1540)   else
  1541)     open(unit=86,file=string2)
  1542)   endif
  1543) 
  1544)   write(86,*) '--------------------------------------------------------'
  1545)   write(86,*) trim(string)
  1546)   write(86,*) '             cell id: ', natural_id
  1547)   select case(global_auxvar%istate)
  1548)     case(LIQUID_STATE)
  1549)       write(86,*) ' Thermodynamic state: Liquid phase'
  1550)       liquid_density = general_auxvar%den(lid)
  1551)       liquid_energy = general_auxvar%U(lid)
  1552)       liquid_saturation = general_auxvar%sat(lid)
  1553)     case(GAS_STATE)
  1554)       write(86,*) ' Thermodynamic state: Gas phase'
  1555)       gas_density = general_auxvar%den(gid)
  1556)       gas_energy = general_auxvar%U(gid)
  1557)       gas_saturation = general_auxvar%sat(gid)
  1558)     case(TWO_PHASE_STATE)
  1559)       write(86,*) ' Thermodynamic state: Two phase'
  1560)       liquid_density = general_auxvar%den(lid)
  1561)       gas_density = general_auxvar%den(gid)
  1562)       liquid_energy = general_auxvar%U(lid)
  1563)       gas_energy = general_auxvar%U(gid)
  1564)       liquid_saturation = general_auxvar%sat(lid)
  1565)       gas_saturation = general_auxvar%sat(gid)
  1566)   end select
  1567)   liquid_mass = (liquid_density*general_auxvar%xmol(lid,lid)* & 
  1568)                  liquid_saturation+ &
  1569)                  gas_density*general_auxvar%xmol(lid,gid)* & 
  1570)                  gas_saturation)* & 
  1571)                  general_auxvar%effective_porosity*material_auxvar%volume
  1572)   gas_mass = (liquid_density*general_auxvar%xmol(gid,lid)* & 
  1573)               liquid_saturation+ &
  1574)               gas_density*general_auxvar%xmol(gid,gid)* & 
  1575)               gas_saturation)* & 
  1576)               general_auxvar%effective_porosity*material_auxvar%volume
  1577)   write(86,*) 'tot liq comp mass [kmol]: ', liquid_mass
  1578)   write(86,*) 'tot gas comp mass [kmol]: ', gas_mass
  1579)   write(86,*) '             energy [MJ]: ', liquid_mass*liquid_energy + &
  1580)                                             gas_mass*gas_energy
  1581)   write(86,*) '         liquid pressure: ', general_auxvar%pres(lid)
  1582)   write(86,*) '            gas pressure: ', general_auxvar%pres(gid)
  1583)   write(86,*) '            air pressure: ', general_auxvar%pres(apid)
  1584)   write(86,*) '      capillary pressure: ', general_auxvar%pres(cpid)
  1585)   write(86,*) '          vapor pressure: ', general_auxvar%pres(vpid)
  1586)   write(86,*) '     saturation pressure: ', general_auxvar%pres(spid)
  1587)   write(86,*) '         temperature [C]: ', general_auxvar%temp
  1588)   write(86,*) '       liquid saturation: ', general_auxvar%sat(lid)
  1589)   write(86,*) '          gas saturation: ', general_auxvar%sat(gid)
  1590)   write(86,*) '   liquid density [kmol]: ', general_auxvar%den(lid)
  1591)   write(86,*) '     liquid density [kg]: ', general_auxvar%den_kg(lid)
  1592)   write(86,*) '      gas density [kmol]: ', general_auxvar%den(gid)
  1593)   write(86,*) '        gas density [kg]: ', general_auxvar%den_kg(gid)
  1594)   write(86,*) '     X (water in liquid): ', general_auxvar%xmol(lid,lid)
  1595)   write(86,*) '       X (air in liquid): ', general_auxvar%xmol(gid,lid)
  1596)   write(86,*) '        X (water in gas): ', general_auxvar%xmol(lid,gid)
  1597)   write(86,*) '          X (air in gas): ', general_auxvar%xmol(gid,gid)
  1598)   write(86,*) '      liquid H [MJ/kmol]: ', general_auxvar%H(lid)
  1599)   write(86,*) '         gas H [MJ/kmol]: ', general_auxvar%H(gid)
  1600)   write(86,*) '      liquid U [MJ/kmol]: ', general_auxvar%U(lid)
  1601)   write(86,*) '         gas U [MJ/kmol]: ', general_auxvar%U(gid)
  1602)   write(86,*) '         liquid mobility: ', general_auxvar%mobility(lid)
  1603)   write(86,*) '            gas mobility: ', general_auxvar%mobility(gid)
  1604)   write(86,*) '      effective porosity: ', general_auxvar%effective_porosity
  1605)   write(86,*) '...'
  1606)   write(86,*) liquid_mass
  1607)   write(86,*) gas_mass
  1608)   write(86,*) liquid_mass*general_auxvar%U(lid) + &
  1609)               gas_mass*general_auxvar%U(gid)
  1610)   write(86,*) general_auxvar%pres(lid)
  1611)   write(86,*) general_auxvar%pres(gid)
  1612)   write(86,*) general_auxvar%pres(apid)
  1613)   write(86,*) general_auxvar%pres(cpid)
  1614)   write(86,*) general_auxvar%pres(vpid)
  1615)   write(86,*) general_auxvar%pres(spid)
  1616)   write(86,*) general_auxvar%temp
  1617)   write(86,*) general_auxvar%sat(lid)
  1618)   write(86,*) general_auxvar%sat(gid)
  1619)   write(86,*) general_auxvar%den(lid)
  1620)   write(86,*) general_auxvar%den_kg(lid)
  1621)   write(86,*) general_auxvar%den(gid)
  1622)   write(86,*) general_auxvar%den_kg(gid)
  1623)   write(86,*) general_auxvar%xmol(lid,lid)
  1624)   write(86,*) general_auxvar%xmol(gid,lid)
  1625)   write(86,*) general_auxvar%xmol(lid,gid)
  1626)   write(86,*) general_auxvar%xmol(gid,gid)
  1627)   write(86,*) general_auxvar%H(lid)
  1628)   write(86,*) general_auxvar%H(gid)
  1629)   write(86,*) general_auxvar%U(lid)
  1630)   write(86,*) general_auxvar%U(gid)
  1631)   write(86,*) ''
  1632)   write(86,*) general_auxvar%mobility(lid)
  1633)   write(86,*) general_auxvar%mobility(gid)
  1634)   write(86,*) general_auxvar%effective_porosity
  1635)   write(86,*) '--------------------------------------------------------'
  1636)   
  1637)   close(86)
  1638) 
  1639) end subroutine GeneralOutputAuxVars1
  1640) 
  1641) ! ************************************************************************** !
  1642) 
  1643) subroutine GeneralOutputAuxVars2(general_auxvars,global_auxvars,option)
  1644)   ! 
  1645)   ! Prints out the contents of an auxvar to a file
  1646)   ! 
  1647)   ! Author: Glenn Hammond
  1648)   ! Date: 02/18/13
  1649)   ! 
  1650) 
  1651)   use Global_Aux_module
  1652)   use Option_module
  1653) 
  1654)   implicit none
  1655) 
  1656)   type(general_auxvar_type) :: general_auxvars(0:,:)
  1657)   type(global_auxvar_type) :: global_auxvars(:)
  1658)   type(option_type) :: option
  1659) 
  1660)   character(len=MAXSTRINGLENGTH) :: string
  1661)   PetscInt :: apid, cpid, vpid
  1662)   PetscInt :: gid, lid, acid, wid, eid
  1663)   PetscInt :: i, n, idof
  1664) 
  1665)   lid = option%liquid_phase
  1666)   gid = option%gas_phase
  1667)   apid = option%air_pressure_id
  1668)   cpid = option%capillary_pressure_id
  1669)   vpid = option%vapor_pressure_id
  1670) 
  1671)   acid = option%air_id ! air component id
  1672)   wid = option%water_id
  1673)   eid = option%energy_id
  1674)   
  1675)   string = 'general_auxvar.txt'
  1676)   open(unit=86,file=string)
  1677)   
  1678)   n = size(global_auxvars)
  1679) 
  1680) 100 format(a,100('','',i9))
  1681)   
  1682)   write(86,'(a,100('','',i9))') '             cell id: ', &
  1683)     ((i,i=1,n),idof=0,3)
  1684)   write(86,'(a,100('','',i2))') '                idof: ', &
  1685)     ((idof,i=1,n),idof=0,3)
  1686)   write(86,'(a,100('','',i2))') '               state: ', &
  1687)     (global_auxvars(i)%istate,i=1,n)
  1688)   write(86,100) '      liquid pressure: ', &
  1689)     ((general_auxvars(idof,i)%pres(lid),i=1,n),idof=0,3)
  1690)   write(86,100) '         gas pressure: ', &
  1691)     ((general_auxvars(idof,i)%pres(gid),i=1,n),idof=0,3)
  1692)   write(86,100) '         air pressure: ', &
  1693)     ((general_auxvars(idof,i)%pres(apid),i=1,n),idof=0,3)
  1694)   write(86,100) '   capillary pressure: ', &
  1695)     ((general_auxvars(idof,i)%pres(cpid),i=1,n),idof=0,3)
  1696)   write(86,100) '       vapor pressure: ', &
  1697)     ((general_auxvars(idof,i)%pres(vpid),i=1,n),idof=0,3)
  1698)   write(86,100) '      temperature [C]: ', &
  1699)     ((general_auxvars(idof,i)%temp,i=1,n),idof=0,3)
  1700)   write(86,100) '    liquid saturation: ', &
  1701)     ((general_auxvars(idof,i)%sat(lid),i=1,n),idof=0,3)
  1702)   write(86,100) '       gas saturation: ', &
  1703)     ((general_auxvars(idof,i)%sat(gid),i=1,n),idof=0,3)
  1704)   write(86,100) 'liquid density [kmol]: ', &
  1705)     ((general_auxvars(idof,i)%den(lid),i=1,n),idof=0,3)
  1706)   write(86,100) '  liquid density [kg]: ', &
  1707)     ((general_auxvars(idof,i)%den_kg(lid),i=1,n),idof=0,3)
  1708)   write(86,100) '   gas density [kmol]: ', &
  1709)     ((general_auxvars(idof,i)%den(gid),i=1,n),idof=0,3)
  1710)   write(86,100) '     gas density [kg]: ', &
  1711)     ((general_auxvars(idof,i)%den_kg(gid),i=1,n),idof=0,3)
  1712)   write(86,100) '  X (water in liquid): ', &
  1713)     ((general_auxvars(idof,i)%xmol(lid,lid),i=1,n),idof=0,3)
  1714)   write(86,100) '    X (air in liquid): ', &
  1715)     ((general_auxvars(idof,i)%xmol(gid,lid),i=1,n),idof=0,3)
  1716)   write(86,100) '     X (water in gas): ', &
  1717)     ((general_auxvars(idof,i)%xmol(lid,gid),i=1,n),idof=0,3)
  1718)   write(86,100) '       X (air in gas): ', &
  1719)     ((general_auxvars(idof,i)%xmol(gid,gid),i=1,n),idof=0,3)
  1720)   write(86,100) '   liquid H [MJ/kmol]: ', &
  1721)     ((general_auxvars(idof,i)%H(lid),i=1,n),idof=0,3)
  1722)   write(86,100) '      gas H [MJ/kmol]: ', &
  1723)     ((general_auxvars(idof,i)%H(gid),i=1,n),idof=0,3)
  1724)   write(86,100) '   liquid U [MJ/kmol]: ', &
  1725)     ((general_auxvars(idof,i)%U(lid),i=1,n),idof=0,3)
  1726)   write(86,100) '      gas U [MJ/kmol]: ', &
  1727)     ((general_auxvars(idof,i)%U(gid),i=1,n),idof=0,3)
  1728)   write(86,*)
  1729)   write(86,100) '      liquid mobility: ', &
  1730)     ((general_auxvars(idof,i)%mobility(lid),i=1,n),idof=0,3)
  1731)   write(86,100) '         gas mobility: ', &
  1732)     ((general_auxvars(idof,i)%mobility(gid),i=1,n),idof=0,3)
  1733)   write(86,100) '   effective porosity: ', &
  1734)     ((general_auxvars(idof,i)%effective_porosity,i=1,n),idof=0,3)
  1735)   
  1736)   close(86)
  1737) 
  1738) end subroutine GeneralOutputAuxVars2
  1739) 
  1740) ! ************************************************************************** !
  1741) 
  1742) subroutine GeneralAuxVarSingleDestroy(auxvar)
  1743)   ! 
  1744)   ! Deallocates a mode auxiliary object
  1745)   ! 
  1746)   ! Author: Glenn Hammond
  1747)   ! Date: 01/10/12
  1748)   ! 
  1749) 
  1750)   implicit none
  1751) 
  1752)   type(general_auxvar_type), pointer :: auxvar
  1753)   
  1754)   if (associated(auxvar)) then
  1755)     call GeneralAuxVarStrip(auxvar)
  1756)     deallocate(auxvar)
  1757)   endif
  1758)   nullify(auxvar)  
  1759) 
  1760) end subroutine GeneralAuxVarSingleDestroy
  1761) 
  1762) ! ************************************************************************** !
  1763) 
  1764) subroutine GeneralAuxVarArray1Destroy(auxvars)
  1765)   ! 
  1766)   ! Deallocates a mode auxiliary object
  1767)   ! 
  1768)   ! Author: Glenn Hammond
  1769)   ! Date: 01/10/12
  1770)   ! 
  1771) 
  1772)   implicit none
  1773) 
  1774)   type(general_auxvar_type), pointer :: auxvars(:)
  1775)   
  1776)   PetscInt :: iaux
  1777)   
  1778)   if (associated(auxvars)) then
  1779)     do iaux = 1, size(auxvars)
  1780)       call GeneralAuxVarStrip(auxvars(iaux))
  1781)     enddo  
  1782)     deallocate(auxvars)
  1783)   endif
  1784)   nullify(auxvars)  
  1785) 
  1786) end subroutine GeneralAuxVarArray1Destroy
  1787) 
  1788) ! ************************************************************************** !
  1789) 
  1790) subroutine GeneralAuxVarArray2Destroy(auxvars)
  1791)   ! 
  1792)   ! Deallocates a mode auxiliary object
  1793)   ! 
  1794)   ! Author: Glenn Hammond
  1795)   ! Date: 01/10/12
  1796)   ! 
  1797) 
  1798)   implicit none
  1799) 
  1800)   type(general_auxvar_type), pointer :: auxvars(:,:)
  1801)   
  1802)   PetscInt :: iaux, idof
  1803)   
  1804)   if (associated(auxvars)) then
  1805)     do iaux = 1, size(auxvars,2)
  1806)       do idof = 1, size(auxvars,1)
  1807)         call GeneralAuxVarStrip(auxvars(idof-1,iaux))
  1808)       enddo
  1809)     enddo  
  1810)     deallocate(auxvars)
  1811)   endif
  1812)   nullify(auxvars)  
  1813) 
  1814) end subroutine GeneralAuxVarArray2Destroy
  1815) 
  1816) ! ************************************************************************** !
  1817) 
  1818) subroutine GeneralAuxVarStrip(auxvar)
  1819)   ! 
  1820)   ! GeneralAuxVarDestroy: Deallocates a general auxiliary object
  1821)   ! 
  1822)   ! Author: Glenn Hammond
  1823)   ! Date: 03/07/11
  1824)   ! 
  1825)   use Utility_module, only : DeallocateArray
  1826) 
  1827)   implicit none
  1828) 
  1829)   type(general_auxvar_type) :: auxvar
  1830)   
  1831)   call DeallocateArray(auxvar%pres)  
  1832)   call DeallocateArray(auxvar%sat)  
  1833)   call DeallocateArray(auxvar%den)  
  1834)   call DeallocateArray(auxvar%den_kg)  
  1835)   call DeallocateArray(auxvar%xmol)  
  1836)   call DeallocateArray(auxvar%H)  
  1837)   call DeallocateArray(auxvar%U)  
  1838)   call DeallocateArray(auxvar%mobility)  
  1839)   if (associated(auxvar%d)) then
  1840)     deallocate(auxvar%d)
  1841)     nullify(auxvar%d)
  1842)   endif
  1843)   
  1844) end subroutine GeneralAuxVarStrip
  1845) 
  1846) ! ************************************************************************** !
  1847) 
  1848) subroutine GeneralAuxDestroy(aux)
  1849)   ! 
  1850)   ! Deallocates a general auxiliary object
  1851)   ! 
  1852)   ! Author: Glenn Hammond
  1853)   ! Date: 03/07/11
  1854)   ! 
  1855)   use Utility_module, only : DeallocateArray
  1856) 
  1857)   implicit none
  1858) 
  1859)   type(general_type), pointer :: aux
  1860)   PetscInt :: iaux, idof
  1861)   
  1862)   if (.not.associated(aux)) return
  1863)   
  1864)   call GeneralAuxVarDestroy(aux%auxvars)
  1865)   call GeneralAuxVarDestroy(aux%auxvars_bc)
  1866)   call GeneralAuxVarDestroy(aux%auxvars_ss)
  1867) 
  1868)   call DeallocateArray(aux%inactive_rows_local)
  1869)   call DeallocateArray(aux%inactive_rows_local_ghosted)
  1870)   call DeallocateArray(aux%row_zeroing_array)
  1871) 
  1872)   if (associated(aux%general_parameter)) then
  1873)     call DeallocateArray(aux%general_parameter%diffusion_coefficient)
  1874)     deallocate(aux%general_parameter)
  1875)   endif
  1876)   nullify(aux%general_parameter)
  1877)   
  1878)   deallocate(aux)
  1879)   nullify(aux)
  1880)   
  1881) end subroutine GeneralAuxDestroy
  1882) 
  1883) end module General_Aux_module

generated by
Intel(R) C++/Fortran Compiler code-coverage tool
Web-Page Owner: Nobody