Silverfrost Forums

Welcome to our forums

Learning Clearwin

5 Feb 2025 4:58 #31903

Hello

I built a test code using the tutorial as a template:

winapp

module schwarzschildmodule
    use clrwin
    implicit none
   
    real(kind=2) :: mass = 1.0
    real(kind=2), parameter :: sol = 1.989e30   ! 1 solar mass in kg
    real(kind=2), parameter :: velocity_of_light = 2.99792458e8   ! m/s
    real(kind=2), parameter :: gravitational_constant = 6.67430e-11  ! m^3 kg^1 s^2
    character(50) :: str = ''

contains

    function Schwarzschild_radius(mass) result(Rs)
        real(kind=2), intent(in) :: mass
        real(kind=2) :: Rs
        Rs = 2 * gravitational_constant * mass / velocity_of_light**2
    end function Schwarzschild_radius

    integer function calculate_radius()
        implicit none
        real(kind=2) :: SRadius
        
        if (mass <= 0) then
            str = 'Mass must be positive!'
        else
            SRadius = Schwarzschild_radius(mass * sol)
            write(str, '('Schwarzschild radius: ', F15.5, ' meters')') SRadius
        end if
        
        call window_update@(str)
        calculate_radius = 1
    end function calculate_radius

    integer function about()
        implicit none
        integer :: iw
        
        iw= winio@('%ca[about Schwarzschild calculator]&')
        iw= winio@('%fn[times new roman]%ts%bf%cnSchwarzschild Radius Calculator&', 2.0d0)
        iw= winio@('%ts%4nl&', 1.0d0)
        iw= winio@('%cnProgram to calculate Schwarzschild radius for a given mass in solar masses%2nl&')
        iw= winio@('%ts%tc%cn%bfclearwin+&', 1.5d0, rgb@(255,0,0))
        iw= winio@('%tc%sf%2nl%cnby&', -1)
        iw= winio@('%2nl%cnSalford Software&')
        iw= winio@('%2nl%cn%9`bt[ok]')
        
        about = 1
    end function about

end module schwarzschildmodule

!==================================================================

program schwarzschild_calculator

    use schwarzschildmodule
    implicit none
    
    integer :: iw

    iw= winio@('%ca[Schwarzschild Radius Calculator]&')
    iw= winio@('%mn[&file[e&xit]]&', 'exit')
    iw= winio@('%mn[&help[&about Schwarzschild calculator]]&', about)
    iw= winio@('%il&', 0, 1000)
    iw= winio@('Mass in solar masses: %rf&', mass)
    iw= winio@('%ta%`^bt[Calculate Radius]&', calculate_radius)
    iw= winio@('%2nl%tc[red]%ob%42st%cb', str)


end program schwarzschild_calculator

How can I add several function outputs to the final output? Is it a call back function for each module function, and is there an easy way of grouping things to avoid repetition?

Lester

5 Feb 2025 10:48 #31904

Since you only compute a single value, there appears to be only one output, given the input 'mass'.

I'm not understanding the question, and would be glad to help, as would most folks on this forum.

As an aside, in some of my windows, I will click on a single button to start a function, and have several variables that contain intermediate results displayed. as or after the function executes. Is that what you are asking?

Bill

5 Feb 2025 11:14 #31905

Lester,

Like Bill, I'm struggling to understand what you are trying to achieve.

However, I played about with your code and it may be that this provides something like what you are looking for? One 'Calculate' button, %rb radio buttons to select one or more different functions, with the output written to a %re edit box.

The output to the %re is determined by the status of the control values for %rb, and the single callback simply works serial on each in turn, doing the necessary calculations (which may be further function calls) if required.

winapp
module schwarzschildmodule
    use clrwin
    implicit none
    real(kind=2) :: mass = 1.0
    real(kind=2), parameter :: sol = 1.989e30   ! 1 solar mass in kg
    real(kind=2), parameter :: velocity_of_light = 2.99792458e8   ! m/s
    real(kind=2), parameter :: gravitational_constant = 6.67430e-11  ! m^3 kg^1 s^2
    character(50) :: str = ''
    character(len=32*1024) :: buffer = ''
    integer :: sr_control = 1, sin_control = 0, cos_control = 0, random_control = 0
contains

    integer function calculate_cb()
        implicit none
        real(kind=2) :: val
        buffer = ''
        if (SR_control .eq. 1) then
          if (mass <= 0) then
              str = 'Mass must be positive!'
          else
              val = 2 * gravitational_constant * mass * sol / velocity_of_light**2
              write(str, '('Schwarzschild radius: ', F15.5, ' meters')') val
          end if
          call append2buffer
        end if
        if (SIN_control .eq. 1) then
          val = sin(mass)
          write(str, '('Sin: ', F15.5)') val
          call append2buffer
        end if
        if (COS_control .eq. 1) then
          val = cos(mass)
          write(str, '('Cos: ', F15.5)') val
          call append2buffer
        end if
        if (RANDOM_control .eq. 1) then
          call random_number(val)
          write(str, '('Random', F15.5)') val
          call append2buffer
        end if
        call window_update@(buffer)
        calculate_cb = 2
    end function calculate_cb

    subroutine append2buffer
      if (len_trim(buffer) .eq. 0) then
        buffer = str
      else 
        buffer = trim(buffer)//achar(13)//achar(10)//str
      end if    
    end subroutine append2buffer
end module schwarzschildmodule

program schwarzschild_calculator
    use schwarzschildmodule
    implicit none
    integer :: iw
    iw= winio@('%ca[Schwarzschild Radius Calculator]&')
    iw= winio@('%mn[&file[e&xit]]&', 'exit')
    iw= winio@('%il&', 0, 1000)
    iw= winio@('Mass in solar masses: %rf%2nl&', mass)
    iw= winio@('%rb[Schwarzschild Radius]%nl&',SR_control)
    iw= winio@('%rb[Sin]%nl&',SIN_control)
    iw= winio@('%rb[Cos]%nl&',COS_control)
    iw= winio@('%rb[Random]%2nl&',Random_control)
    iw= winio@('%cn%`^tt[Calculate]%2nl&', calculate_cb)
    iw= winio@('%42.10re[VSCROLLBAR]',buffer)
end program schwarzschild_calculator
6 Feb 2025 6:37 #31906

Users who are getting started with ClearWin+ should take a look at the book 'Fortran and the Art of Windows Programming' written by Eddie Broomhead.

This can be found in pdf form, installed in the folder \Silverfrost\FTN95\doc.

6 Feb 2025 12:45 #31907

Thanks Ken for the code idea, that works for the basic structure I was looking to build.

Found the guide Paul, that will be a good start; a bit of a learning curve.

I did try to load the original code up but it did not work, so guess there must be limit.

Lester

6 Feb 2025 8:59 #31908

and here.... https://www.silverfrost.com/ftn95-help/art_of_programming/contents.aspx

7 Feb 2025 9:30 #31909

There is also the Clearwin Examples folder at the Silverfrost GitLab folder. Link on the RHS of the Silverfrost homepage which points to https://gitlab.com/silverfrost

I have found the content of the Controls Demo folder particularly helpful.

Once in the folder, the entire contents can be downloaded as a zip file via the blue code button on the RHS.

Once downloaded, you can compile and run the single demo program and study the source code to understand what the controls do.

7 Feb 2025 10:38 #31910

Thanks Ken for the pointer to the Clearwin examples folder; that really helps.

Have updated the existing code with sliders for mass and velocity which worked! It is a start, but interesting to learn the basics. Fortran is interesting to code just for interest and learning new methods.

Is Dropbox the way to share large code samples? The code block here seems limited.

Lester

7 Feb 2025 12:54 #31911

Lester,

Yes, there is a limit of about 70 lines of code that can be posted here.

Dropbox file sharing is one alternative, which seems to work well.

8 Feb 2025 4:33 #31913

I would also recommend reading Eddie's book 'Fortran and the Art of Windows Programming'.

It has been a few/many years since I last wrote clearwin+ programs, and the discussion of the approach to the %xx options is a useful help.

I started with his homer.f90 example program in 'first steps' and have just added more features. ( .f90 helps with longer coding lines! )

I actually spent a year first developing the data structures and analysis phase. Now I can see how to improve the analysis !

8 Feb 2025 4:36 #31914

Quoted from Kenneth_Smith Lester,

Yes, there is a limit of about 70 lines of code that can be posted here.

Dropbox file sharing is one alternative, which seems to work well.

Could the forum have a scrollable code box with more capacity ?

9 Feb 2025 3:30 #31919

Robert, thanks for the on-line link!

Please login to reply.