/r/fortran

Photograph via snooOG

The choice for high performance (scientific) computing!

Suggested Links:

Other sub-reddits:

If your submission does not appear, please message the moderators!

IRC: freenode, #fortran

/r/fortran

7,133 Subscribers

15

My Fortran implementation of quake3 is being sabotaged by safety or something.

The two errors I'm getting, are that my pointers aren't allowed to go between different data types, which I need them to. And, that the constant is too big to store, even tho its a 64 bit integer.

PROGRAM Q_sqrt
  IMPLICIT NONE

  REAL(4) :: input
  INTEGER(4) :: i
  REAL(4) :: x
  REAL(4) :: x2
  REAL(4) :: y

  PRINT *, 'Number:'
  READ "(f10.7)", input

  x2 = input * 0.5
  y = input
  i = TRANSFER(y, i)
  i = 1597463007 - RSHIFT(i,1)
  x = TRANSFER(i, x)

  x = x * (1.5 - (x2 * x * x))

  PRINT "(f10.7)", x
END PROGRAM Q_sqrt

Edit: so I've updated the code with Transfers and made the integer 32bit, but all I get in the output is 0.000000.

Edit 2: Thanks for the help, y'all. I got working by making all the variables 32 bit and I was doing the Newton equation wrong.

11 Comments
2024/05/05
21:17 UTC

7

runtime error : end of file please help

4 Comments
2024/05/04
14:18 UTC

2

Program with function

Hello,

I wrote a program for area perimeter of circle. I'm getting error. The program is:

function circle(r)

real :: r, A, c

pi = 3.14159

A = pi*r**2

c = 2.0pir

end function circle(r)

the error is: syntax error in END FUNCTION statement at (1)

Request help in finding error & fixing

12 Comments
2024/05/03
06:23 UTC

20

PRIMA has got a Python interface

PRIMA now has an official Python interface, thanks to the huge efforts of Nickolai Belakovski. It talks to Python via pybind11 and its C API instead of using F2PY.

I hope PRIMA will provide an example of binding modern Fortran libraries with Python.

Concerning Python, the next steps of PRIMA will be


BTW, Nickolai is truly outstanding as a person and a programmer / engineer. He worked for amazing companies including Bloomberg and Tesla. If you search for Nickolai Belakovski, you can find the following interesting article about him:

Rocket Science: From Russia and Bloomberg, With Love,

and the following one by him:

The little-known Soviet mission to rescue a dead space station

It is a privilege to have him contributing to PRIMA.

See Nickolai's homepage for more (although it does not seem up to date).

1 Comment
2024/05/01
14:42 UTC

36

Can we disallow screenshots of text?

This subreddit has the highest frequency of screenshots of terminal output / code of any programming subreddit I’m in. It’s embarrassing to our community that people can’t do the proper thing and share text as text. I propose to make it against the rules so such posts can be removed and those posting them can learn to solicit feedback constructively.

4 Comments
2024/04/30
19:35 UTC

10

how to stop the subroutine modifying its arguments

Noob here, one of my subroutine (one_mcs) takes in temparature as the argument. Its not supposed to modify the value of temparature, but it does(because, at line 66, when i write temparature into a .dat file, its supposed to vary between 0.5 and 4 in steps of 0.002, but its all wacky and fluctuates like crazy and become negative randomly :( ) How do i fix this? Thanks in advance :)

Here is the code, the subroutine one_mcs starts at line 80.

7 Comments
2024/04/29
16:56 UTC

22

CrossBLAS Project: A Modern Fork of BLAS for Fortran Enthusiasts

Hey folks,

I'm excited to share with you all a project I've been working on called CrossBLAS. It's a modern fork of the original netlib BLAS code base, now revamped for better compatibility and cleanliness.

What's CrossBLAS? CrossBLAS aims to bring BLAS's robustness to a wider range of platforms and compilers. I've ensured it compiles seamlessly with Intel's ifort.exe on Windows and GNU's GFortran on WSL Ubuntu. The code is now more organized and less cluttered, making it easier for developers to navigate and contribute.

Current Status: While the core functionality is up and running, there are still several issues to tackle:

  • CTests are not operational yet.
  • The Config.cmake file generation is a work in progress.

Call for Collaboration: This is where I need your help! I'm calling on all Fortran aficionados and coding enthusiasts to test CrossBLAS. Your feedback is invaluable, and I encourage you to open issues on the GitHub repository to discuss any problems or suggestions you might have. Of course, pull requests are more than welcome—let's make CrossBLAS even better together!

Get Involved: Check out the repository, give it a spin, and let's collaborate. Your contributions will improve CrossBLAS and support the broader Fortran community.

Thank you for your time, and happy coding!

4 Comments
2024/04/29
00:19 UTC

14

Not able to solve this issue

So I am doing some calculation using fortran MPI code and I'm getting this error.

8 Comments
2024/04/24
06:35 UTC

7

Fortran Compiler for Windows 11

Hi, i need some help.
I didn't find for Fortran Compiler for Windows 11 system. Last year i used Force Fortran and my system was windows 10 but today i use win 11 and force fortran didn't work this system. Have you reccomendation compiler program or how can i use fortran in visual studio?

7 Comments
2024/04/18
17:53 UTC

3

Bind(c) effect on variables in subroutines and functions

I am curious if anyone has insight into any effect the use of c-bindings has on the variables used within a subroutine or function. I am aware that the use of bind(c) will generate a name for the function/subroutine that will be callable by both c and fortran. Specifically does the use of bind(c) effect the data type of a variable within the subroutine/function? For example here is a basic example of use:

subroutine add(a,b) bind(c) integer :: a,b,result result = a + b print *, result end subroutine add

I may be answering my own question here, but I implemented the above example with and without bind(c), and stepped through it via gdb, using ptype to examine the data type. I found that the data type seemingly does not change with the addition of bind(c). I am aware that one can change the type through either the kind parameter or by names constants from the iso_c_binding intrinsic module. I guess mainly I am looking to see if this is correct, that the use of bind(c) alone will only affect the name of the function/subroutine object and not the variables contained within?

4 Comments
2024/04/18
14:37 UTC

0

Ai Coding Assistant

Are there are AI coding assistant that will help with fortran?

7 Comments
2024/04/18
02:15 UTC

3

What's your opinion on FORD for code documentation and for static pages pages?

I am working on a code which has minimal documentation. The code has been around and growing for around 8 years so it's getting big. We are thinking about starting documentation and Ford seems like an option. This is the first documented I have ever used but building the docs seems to take some time. It takes 30 seconds while setting the graph to off is this normal? For now I am interested in setting up some tutorials using the static pages. Ford seem okay and does the job well but are there any other alternatives?
I found sphinx documenter online as well but is it any good? I haven't found any examples of the HTML output online.

5 Comments
2024/04/17
11:12 UTC

7

Cannot for the life of me figure out how scope works.

Beginner here,using Fortran90, i have my code looking something like this

begin program izing

!body that uses one_mcs(). and one_mcs() itself uses find_energy()

contains

subroutine one_mcs()

!uses find_energy function

end subroutine one_mcs()

real function find_energy()

end function find_energy

end program izing

The issue is i get this error

/usr/bin/ld: /tmp/ccqTTEvT.o: in function `one_mcs.4':

ising2.f90:(.text+0x89d): undefined reference to `find_energy_'

collect2: error: ld returned 1 exit status

I infer that one_mcs subroutine is unable to access find_energy function? why? how do i fix this?

Also should i post the whole code? its like 200 lines

5 Comments
2024/04/16
06:47 UTC

13

Downloading

How can I download fortran90 in my pc win11 pro. I've been trying for days but still have not found the version I have been looking for. The pic is an example of how the version looks

26 Comments
2024/04/12
13:22 UTC

3

help with program on triangle area, perimeter

Hello,

I wrote this following program to find area, perimeter of triangle. I'm using vscode. After I ran gfortran Triangle1.f90 -o Triangle1, it just returned to konsole without promoting to enter values of a,b,c,h. Please guide where it went wrong.

!program for area, perimeter of triangle using trad formula

program Triangle1

implicit none

real :: a, b, c, h, Area, Perimeter

print*, "enter the value of a, b, c, h"

read*, a, b, c, h

Area = (b*h)/2

Perimeter = a + b + c

print*, "Area =", Area
print*, "Perimeter =", Perimeter

end program Triangle1

6 Comments
2024/04/12
07:41 UTC

6

gfortran selection of module procedure based on argument type

Hi All, I am currently searching for some documentation that will explain the method by which a compiler (say gfortran for example) selects the correct form of a function from a generic interface, whose specific forms differ by argument type. I am familiar with how to setup the interface, define the functions, etc. I understand that this process will differ by compiler, and as I am using gfortran, any documentation specific to the gfortran method will be sufficient and greatly appreciated.

3 Comments
2024/04/11
02:10 UTC

8

Seeking Collaborators to Revive JModelica with RADAU5 and More!

Hey folks! 🚀

I’m trying to revive the JModelica project, and I need your help! I am struggling with some tricky bits involving Hairer’s stiff ODE solver, RADAU5, as part of the Assimulo package. If you have experience or interest in numerical methods and Fortran, I could really use your help! If you want to know more about JModelica, you may check out this post I wrote a few years ago.

Here’s the scoop:

  • Current Challenge: I'm facing compilation issues with RADAU5 in the JModelica/Assimulo context. If you’re savvy with Fortran and debugging, your insights would be invaluable!
  • Next Steps: Post-smoothing out these initial bumps, I'm eyeing porting the project to GitHub, integrating it with package managers like fpm, Conan, and vcpkg, and ensuring it’s friendly across different platforms and compilers (think Windows, macOS, Linux, GNU gfortran, LLVM Flang, Intel ifort/ifx).

If you are interested in sharpening your FORTRAN skills and supporting the open-source ecosystem, drop a message here or reach out directly.

8 Comments
2024/04/09
11:58 UTC

9

Coding style: Handling long conditionals?

Out of interest, how are you handling situations, where a conditional expression is too long for one line?

For instance, I came across this situation (negative values were used to indicate configuration values, that are not set):

IF(simulation_config%time_increment > 0) THEN
    time_increment = simulation_config%time_increment
ELSE IF( &
    simulation_config%reference_velocity > 0 .AND. &
    simulation_config%distance_increment > 0       &
) THEN
    time_increment = simulation_config%distance_increment &
                   / simulation_config%reference_velocity
ELSE
    ! error handling code for missing configuration
END IF

Note the ELSE IF. If I would entirely leave the code formatting to Emacs's indentation functions, I'd get

IF(simulation_config%time_increment > 0) THEN
    time_increment = simulation_config%time_increment
ELSE IF( &
          simulation_config%reference_velocity > 0 .AND. &
          simulation_config%distance_increment > 0       &
          ) THEN
    time_increment = simulation_config%distance_increment &
              / simulation_config%reference_velocity
ELSE
    ! error handling code for missing configuration
END IF

which I find awful for readability. My solution looks better to me, but now I depend on manual code formatting.

Note that this question has come up often for me, across multiple languages, including Python. Coding guidelines often omit such cases too, and code formatting tools are hit-and-miss on that issue.

It also comes up for other constructs, e.g.

ASSOCIATE(v0 => simulation_config%reference_velocity, &
          dx => simulation_config%distance_increment)
    time_increment = dx / v0
END ASSOCIATE

has the same issue of making the code structure less clear, as does the indented ) THEN line.

9 Comments
2024/04/05
09:45 UTC

7

open a file, read a file, asign a variable?

hi! I'm new to Fortran and I'm trying to understand these lines of code:

open(unit=11, name='initcond.dat')

open(unit=12, name='initGAS.dat')

open(unit=21, name='nm-Dark-A.dat')

open(unit=22, name='prho-Dark-A.dat')

open(unit=23, name='eR-Dark-A.dat')

open(unit=24, name='ji-Dark-A.dat')

open(unit=25, name='f-Dark-A.dat')

open(unit=26, name='estrella-Dark-A.dat')

open(unit=27, name='divergencias-Dark-A.dat')

read(11,*) xo

read(11,*) no

read(11,*) n1o

read(11,*) mo

read(11,*) m1o

read(11,*) Ro

read(11,*) DRo

read(11,*) Mso

read(11,*) delta

close(11)

read(12,*) rho

read(12,*) nHS

read(12,*) mHS

read(12,*) f1hoy

close(12)

that's just part of the code; I have two of these files (I know I'd need to have acces to the others in order to properly run the code).

what I understand up until now is that to open the files in the program, I asign a number (e.g. " unit = 11 ") to a specific file (e.g. " name='initcond.dat' ").

what I don't understand is the " read " part.

I'm guessing the number (e.g. " 11 " in " read(11,*) xo ") refers to the file I want to access. and I thought that writing a variable next to the statement " read(11,*) " meant that it assigned a value in the file associated with " 11 " .

but why does this code do that 9 times? the file I have for "initcond.dat" is the following text file:

0.d0 Radio inicial

1.d0 gtt central

0.d0 gtt' central

1.d0 grr central

0.d0 grr' central

170.d0 172.5d0 8.93108d0 R central

0.d0 Derivada del escalar de curvatura

0.d0 Masa_s central

1d-6 delta

if I follow my reasoning, I'd think that it reads the first line, and then it asigns a variable " xo " (but to what? is it taking " xo = 0.d0 Radio inicial "? or just " xo = 0.d0 "? how does it know what to assign to what?).

but then I think that the next line of code does the same thing to the first line of the " .txt " file so it doesn't really read each line of the " .txt " file and assign a variable to each one of the values in the lines of "initcond.dat".

could someone please explain this to me?

(also what does the asterisk in " read(11,*) xo " stand for?)

4 Comments
2024/04/04
10:57 UTC

38

If you are still using Fortran 77, why?

In the recent thread

https://www.reddit.com/r/fortran/comments/1bmn0xj/most_popular_programming_languages_listing/

a few people mentioned that they are still programming in Fortran 77. May I ask why? There are automatic tools to translate fixed source form to free. You can use a modern Fortran compiler such as gfortran to compile old code, perhaps with the std=legacy option, and you can use features such as allocatable arrays, array operations, derived types, and DO-END DO etc. in new code. (DO-END DO was not part of the F77 standard, but most compilers did support it, and much Fortran-77-style code does use it.)

What are the institutional constraints keeping some projects in Fortran 77?

34 Comments
2024/03/29
13:24 UTC

7

could it be possible for a port of PLPlot for Fortran to work in vcpkg?

Im someone who, although is horrified with the language of Fortran after coming from a basic knowledge of c++, really wants to be able to use something like PLPlot for Fortran to make some graphs. vcpkg does have PLPlot but it's for c/c++ only. I only want to know if it'd be possible to make some kind of port for a plplotFortran to use and inegrate into VS 2022. I know it sounds ambittious but just consider it.

7 Comments
2024/03/25
21:37 UTC

37

Most popular programming languages listing - Fortran use cases today?

I am working in Data Science and for my own more intellectual than (at this phase) practical interests, I am curious learning some older basic language. Fortran is found, let's say relatively high the current listing of top languages

https://redmonk.com/sogrady/2024/03/08/language-rankings-1-24/

Do you see Fortran as sciences/academia only or if there are more recent applications in industry, what are they like - can you see there could be Data Science cases on your table when working on Fortran?

32 Comments
2024/03/24
15:11 UTC

5

Big size of CLASS(*) variables?

I am trying to make sense of the size of user-derived types when using a class(*) entry.

Given the following test program

program main
    type int
        integer, pointer :: int
    end type int
    type ptr
        class(*), pointer :: ptr
    end type ptr
    type(int) intval
    type(ptr) ptrval

    print *, "type(int) size: ", sizeof(intval)
    print *, "type(ptr) size: ", sizeof(ptrval)
end program main

I obtain the output

>> ifort b.f90 -O3 -o b.bin
>> ./b.bin
 type(int) size:                      8
 type(ptr) size:                    128

>> gfortran b.f90 -O3 -o b.bin
>> ./b.bin
 type(int) size:                     8
 type(ptr) size:                    24

I do understand, that the polymorphic pointer needs more data. It is not clear to me however, why with Intel Fortran it results in 128 bytes, and just 24 bytes with GFortran. Especially when I look at the binary contents via TRANSFER and find mostly just zeros (extended source code under https://pastebin.com/jS5MGApC):

>> ifort b.f90 -O3 -o b.bin
>> ./b.bin
type(int) size: 8
type(ptr) size: 128

type(int) bin dump (@ indicates zero byte):
001-016   28 D2 4B 00 00 00 00 00                          (.K@@@@@        

type(ptr) bin dump (@ indicates zero byte):
001-016   28 D2 4B 00 00 00 00 00 04 00 00 00 00 00 00 00  (.K@@@@@.@@@@@@@
017-032   00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00  @@@@@@@@.@@@@@@@
033-048   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  @@@@@@@@@@@@@@@@
049-064   98 79 49 00 00 00 00 00 00 00 00 00 00 00 00 00  .yI@@@@@@@@@@@@@
065-080   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  @@@@@@@@@@@@@@@@
081-096   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  @@@@@@@@@@@@@@@@
097-112   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  @@@@@@@@@@@@@@@@
113-128   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  @@@@@@@@@@@@@@@@

>> gfortran b.f90 -O3 -o b.bin
>> ./b.bin
type(int) size: 8
type(ptr) size: 24

type(int) bin dump (@ indicates zero byte):
001-016   44 AB 59 1C FF 7F 00 00                          D.Y...@@        

type(ptr) bin dump (@ indicates zero byte):
001-016   44 AB 59 1C FF 7F 00 00 A0 11 40 00 00 00 00 00  D.Y...@@..@@@@@@
017-032   00 00 00 00 00 00 00 00                          @@@@@@@@        

What is actually being stored there?

3 Comments
2024/03/15
11:54 UTC

6

Trouble with MPI

Hello! I just installed gfortran, Visual Studio Code, tested it and it works. Then i installed MsMPI (microsoft MPI), but when i compile, the Visual studio has this output:

5 | include "mpif.h"

|

Fatal Error: Cannot open included file 'mpif.h'

compilation terminated.

So, i am here to seek your knowlegde to guide me. (the OS is windows 11 Pro)

25 Comments
2024/03/12
17:01 UTC

8

RSA Fundamentals

Hello all!

As programming practice, I occasionally re-write Comp-Sci tasks in various languages. Lately it’s been Fortran that gets the focus of my practicing. I did want to write this in parallel some how, but as a single core application it works well.

This is a very short bit length implementation of RSA encryption which is not intended for serious use.

It does show off a neat OOP stepping technique of moving the pointers to objects in order to run multiple recursions of the same data.

Link below, enjoy!
https://drive.google.com/drive/folders/1VB-sL1tT8euAoYOcWqkchqueLHZ5nZ0H?usp=drive_link

Have a great day all!
Knarfnarf

0 Comments
2024/03/11
05:20 UTC

7

Compilers for CI of Fortran projects

(Originally posted at Fortran Discourse)

Testing has been playing a central role in the development of PRIMA. The tests are automated by GitHub Actions. The concept of CI (Continuous Integration) and GitHub Actions have been not only life-changing but also eye-opening to me. They enable me to test my code with intensity and extensiveness that are unimaginable otherwise.

GitHub Actions with GitHub-hosted runners are particularly useful. With them, you can test your code in a fresh environment. In addition, you do not need to worry about messing up your computer due to the tests. Most importantly, you have access to a virtually unlimited amount of computers in the cloud rather than being limited by physical computers available in your office.

Which Fortran compilers are available on GitHub Actions with GitHub-hosted runners? In my experience, all major compilers on the market except for nagfor from NAG. Here, even discontinued compilers such as g95 and Oracle sunf95 are included, but IBM Open XL Fortran compiler and Cray Fortran compiler are excluded, as they work only on vendor-specific platforms.

nagfor cannot be used with GitHub-hosted runners due to the special way it manages the license. Each license can only be used on a computer with a specific "Kusari ID". You can deactivate the license on a computer and move it to another one, but you need to send an email to NAG to tell them the new ID and ask for a new license key --- a procedure not automatable.

It is not particularly ideal to be such an exception in a world where everything is moving to the cloud, and even MATLAB is available on GitHub-hosted runners, not to mention other compilers such as Intel ifx, NVIDIA nvfortran, and AOCC flang. I do believe it is something urgent to deal with — even though it is not my business. I once enquired NAG support for the possibility of supporting GitHub-hosted runners in the future, and the response was, unfortunately, ---

"our developers do not view this item as important where our product(s) is concerned."

So, no hope at all. What a pity for such an excellent compiler!



P.S.:

How to make compilers available on GitHub Actions with GitHub-hosted runners? In addition to the fantastic fortran-lang/setup-fortran provided by @awvwgk and other contributors, I use the following scripts, which are maintained at https://github.com/equipez/github_actions_scripts. Note that my scripts are homemade for personal use, and they install only the latest available version of the compilers, whereas you can control the version with fortran-lang/setup-fortran. The scripts are not composed for use on local machines, as they may make unwanted changes to your system.

See my workflow for a concrete example of using these scripts on GitHub-hosted runners.

1 Comment
2024/03/10
06:01 UTC

11

Are there designs in fortran 77 which force people to use common blocks/global variables?

I find that the library ARPACK-NG, an eigensolver written in fortran, has common blocks/global variables in its code, which causes many issues like the lack of thread safety.

The library uses a reverse communication interface, and workspace arrays are provided by the caller.

My question is - we already have workspace arrays, so why not use it for everything? Why bother using common blocks and global variables, when there are so many disadvantages? Is this an issue with old fortran or is it because of something else?

9 Comments
2024/03/09
09:04 UTC

Back To Top