subroutine r8_fake_use ( x ) !*****************************************************************************80 ! !! r8_fake_use() pretends to use an R8 variable. ! ! Discussion: ! ! Some compilers will issue a warning if a variable is unused. ! Sometimes there's a good reason to include a variable in a program, ! but not to use it. Calling this function with that variable as ! the argument will shut the compiler up. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 21 April 2020 ! ! Author: ! ! John Burkardt ! ! Input: ! ! real ( kind = rk8 ) X, the variable to be "used". ! implicit none integer, parameter :: rk8 = kind ( 1.0D+00 ) real ( kind = rk8 ) x if ( x /= x ) then write ( *, '(a)' ) ' r8_fake_use(): variable is NAN.' end if return end function r8mat_det_4d ( a ) !*****************************************************************************80 ! !! r8mat_det_4d() computes the determinant of a 4 by 4 R8MAT. ! ! Discussion: ! ! An R8MAT is a two dimensional matrix of double precision real values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 01 March 1999 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, real ( kind = rk ) A(4,4), the matrix whose determinant is desired. ! ! Output, real ( kind = rk ) R8MAT_DET_4D, the determinant of the matrix. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) real ( kind = rk ) a(4,4) real ( kind = rk ) r8mat_det_4d r8mat_det_4d = & a(1,1) * ( & a(2,2) * ( a(3,3) * a(4,4) - a(3,4) * a(4,3) ) & - a(2,3) * ( a(3,2) * a(4,4) - a(3,4) * a(4,2) ) & + a(2,4) * ( a(3,2) * a(4,3) - a(3,3) * a(4,2) ) ) & - a(1,2) * ( & a(2,1) * ( a(3,3) * a(4,4) - a(3,4) * a(4,3) ) & - a(2,3) * ( a(3,1) * a(4,4) - a(3,4) * a(4,1) ) & + a(2,4) * ( a(3,1) * a(4,3) - a(3,3) * a(4,1) ) ) & + a(1,3) * ( & a(2,1) * ( a(3,2) * a(4,4) - a(3,4) * a(4,2) ) & - a(2,2) * ( a(3,1) * a(4,4) - a(3,4) * a(4,1) ) & + a(2,4) * ( a(3,1) * a(4,2) - a(3,2) * a(4,1) ) ) & - a(1,4) * ( & a(2,1) * ( a(3,2) * a(4,3) - a(3,3) * a(4,2) ) & - a(2,2) * ( a(3,1) * a(4,3) - a(3,3) * a(4,1) ) & + a(2,3) * ( a(3,1) * a(4,2) - a(3,2) * a(4,1) ) ) return end subroutine r8mat_transpose_print ( m, n, a, title ) !*****************************************************************************80 ! !! r8mat_transpose_print() prints an R8MAT, transposed. ! ! Discussion: ! ! An R8MAT is an array of R8 values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 June 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns. ! ! Input, real ( kind = rk ) A(M,N), an M by N matrix to be printed. ! ! Input, character ( len = * ) TITLE, an optional title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer m integer n real ( kind = rk ) a(m,n) character ( len = * ) title call r8mat_transpose_print_some ( m, n, a, 1, 1, m, n, title ) return end subroutine r8mat_transpose_print_some ( m, n, a, ilo, jlo, ihi, jhi, title ) !*****************************************************************************80 ! !! r8mat_transpose_print_some() prints some of an R8MAT, transposed. ! ! Discussion: ! ! An R8MAT is an array of R8 values. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 14 June 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer M, N, the number of rows and columns. ! ! Input, real ( kind = rk ) A(M,N), an M by N matrix to be printed. ! ! Input, integer ILO, JLO, the first row and column to print. ! ! Input, integer IHI, JHI, the last row and column to print. ! ! Input, character ( len = * ) TITLE, an optional title. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer, parameter :: incx = 5 integer m integer n real ( kind = rk ) a(m,n) character ( len = 14 ) ctemp(incx) integer i integer i2 integer i2hi integer i2lo integer ihi integer ilo integer inc integer j integer j2hi integer j2lo integer jhi integer jlo character ( len = * ) title write ( *, '(a)' ) ' ' write ( *, '(a)' ) trim ( title ) do i2lo = max ( ilo, 1 ), min ( ihi, m ), incx i2hi = i2lo + incx - 1 i2hi = min ( i2hi, m ) i2hi = min ( i2hi, ihi ) inc = i2hi + 1 - i2lo write ( *, '(a)' ) ' ' do i = i2lo, i2hi i2 = i + 1 - i2lo write ( ctemp(i2), '(i8,6x)' ) i end do write ( *, '('' Row '',5a14)' ) ctemp(1:inc) write ( *, '(a)' ) ' Col' write ( *, '(a)' ) ' ' j2lo = max ( jlo, 1 ) j2hi = min ( jhi, n ) do j = j2lo, j2hi do i2 = 1, inc i = i2lo - 1 + i2 write ( ctemp(i2), '(g14.6)' ) a(i,j) end do write ( *, '(i5,1x,5a14)' ) j, ( ctemp(i), i = 1, inc ) end do end do return end subroutine reference_to_physical_tet4 ( t, n, ref, phy ) !*****************************************************************************80 ! !! reference_to_physical_tet4() maps TET4 reference points to physical points. ! ! Discussion: ! ! Given the vertices of an order 4 physical tetrahedron and a point ! (R,S,T) in the reference tetrahedron, the routine computes the value ! of the corresponding point (X,Y,Z) in the physical tetrahedron. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 09 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, real ( kind = rk ) T(3,4), the coordinates of the vertices. ! The vertices are assumed to be the images of (1,0,0), (0,1,0), ! (0,0,1) and (0,0,0) respectively. ! ! Input, integer N, the number of points to transform. ! ! Input, real ( kind = rk ) REF(3,N), points in the reference tetrahedron. ! ! Output, real ( kind = rk ) PHY(3,N), corresponding points in the ! physical tetrahedron. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer n integer i real ( kind = rk ) phy(3,n) real ( kind = rk ) ref(3,n) real ( kind = rk ) t(3,4) do i = 1, 3 phy(i,1:n) = & t(i,1) * ref(1,1:n) & + t(i,2) * ref(2,1:n) & + t(i,3) * ref(3,1:n) & + t(i,4) * ( 1.0D+00 - ref(1,1:n) - ref(2,1:n) - ref(3,1:n) ) end do return end subroutine tetrahedron_integrand_01 ( p_num, p, f_num, fp ) !*****************************************************************************80 ! !! tetrahedron_integrand_01() evaluates 1 integrand function. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Input, real ( kind = rk ) P(3,P_NUM), the evaluation points. ! ! Input, integer F_NUM, the number of integrands. ! ! Output, real ( kind = rk ) FP(F_NUM,P_NUM), the integrand values. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer f_num integer p_num real ( kind = rk ) fp(f_num,p_num) real ( kind = rk ) p(3,p_num) call r8_fake_use ( p(1,1) ) fp(1,1:p_num) = 1.0D+00 return end subroutine tetrahedron_integrand_02 ( p_num, p, f_num, fp ) !*****************************************************************************80 ! !! tetrahedron_integrand_02() evaluates 3 integrand functions. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Input, real ( kind = rk ) P(3,P_NUM), the evaluation points. ! ! Input, integer F_NUM, the number of integrands. ! ! Output, real ( kind = rk ) FP(F_NUM,P_NUM), the integrand values. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer f_num integer p_num real ( kind = rk ) fp(f_num,p_num) real ( kind = rk ) p(3,p_num) fp(1,1:p_num) = p(1,1:p_num) fp(2,1:p_num) = p(2,1:p_num) fp(3,1:p_num) = p(3,1:p_num) return end subroutine tetrahedron_integrand_03 ( p_num, p, f_num, fp ) !*****************************************************************************80 ! !! tetrahedron_integrand_03() evaluates 6 integrand functions. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Input, real ( kind = rk ) P(3,P_NUM), the evaluation points. ! ! Input, integer F_NUM, the number of integrands. ! ! Output, real ( kind = rk ) FP(F_NUM,P_NUM), the integrand values. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer f_num integer p_num real ( kind = rk ) fp(f_num,p_num) real ( kind = rk ) p(3,p_num) fp(1,1:p_num) = p(1,1:p_num) * p(1,1:p_num) fp(2,1:p_num) = p(1,1:p_num) * p(2,1:p_num) fp(3,1:p_num) = p(1,1:p_num) * p(3,1:p_num) fp(4,1:p_num) = p(2,1:p_num) * p(2,1:p_num) fp(5,1:p_num) = p(2,1:p_num) * p(3,1:p_num) fp(6,1:p_num) = p(3,1:p_num) * p(3,1:p_num) return end subroutine tetrahedron_integrand_04 ( p_num, p, f_num, fp ) !*****************************************************************************80 ! !! tetrahedron_integrand_04() evaluates 10 integrand functions. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Input, real ( kind = rk ) P(3,P_NUM), the evaluation points. ! ! Input, integer F_NUM, the number of integrands. ! ! Output, real ( kind = rk ) FP(F_NUM,P_NUM), the integrand values. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer f_num integer p_num real ( kind = rk ) fp(f_num,p_num) real ( kind = rk ) p(3,p_num) fp( 1,1:p_num) = p(1,1:p_num)**3 fp( 2,1:p_num) = p(1,1:p_num)**2 * p(2,1:p_num) fp( 3,1:p_num) = p(1,1:p_num)**2 * p(3,1:p_num) fp( 4,1:p_num) = p(1,1:p_num) * p(2,1:p_num)**2 fp( 5,1:p_num) = p(1,1:p_num) * p(2,1:p_num) * p(3,1:p_num) fp( 6,1:p_num) = p(1,1:p_num) * p(3,1:p_num)**2 fp( 7,1:p_num) = p(2,1:p_num)**3 fp( 8,1:p_num) = p(2,1:p_num)**2 * p(3,1:p_num) fp( 9,1:p_num) = p(2,1:p_num) * p(3,1:p_num)**2 fp(10,1:p_num) = p(3,1:p_num)**3 return end subroutine tetrahedron_integrand_05 ( p_num, p, f_num, fp ) !*****************************************************************************80 ! !! tetrahedron_integrand_05() evaluates 15 integrand functions. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Input, real ( kind = rk ) P(3,P_NUM), the evaluation points. ! ! Input, integer F_NUM, the number of integrands. ! ! Output, real ( kind = rk ) FP(F_NUM,P_NUM), the integrand values. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer f_num integer p_num real ( kind = rk ) fp(f_num,p_num) real ( kind = rk ) p(3,p_num) fp( 1,1:p_num) = p(1,1:p_num)**4 fp( 2,1:p_num) = p(1,1:p_num)**3 * p(2,1:p_num) fp( 3,1:p_num) = p(1,1:p_num)**3 * p(3,1:p_num) fp( 4,1:p_num) = p(1,1:p_num)**2 * p(2,1:p_num)**2 fp( 5,1:p_num) = p(1,1:p_num)**2 * p(2,1:p_num) * p(3,1:p_num) fp( 6,1:p_num) = p(1,1:p_num)**2 * p(3,1:p_num)**2 fp( 7,1:p_num) = p(1,1:p_num) * p(2,1:p_num)**3 fp( 8,1:p_num) = p(1,1:p_num) * p(2,1:p_num)**2 * p(3,1:p_num) fp( 9,1:p_num) = p(1,1:p_num) * p(2,1:p_num) * p(3,1:p_num)**2 fp(10,1:p_num) = p(1,1:p_num) * p(3,1:p_num)**3 fp(11,1:p_num) = p(2,1:p_num)**4 fp(12,1:p_num) = p(2,1:p_num)**3 * p(3,1:p_num) fp(13,1:p_num) = p(2,1:p_num)**2 * p(3,1:p_num)**2 fp(14,1:p_num) = p(2,1:p_num) * p(3,1:p_num)**3 fp(15,1:p_num) = p(3,1:p_num)**4 return end subroutine tetrahedron_monte_carlo ( t, p_num, f_num, tetrahedron_unit_sample, & tetrahedron_integrand, result ) !*****************************************************************************80 ! !! tetrahedron_monte_carlo() applies the Monte Carlo rule to integrate a function. ! ! Discussion: ! ! The function f(x,y,z) is to be integrated over a tetrahedron. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, real ( kind = rk ) T(3,4), the vertices. ! ! Input, integer P_NUM, the number of sample points. ! ! Input, integer F_NUM, the number of functions to integrate. ! ! Input, external TETRAHEDRON_UNIT_SAMPLE, the sampling routine. ! ! Input, external TETRAHEDRON_INTEGRAND, the integrand routine. ! ! Output, real ( kind = rk ) RESULT(F_NUM), the approximate integrals. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer f_num integer p_num real ( kind = rk ) fp(f_num,p_num) integer i real ( kind = rk ) p(3,p_num) real ( kind = rk ) p2(3,p_num) real ( kind = rk ) result(f_num) real ( kind = rk ) t(3,4) external tetrahedron_sample external tetrahedron_integrand real ( kind = rk ) volume call tetrahedron_volume ( t, volume ) call tetrahedron_unit_sample ( p_num, p ) call reference_to_physical_tet4 ( t, p_num, p, p2 ) call tetrahedron_integrand ( p_num, p2, f_num, fp ) do i = 1, f_num result(i) = volume * sum ( fp(i,1:p_num) ) / real ( p_num, kind = rk ) end do return end subroutine tetrahedron_unit_sample_01 ( p_num, p ) !*****************************************************************************80 ! !! tetrahedron_unit_sample_01() selects points from the unit tetrahedron. ! ! Discussion: ! ! The unit tetrahedron has vertices (1,0,0), (0,1,0), (0,0,1), (0,0,0). ! ! Any point in the unit tetrahedron CAN be chosen by this algorithm. ! ! However, the points that are chosen tend to be clustered near ! the centroid. ! ! This routine is supplied as an example of "bad" sampling. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Output, real ( kind = rk ) P(3,P_NUM), the points. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer p_num real ( kind = rk ) e(4) real ( kind = rk ) e_sum integer j real ( kind = rk ) p(3,p_num) do j = 1, p_num call random_number ( harvest = e(1:4) ) e_sum = sum ( e(1:4) ) e(1:4) = e(1:4) / e_sum ! ! We may take the values E(1:3) as being the barycentric ! coordinates of the point. ! p(1:3,j) = e(1:3) end do return end subroutine tetrahedron_unit_sample_02 ( p_num, p ) !*****************************************************************************80 ! !! tetrahedron_unit_sample_02() selects points from the unit tetrahedron. ! ! Discussion: ! ! The unit tetrahedron has vertices (1,0,0), (0,1,0), (0,0,1), (0,0,0). ! ! The sampling is uniform. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Claudio Rocchini, Paolo Cignoni, ! Generating Random Points in a Tetrahedron, ! Journal of Graphics Tools, ! Volume 5, Number 5, 2000, pages 9-12. ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Output, real ( kind = rk ) P(3,P_NUM), the points. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer p_num real ( kind = rk ) c(3) integer j real ( kind = rk ) t real ( kind = rk ) p(3,p_num) do j = 1, p_num call random_number ( harvest = c(1:3) ) if ( 1.0D+00 < c(1) + c(2) ) then c(1) = 1.0D+00 - c(1) c(2) = 1.0D+00 - c(2) end if if ( 1.0D+00 < c(2) + c(3) ) then t = c(3) c(3) = 1.0D+00 - c(1) - c(2) c(2) = 1.0D+00 - t else if ( 1.0D+00 < c(1) + c(2) + c(3) ) then t = c(3) c(3) = c(1) + c(2) + c(3) - 1.0D+00 c(1) = 1.0D+00 - c(2) - t end if p(1:3,j) = c(1:3) end do return end subroutine tetrahedron_unit_sample_03 ( p_num, p ) !*****************************************************************************80 ! !! tetrahedron_unit_sample_03() selects points from the unit tetrahedron. ! ! Discussion: ! ! The unit tetrahedron has vertices (1,0,0), (0,1,0), (0,0,1), (0,0,0). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Greg Turk, ! Generating Random Points in a Triangle, ! in Graphics Gems, ! edited by Andrew Glassner, ! AP Professional, 1990, pages 24-28. ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Output, real ( kind = rk ) P(3,P_NUM), the points. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer p_num real ( kind = rk ) a real ( kind = rk ) b real ( kind = rk ) c real ( kind = rk ) d real ( kind = rk ) e real ( kind = rk ) f real ( kind = rk ) g integer j real ( kind = rk ) p(3,p_num) real ( kind = rk ) r(3) do j = 1, p_num call random_number ( harvest = r(1:3) ) e = r(1)**(1.0D+00/3.0D+00) f = sqrt ( r(2) ) g = r(3) a = 1.0D+00 - e b = ( 1.0D+00 - f ) * e c = ( 1.0D+00 - g ) * f * e d = g * f * e p(1,j) = a p(2,j) = b p(3,j) = c end do return end subroutine tetrahedron_unit_sample_04 ( p_num, p ) !*****************************************************************************80 ! !! tetrahedron_unit_sample_04() selects points from the unit tetrahedron. ! ! Discussion: ! ! The unit tetrahedron has vertices (1,0,0), (0,1,0), (0,0,1), (0,0,0). ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 16 August 2009 ! ! Author: ! ! John Burkardt ! ! Reference: ! ! Reuven Rubinstein, ! Monte Carlo Optimization, Simulation, and Sensitivity ! of Queueing Networks, ! Krieger, 1992, ! ISBN: 0894647644, ! LC: QA298.R79. ! ! Parameters: ! ! Input, integer P_NUM, the number of points. ! ! Output, real ( kind = rk ) P(3,P_NUM), the points. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer p_num real ( kind = rk ) e(4) integer j real ( kind = rk ) p(3,p_num) ! ! The construction begins by sampling DIM_NUM+1 points from the ! exponential distribution with parameter 1. ! do j = 1, p_num call random_number ( harvest = e(1:4) ) e(1:4) = - log ( e(1:4) ) p(1:3,j) = e(1:3) / sum ( e(1:4) ) end do return end subroutine tetrahedron_volume ( tet_xyz, volume ) !*****************************************************************************80 ! !! tetrahedron_volume() computes the volume of a tetrahedron in 3D. ! ! Licensing: ! ! This code is distributed under the MIT license. ! ! Modified: ! ! 30 December 2004 ! ! Author: ! ! John Burkardt ! ! Parameters: ! ! Input, real ( kind = rk ) TET_XYZ(3,4), the coordinates of the vertices. ! ! Output, real ( kind = rk ) VOLUME, the volume of the tetrahedron. ! implicit none integer, parameter :: rk = kind ( 1.0D+00 ) integer, parameter :: dim_num = 3 real ( kind = rk ) a(4,4) real ( kind = rk ) r8mat_det_4d real ( kind = rk ) tet_xyz(dim_num,4) real ( kind = rk ) volume a(1:dim_num,1:4) = tet_xyz( 1:dim_num,1:4) a(4,1:4) = 1.0D+00 volume = abs ( r8mat_det_4d ( a ) ) / 6.0D+00 return end