A User-Coded Field Function

This section provides an example of a user-coded field function.

Suppose that you would like to define the Sutherland viscosity law as a field function. Defining this field function means that you can visualize the Sutherland viscosity for a given temperature field without having to apply it as the dynamic viscosity in your region of interest. The law is simple enough to be implemented as a user field function, but here you can see how it can be implemented as a user function. The function could be called sutherlandViscosity, taking the temperature as an argument and returning the viscosity.

In C, the following could be coded in a file sutherlandViscosity.c, using the uclib.h file:


  #include <math.h>
  #include "uclib.h"
  /* Dynamic viscosity based on Sutherland's law */
  void USERFUNCTION_EXPORT
  sutherlandViscosity(Real *result, int size, Real *T)
  {
  /* Reference viscosity, Sutherland constant and reference temperature */
    Real v0 = 1.716E-5;
    Real Cs = 110.0;
    Real T0 = 273.15;
    int i;
  /* Loop through all entities applying Sutherland's law */
    for (i = 0; i != size; ++i)
    {
      result[i] = v0 * pow(T[i]/T0, 1.5) * (T0 + Cs)/(T[i] + Cs);
    }
  }

The equivalent in Fortran 90 could be a file sutherlandViscosity.f, using the StarReal.f file:


C Dynamic viscosity based on Sutherland's law
      subroutine sutherlandViscosity(result,size,T)
      use StarRealMod
      implicit none
      integer, intent(in) :: size
      real(StarReal), intent(out) :: result(size)
      real(StarReal), intent(in) :: T(*)
      integer i
C Reference viscosity, Sutherland constant and reference temperature
      real(StarReal), parameter :: v0 = 1.716E-5
      real(StarReal), parameter :: Cs = 110.0
      real(StarReal), parameter :: T0 = 273.15
      
C Loop through all entities applying Sutherland's law
      do i = 1,size
        result(i) = v0 * (T(i)/T0)**1.5 * (T0 + Cs)/(T(i) + Cs)
      end do
      
      return
      end

This user function can then be registered with Simcenter STAR-CCM+.