#################
# Test goal: Test of boundary field interpolation
# Author: Jan Stebel
# Source for reference: qualitative

flow123d_version: 3.1.0
problem: !Coupling_Sequential
  description: |
      Transport problem with Darcy flow driven by the boundary condition from the larger model.
      Source term is prescribed on a part of the borehole boundary given by the index of container position.
  mesh:
    mesh_file: /home/jb/workspace/Endorse/tests/sandbox/edz_pos10/output/L00_S0000011/input_fields.msh2
    regions:
      - !From_Label
        name: .fr_bore
        mesh_label: .fr_boreholes
        allow_empty: True
      - !Union
        name: fractures
        regions:
            - fr_large
            - fr_small

  flow_equation: !Flow_Darcy_LMH
    output_specific:
    nonlinear_solver:
      linear_solver: !Petsc
        a_tol: 1e-16
        r_tol: 1e-8
        options: -ksp_type cg -pc_type hypre -pc_hypre_type boomeramg  -pc_hypre_boomeramg_coarsen_type HMIS -pc_hypre_boomeramg_strong_threshold 0.5  -ksp_monitor

    input_fields:
      - region: [.box_outer, .fr_outer]
        bc_type: dirichlet
        bc_piezo_head: !FieldFE
          mesh_data_file: /home/jb/workspace/Endorse/tests/test_data/large_model_local.msh2
          field_name: piezo_head_p0
          interpolation: P0_intersection
      - region: [box, fractures] # TODO: common region for fractures
        conductivity:  !FieldFE
          mesh_data_file: /home/jb/workspace/Endorse/tests/sandbox/edz_pos10/output/L00_S0000011/input_fields.msh2
          field_name: conductivity
          #interpolation: P0_intersection
        cross_section:  !FieldFE
          mesh_data_file: /home/jb/workspace/Endorse/tests/sandbox/edz_pos10/output/L00_S0000011/input_fields.msh2
          field_name: cross_section
          #interpolation: P0_intersection
    n_schurs: 2
    output:
      fields:
        - piezo_head_p0
        #- pressure_p0
        #- pressure_p1
        - velocity_p0
        - region_id
        - conductivity
        - cross_section
    balance:
      cumulative: true
    output_stream:
      format: !vtk
        variant: ascii

  solute_equation: !Coupling_OperatorSplitting
    substances:
        - name: U235 # uranium 235; should rather be Iodium with zero K_d.
          molar_mass: 0.235
    transport: !Solute_AdvectionDiffusion_DG
      user_fields:
        - name: bc_flux_x
          is_boundary: true
          scalar_field: !FieldFormula
            # variant for more boreholes
            #value: if((y > -2.2)and(y < 2.2)and(x > 60)and(x < 64),1.0,0) #if((y>-1129275)and(y<-1129250)and(x>-682460)and(x<-682450),1,0)
            # variant for single borehole
            value: if((x > 60)and(x < 64),1.0,0.0)
#        - name: container_conc
#          is_boundary: true
#          scalar_field: !FieldFormula
#            value:
#        - name: bc_flux_t
#          scalar_field: !FieldTimeFunction
#            time_function:
#              values: !include_csv
#                file: /home/jb/workspace/Endorse/tests/test_data/conc_flux_UOS_kg_y.csv
#                separator: " "
#                n_head_lines: 1
#                format:
#                  t:
#                    time: $0
#                  value: $1

#          scalar_field: !FieldTimeFunction
#            time_function:  # conc_flux_UOS_[kg:y]
#              values: !include_csv
#                file: /home/jb/workspace/Endorse/tests/test_data/conc_flux_UOS_kg_y.csv
#                separator: ","
#                n_head_lines: 1
#                format:
#                  t:
#                    time: $0
#                  value: $1

      input_fields:
        - region: [box, fractures]
          porosity: !FieldFE
            mesh_data_file: /home/jb/workspace/Endorse/tests/sandbox/edz_pos10/output/L00_S0000011/input_fields.msh2
            field_name: porosity
          
          # dg_penalty: 1       # mass: 80, error: 4.5, default value, solution highly oscilates
          # dg_penalty: 10      # mass: 40, error: 4.0,  high error, hard to converge
          # dg_penalty: 100     # mass: 27, error: 16,  high error, hard to converge
          dg_penalty: 10
          diff_m: 3e-16
          disp_l: 1
          disp_t: 0.1

        - region: [.box_boreholes, .fr_bore]
          bc_type: diffusive_flux #total_flux
          bc_robin_sigma: !FieldFormula
            value: bc_flux_x * (2e-10) / 1  # max. apparent diffusion of I in bentonite m^2/ s ; approx. 1m total bentonite layer
          bc_conc: !FieldFormula
            value: 0.1 * exp(-(5 * log(10) / 1000) * maximum(t / (365.2425 * 24 * 3600), 100))
#          bc_flux: !FieldFormula
#            #value: bc_flux_x * bc_flux_t / (365.2425 * 24 * 3600)  conc in mg/m3
#            value: bc_flux_x * bc_flux_t / (365.2425 * 0.24 * 0.36)
#            #unit: kg*m*md/y  unfortunately does not work so we scale manually from kg/y/m2 to kg/s/m2
#            # even for nonzero diffusion getting zero mass in balance and substantial error (since fluxes are taken from field evaluation)
#            # trying some scaling in between (could be interpreted as change in conc unit anyway)
      solver:
        a_tol: 1e-8
        r_tol: 1e-4
        options: -ksp_type gmres -pc_type ilu -pc_factor_levels 2 -ksp_diagonal_scale -ksp_diagonal_scale_fix -pc_factor_fill 1.0 -ksp_monitor

      output:    
        fields:
        - field: conc
          interpolation: [P0_value, P1_average, D1_value] #[P0_value, P1_average, D1_value]
        - region_id
    output_stream:
      format: !vtk
        variant: ascii
      times:
        - [30, 'y']
        - [60, 'y']
        - [120, 'y']
        - [240, 'y']
        - [500, 'y']
        - [1100, 'y']
        - [2300, 'y']
        - [5000, 'y']
        - [10000, 'y']
        - time: [20000, 'y']
          step: [20000, 'y']


    time:
      end_time: [200000, 'y']
      max_dt: [20000, 'y']
      dt_limits:
        - time: [0, 'y']
          max_dt: [30, 'y']
        - time: [60, 'y']             # 2
          max_dt: [60, 'y']
        - time: [540, 'y']            # 10
          max_dt: [120, 'y']
        - time: [1100, 'y']           # 15
          max_dt: [240, 'y']
        - time: [2300, 'y']          # 20
          max_dt: [500, 'y']
        - time: [5000, 'y']          # 25
          max_dt: [1000, 'y']        #
        - time: [10000, 'y']          # 30
          max_dt: [2500, 'y']        #
        - time: [20000, 'y']          # 34
          max_dt: [5000, 'y']        #
        - time: [40000, 'y']          # 38
          max_dt: [10000, 'y']        #
        - time: [80000, 'y']          # 42
          max_dt: [20000, 'y']        # 53
      common_time_unit: y
      write_used_timesteps: time_steps.yaml
    balance:
      cumulative: true
