! use File::Copy;
! use Cwd;

COMMAND FILE:
  CFX Pre Version = 12.0
END

>load mode=new
> update

! getEgFile("PipeValveMesh.gtm");

>writeCaseFile filename=PipeValve.cfx
> update

>gtmImport filename=PipeValveMesh.gtm, type=GTM,\
 units=m, nameStrategy= Assembly
> update

LIBRARY:
  &replace   MATERIAL: Sand Fully Coupled
    Material Group = Particle Solids
    Object Origin = User
    Option = Pure Substance
    Thermodynamic State = Solid
    PROPERTIES:
      Option = General Material
      EQUATION OF STATE:
        Density = 2300 [kg m^-3]
        Molar Mass = 1.0 [kg kmol^-1]
        Option = Value
      END # EQUATION OF STATE:
      REFERENCE STATE:
        Option = Specified Point
        Reference Temperature = 300 [K]
      END # REFERENCE STATE:
      SPECIFIC HEAT CAPACITY:
        Option = Value
        Specific Heat Capacity = 0 [J kg^-1 K^-1]
      END # SPECIFIC HEAT CAPACITY:
    END # PROPERTIES:
  END # MATERIAL:Sand Fully Coupled
END # LIBRARY:
> update

>copy from=/LIBRARY/MATERIAL:Sand Fully Coupled, to=/LIBRARY/MATERIAL:Copy of \
Sand Fully Coupled
> update

>rename, from=/LIBRARY/MATERIAL:Copy of Sand Fully Coupled, to=Sand One Way \
Coupled
> update

PHYSICS OPTIONS CONTROL:
  Automatic Physics Update = On
  Beta Physics Enabled = Off
  Constant Physics Control = On
  Default Boundary Type = Standard
  Default Domain Enabled = On
  Default Interface Enabled = Off
  Interface Generation Method = One per Interface Type
  Physics Validation Disabled = Off
  Show Interface Boundaries in Outline Tree = On
END
> update

FLOW: Flow Analysis 1
  &replace   DOMAIN: Default Domain
    Coord Frame = Coord 0
    Domain Type = Fluid
    Location = B1.P3
    BOUNDARY: Default Domain Default
      Boundary Type = WALL
      Create Other Side = Off
      Interface Boundary = Off
      Location = F1.B1.P3,F10.B1.P3,F11.B1.P3,F12.B1.P3,F13.B1.P3,F2.B1.P3,\
F3.B1.P3,F4.B1.P3,F5.B1.P3,F6.B1.P3,F7.B1.P3,F8.B1.P3,F9.B1.P3
      BOUNDARY CONDITIONS:
        MASS AND MOMENTUM:
          Option = No Slip Wall
        END # MASS AND MOMENTUM:
        WALL ROUGHNESS:
          Option = Smooth Wall
        END # WALL ROUGHNESS:
      END # BOUNDARY CONDITIONS:
    END # BOUNDARY:Default Domain Default
    DOMAIN MODELS:
      BUOYANCY MODEL:
        Option = Non Buoyant
      END # BUOYANCY MODEL:
      DOMAIN MOTION:
        Option = Stationary
      END # DOMAIN MOTION:
      MESH DEFORMATION:
        Option = None
      END # MESH DEFORMATION:
      REFERENCE PRESSURE:
        Reference Pressure = 1 [atm]
      END # REFERENCE PRESSURE:
    END # DOMAIN MODELS:
    FLUID DEFINITION: Sand Fully Coupled
      Material = Sand Fully Coupled
      Option = Material Library
      MORPHOLOGY:
        Option = Dispersed Particle Transport Solid
        PARTICLE DIAMETER DISTRIBUTION:
          Maximum Diameter = 500e-6 [m]
          Mean Diameter = 250e-6 [m]
          Minimum Diameter = 50e-6 [m]
          Option = Normal in Diameter by Mass
          Standard Deviation in Diameter = 70e-6 [m]
        END # PARTICLE DIAMETER DISTRIBUTION:
      END # MORPHOLOGY:
    END # FLUID DEFINITION:Sand Fully Coupled
    FLUID DEFINITION: Sand One Way Coupled
      Material = Sand One Way Coupled
      Option = Material Library
      MORPHOLOGY:
        Option = Dispersed Particle Transport Solid
        PARTICLE DIAMETER DISTRIBUTION:
          Maximum Diameter = 500e-6 [m]
          Mean Diameter = 250e-6 [m]
          Minimum Diameter = 50e-6 [m]
          Option = Normal in Diameter by Mass
          Standard Deviation in Diameter = 70e-6 [m]
        END # PARTICLE DIAMETER DISTRIBUTION:
      END # MORPHOLOGY:
    END # FLUID DEFINITION:Sand One Way Coupled
    FLUID DEFINITION: Water
      Material = Water
      Option = Material Library
      MORPHOLOGY:
        Option = Continuous Fluid
      END # MORPHOLOGY:
    END # FLUID DEFINITION:Water
    FLUID MODELS:
      COMBUSTION MODEL:
        Option = None
      END # COMBUSTION MODEL:
      FLUID: Sand Fully Coupled
        EROSION MODEL:
          Option = Finnie
          Reference Velocity = 1.0 [m s^-1]
          Velocity Power Factor = 2.0
        END # EROSION MODEL:
      END # FLUID:Sand Fully Coupled
      FLUID: Sand One Way Coupled
        EROSION MODEL:
          Option = Finnie
          Reference Velocity = 1.0 [m s^-1]
          Velocity Power Factor = 2.0
        END # EROSION MODEL:
      END # FLUID:Sand One Way Coupled
      HEAT TRANSFER MODEL:
        Option = None
      END # HEAT TRANSFER MODEL:
      THERMAL RADIATION MODEL:
        Option = None
      END # THERMAL RADIATION MODEL:
      TURBULENCE MODEL:
        Option = k epsilon
      END # TURBULENCE MODEL:
      TURBULENT WALL FUNCTIONS:
        Option = Scalable
      END # TURBULENT WALL FUNCTIONS:
    END # FLUID MODELS:
    FLUID PAIR: Water | Sand Fully Coupled
      Particle Coupling = Fully Coupled
      MOMENTUM TRANSFER:
        DRAG FORCE:
          Option = Schiller Naumann
        END # DRAG FORCE:
        PRESSURE GRADIENT FORCE:
          Option = None
        END # PRESSURE GRADIENT FORCE:
        TURBULENT DISPERSION FORCE:
          Option = None
        END # TURBULENT DISPERSION FORCE:
        VIRTUAL MASS FORCE:
          Option = None
        END # VIRTUAL MASS FORCE:
      END # MOMENTUM TRANSFER:
    END # FLUID PAIR:Water | Sand Fully Coupled
    FLUID PAIR: Water | Sand One Way Coupled
      Particle Coupling = One-way Coupling
      MOMENTUM TRANSFER:
        DRAG FORCE:
          Option = Schiller Naumann
        END # DRAG FORCE:
        PRESSURE GRADIENT FORCE:
          Option = None
        END # PRESSURE GRADIENT FORCE:
        TURBULENT DISPERSION FORCE:
          Option = None
        END # TURBULENT DISPERSION FORCE:
        VIRTUAL MASS FORCE:
          Option = None
        END # VIRTUAL MASS FORCE:
      END # MOMENTUM TRANSFER:
    END # FLUID PAIR:Water | Sand One Way Coupled
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1
> update

LIBRARY:
  CEL:
    EXPRESSIONS:
      Rmax=20 [mm]
    END
  END
END
> update

LIBRARY:
  CEL:
    EXPRESSIONS:
      Wmax=5 [m s^-1]
    END
  END
END
> update

LIBRARY:
  CEL:
    EXPRESSIONS:
      Wprof=Wmax*(abs(1-r/Rmax)^0.143)
    END
  END
END
> update

FLOW: Flow Analysis 1
  DOMAIN: Default Domain
    &replace     BOUNDARY: inlet
      Boundary Type = INLET
      Interface Boundary = Off
      Location = inlet
      BOUNDARY CONDITIONS:
        FLOW REGIME:
          Option = Subsonic
        END # FLOW REGIME:
        MASS AND MOMENTUM:
          Option = Cartesian Velocity Components
          U = 0 [m s^-1]
          V = 0 [m s^-1]
          W = Wprof
        END # MASS AND MOMENTUM:
        TURBULENCE:
          Option = Medium Intensity and Eddy Viscosity Ratio
        END # TURBULENCE:
      END # BOUNDARY CONDITIONS:
      FLUID: Sand Fully Coupled
        BOUNDARY CONDITIONS:
          Define Particle Data = On
          MASS AND MOMENTUM:
            Option = Cartesian Velocity Components
            U = 0 [m s^-1]
            V = 0 [m s^-1]
            W = Wprof
          END # MASS AND MOMENTUM:
          PARTICLE MASS FLOW RATE:
            Mass Flow Rate = 0.01 [kg s^-1]
          END # PARTICLE MASS FLOW RATE:
          PARTICLE POSITION:
            Option = Uniform Injection
            NUMBER OF POSITIONS:
              Number = 200
              Option = Direct Specification
            END # NUMBER OF POSITIONS:
          END # PARTICLE POSITION:
        END # BOUNDARY CONDITIONS:
      END # FLUID:Sand Fully Coupled
      FLUID: Sand One Way Coupled
        BOUNDARY CONDITIONS:
          Define Particle Data = On
          MASS AND MOMENTUM:
            Option = Cartesian Velocity Components
            U = 0 [m s^-1]
            V = 0 [m s^-1]
            W = Wprof
          END # MASS AND MOMENTUM:
          PARTICLE MASS FLOW RATE:
            Mass Flow Rate = 0.01 [kg s^-1]
          END # PARTICLE MASS FLOW RATE:
          PARTICLE POSITION:
            Option = Uniform Injection
            NUMBER OF POSITIONS:
              Number = 5000
              Option = Direct Specification
            END # NUMBER OF POSITIONS:
          END # PARTICLE POSITION:
        END # BOUNDARY CONDITIONS:
      END # FLUID:Sand One Way Coupled
    END # BOUNDARY:inlet
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1
> update

FLOW: Flow Analysis 1
  DOMAIN: Default Domain
    &replace     BOUNDARY: outlet
      Boundary Type = OUTLET
      Interface Boundary = Off
      Location = outlet
      BOUNDARY CONDITIONS:
        FLOW REGIME:
          Option = Subsonic
        END # FLOW REGIME:
        MASS AND MOMENTUM:
          Option = Average Static Pressure
          Pressure Profile Blend = 0.05
          Relative Pressure = 0 [Pa]
        END # MASS AND MOMENTUM:
        PRESSURE AVERAGING:
          Option = Average Over Whole Outlet
        END # PRESSURE AVERAGING:
      END # BOUNDARY CONDITIONS:
    END # BOUNDARY:outlet
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1
> update

FLOW: Flow Analysis 1
  DOMAIN: Default Domain
    &replace     BOUNDARY: symP
      Boundary Type = SYMMETRY
      Interface Boundary = Off
      Location = symP
    END # BOUNDARY:symP
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1
> update

FLOW: Flow Analysis 1
  DOMAIN: Default Domain
    &replace     BOUNDARY: pipe wall
      Boundary Type = WALL
      Create Other Side = Off
      Interface Boundary = Off
      Location = pipe wall
      BOUNDARY CONDITIONS:
        MASS AND MOMENTUM:
          Option = No Slip Wall
        END # MASS AND MOMENTUM:
        WALL ROUGHNESS:
          Option = Rough Wall
          Sand Grain Roughness Height = 0.2 [mm]
        END # WALL ROUGHNESS:
      END # BOUNDARY CONDITIONS:
      FLUID: Sand Fully Coupled
        BOUNDARY CONDITIONS:
          Define Particle Data = Off
          PARTICLE WALL INTERACTION:
            Option = Equation Dependent
          END # PARTICLE WALL INTERACTION:
          VELOCITY:
            Option = Restitution Coefficient
            Parallel Coefficient of Restitution = 1.0
            Perpendicular Coefficient of Restitution = 0.8
          END # VELOCITY:
        END # BOUNDARY CONDITIONS:
      END # FLUID:Sand Fully Coupled
      FLUID: Sand One Way Coupled
        BOUNDARY CONDITIONS:
          Define Particle Data = Off
          PARTICLE WALL INTERACTION:
            Option = Equation Dependent
          END # PARTICLE WALL INTERACTION:
          VELOCITY:
            Option = Restitution Coefficient
            Parallel Coefficient of Restitution = 1.0
            Perpendicular Coefficient of Restitution = 0.8
          END # VELOCITY:
        END # BOUNDARY CONDITIONS:
      END # FLUID:Sand One Way Coupled
    END # BOUNDARY:pipe wall
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1
> update

FLOW: Flow Analysis 1
  DOMAIN: Default Domain
    &replace     BOUNDARY: Default Domain Default
      Boundary Type = WALL
      Create Other Side = Off
      Interface Boundary = Off
      Location = F1.B1.P3,F10.B1.P3,F11.B1.P3,F12.B1.P3,F2.B1.P3,F6.B1.P3,\
F7.B1.P3,F8.B1.P3,F9.B1.P3
      BOUNDARY CONDITIONS:
        MASS AND MOMENTUM:
          Option = No Slip Wall
        END # MASS AND MOMENTUM:
        WALL ROUGHNESS:
          Option = Smooth Wall
        END # WALL ROUGHNESS:
      END # BOUNDARY CONDITIONS:
      FLUID: Sand Fully Coupled
        BOUNDARY CONDITIONS:
          Define Particle Data = Off
          PARTICLE WALL INTERACTION:
            Option = Equation Dependent
          END # PARTICLE WALL INTERACTION:
          VELOCITY:
            Option = Restitution Coefficient
            Parallel Coefficient of Restitution = 1.0
            Perpendicular Coefficient of Restitution = 0.9
          END # VELOCITY:
        END # BOUNDARY CONDITIONS:
      END # FLUID:Sand Fully Coupled
      FLUID: Sand One Way Coupled
        BOUNDARY CONDITIONS:
          Define Particle Data = Off
          PARTICLE WALL INTERACTION:
            Option = Equation Dependent
          END # PARTICLE WALL INTERACTION:
          VELOCITY:
            Option = Restitution Coefficient
            Parallel Coefficient of Restitution = 1.0
            Perpendicular Coefficient of Restitution = 0.9
          END # VELOCITY:
        END # BOUNDARY CONDITIONS:
      END # FLUID:Sand One Way Coupled
    END # BOUNDARY:Default Domain Default
  END # DOMAIN:Default Domain
END # FLOW:Flow Analysis 1
> update

FLOW: Flow Analysis 1
  &replace   INITIALISATION:
    Option = Automatic
    INITIAL CONDITIONS:
      Velocity Type = Cartesian
      CARTESIAN VELOCITY COMPONENTS:
        Option = Automatic with Value
        U = 0 [m s^-1]
        V = 0 [m s^-1]
        W = Wprof
      END # CARTESIAN VELOCITY COMPONENTS:
      STATIC PRESSURE:
        Option = Automatic
      END # STATIC PRESSURE:
      TURBULENCE INITIAL CONDITIONS:
        Option = Medium Intensity and Eddy Viscosity Ratio
      END # TURBULENCE INITIAL CONDITIONS:
    END # INITIAL CONDITIONS:
  END # INITIALISATION:
END # FLOW:Flow Analysis 1
> update

FLOW: Flow Analysis 1
  &replace   SOLVER CONTROL:
    Turbulence Numerics = First Order
    ADVECTION SCHEME:
      Option = High Resolution
    END # ADVECTION SCHEME:
    CONVERGENCE CONTROL:
      Length Scale Option = Conservative
      Maximum Number of Iterations = 100
      Minimum Number of Iterations = 1
      Timescale Control = Auto Timescale
      Timescale Factor = 1.0
    END # CONVERGENCE CONTROL:
    CONVERGENCE CRITERIA:
      Residual Target = 1.E-4
      Residual Type = RMS
    END # CONVERGENCE CRITERIA:
    DYNAMIC MODEL CONTROL:
      Global Dynamic Model Control = On
    END # DYNAMIC MODEL CONTROL:
    PARTICLE CONTROL:
      PARTICLE INTEGRATION:
        Maximum Particle Integration Time Step = 1.0E10 [s]
        Option = Forward Euler
      END # PARTICLE INTEGRATION:
      PARTICLE TERMINATION CONTROL:
        Maximum Number of Integration Steps = 10000
        Maximum Tracking Distance = 10 [m]
        Maximum Tracking Time = 10 [s]
      END # PARTICLE TERMINATION CONTROL:
    END # PARTICLE CONTROL:
  END # SOLVER CONTROL:
END # FLOW:Flow Analysis 1
> update

>writeCaseFile filename=PipeValve.def,operation=\
write def file

>writeCaseFile

# Subroutine for copying the file
!no warnings 'redefine';
! sub getEgFile {
!       my $egdir;

!       my @files = @_;
!       foreach my $file (@files) {
                 # Skip if the file exists already
!                next if -e $file;
!                unless ($egdir = $ENV{"CFX_EG_DIR"}) {
!                        die "Examples directory variable CFX_EG_DIR not set.";
!                }
                 # Use backslash if it's a windows filepath
!                my $workdir = cwd();
!                if ($egdir =~ /\\/) {
!                       my $egfile = "$egdir\\$file";
!                       copy($egfile,".") || die "\nThe tutorial file \"$file\" was not found in the working directory. Please populate the working directory (\"$workdir\") with the required files as described in the tutorial.\n";
!                }
                 # Or a forward slash if it's Unix
!                else {
!                       my $egfile = "$egdir\/$file";
!                       copy($egfile,".") || die "\nThe tutorial file \"$file\" was not found in the working directory. Please populate the working directory (\"$workdir\") with the required files as described in the tutorial.\n";
!                }
!
!       }
! }
