Silverfrost Forums

Welcome to our forums

Use constants from module in select case

14 Jan 2025 11:38 #31813

Hello,

I have a module which defines a series of constants and functions, but not sure how they can be accessed via a select case construct.

A snippet of the module:

module GR_functions
    implicit none

! NIST CODATA values (2022)
	real, parameter :: sol = 1.989e30   ! 1 solar mass kg
    real, parameter :: velocity_of_light = 2.99792458e8   ! m/s
    real, parameter :: gravitational_constant = 6.67430e-11  ! m^3 kg^1 s^2
    real, parameter :: Planck_constant = 6.62607015e-34    ! J·s (h)
    real, parameter :: reduced_Planck_constant = 1.054571817e-34    ! J·s (hbar)
    real, parameter :: Boltzmann_constant = 1.380649e-23 ! J -K
    real, parameter :: Stefan_Boltzmann_constant = 5.670374419e-8 ! W m-2 K-4
	real, parameter :: AU = 1.495978707e11   ! Astronomical unit (m)
    real, parameter :: LY = 9.460730472580800e15 ! Light year (m)
    real, parameter :: PC = 3.26156*LY ! Parsec (m)
    integer, parameter :: days_in_year = 365
    integer, parameter :: seconds_in_day = 86400
	integer, parameter :: seconds_in_year = 31536000
	
    contains

    function emc2(mass) result(E)
        real, intent(in) :: mass
        real :: E
        E = mass * velocity_of_light**2 ! Joules
    end function emc2

A short section of the main code:

program rs_test

use GR_functions

implicit none

integer :: choice, real_time
real :: mass, velocity, distance
real :: eqn_result

    print *, 'Choose a function to execute:'
    print *, '1 - E = mc^2'
    print *, '2 - Relativistic time dilation'
    print *, '3 - Schwarzschild radius'
    print *, '4 - Photon sphere'
    print *, '5 - Black hole surface area'
    print *, '6 - Power radiated by black hole'
    print *, '7 - Gravitational time dilation'
    print *, '8 - Photon deflection'
    print *, '9 - Hawking radiation'
    print *, 'Enter your choice (1-9):'
    read *, choice

    ! Use select case to call the selected function

    select case(choice)
    case(1)
        ! E = mc^2 requires mass as input    
        print *, 'Enter mass (in kg):'    
        read *, mass
        eqn_result = emc2(mass)
        print *, 'Energy (E = mc^2): ', eqn_result

My query is how one can use a variable or constant name at the user input where mass is a real value. A runtime error occurs typing sol for example as it is not recognised at this point.

Any pointers?

Thanks

Lester

14 Jan 2025 1:27 #31814

Think I figured this out?

    case(1)
        ! E = mc^2 requires mass as input    
        print *, 'Enter mass as N solar masses:'    
        read *, mass
        mass = mass * sol
        eqn_result = emc2(mass)
        print *, 'Energy (E = mc^2): ', eqn_result

In this case the mass is a multiplier (e.g. n x solar mass), the function then is passed a real value to process.

If there is an easier method it would be good to know. I fixed the constants as double precision.

Thanks

15 Jan 2025 9:08 #31815

In your select case (choice) case (constant_value)

'Choice' can not be a real or complex expression, but must be integer, character or (possibly) logical. It may be an expression that evaluats to integer, character or logical value. It may be a expression or variable that is in a module. It should not be an 'integer, parameter', as that is a constant that can not vary.

'constant_value' must be a constant or parameter that is the same type as 'choice', or a range of values as provided by the case 'selector' syntax. It can not be a variable.

The most common use of Select Case is where choice is either an integer or character expression and constant_value is a constant of the same kind.

There is also 'case default' whch covers any cases that have not been previously selected by case.

You should be able to find typical examples of select case.

for example

   select case ( i+j )
      case ( -4:-1 )     !  small negative
         ii = -1
      case ( 1:4 )   ! small +ve
         ii = 1
      case ( 0 )     !  zero
         ii = 0
      case default     ! value outside -4:4
         ii = -9
   end select

I hope this is what you asked ?

Alternatively, 'mass * sol' may be a value too large for a (4-byte) real constant, so you will need to use an 8-byte real, such as real*8 mass. a (4-byte) real constant is limited to 3.4e+38

15 Jan 2025 1:46 #31818

Thanks for the information John

Please login to reply.