fortran90

Fortran array cannot be returned in function: not a DUMMY variable

▼魔方 西西 提交于 2019-11-29 11:31:57
Being new to Fortran 90 free-form, I would really like to know why the following piece of code snippet would not work: program test2 implicit none !!! A program to practice f90 writing. ! Define double precision data integer, parameter :: dp = kind(1.d0) real(dp) :: a(3), b(3) integer :: i a = (/(i, i=1, 3)/) b = (/(i, i=1, 3)/) write (*, *) m31tensorprod(a, b) contains function m31tensorprod(a, b) real(dp), dimension(3), intent(in) :: a, b real(dp), intent(out) :: m31tensorprod(3, 3) integer :: k1, k2 forall(k1=1:3, k2=1:3) m31tensorprod(k1, k2) = a(k1) * b(k2) end forall return end function

How to make some generic programming in fortran 90/95 working with intrinsic types

感情迁移 提交于 2019-11-29 11:18:29
I would like to program some procedure that will work with different types. I am planning to use the "include" method used in flibs described here and here . I give here a simple exemple. ! -------------------------------------------------------------- ! module data_type type ivalue integer :: v end type type rvalue real(8) :: v end type end module data_type ! -------------------------------------------------------------- ! module imod use data_type, only: T => ivalue include "template.f90" end module imod ! -------------------------------------------------------------- ! module rmod use data

Broadcast array multiplication in Fortran 90/95

拜拜、爱过 提交于 2019-11-29 10:57:36
I was wondering that would there be a better (succinct) way to code this in Fortran? I am trying to multiply each column of a(3, 3) by each value in b(3) . I know in Python there is np.multiply , and not sure about Fortran. !!! test.f90 program test implicit none integer, parameter :: dp=kind(0.d0) real(dp) :: a(3, 3)=reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3]),& b(3)=[1, 2, 3] integer :: i do i = 1, 3 a(:, i) = a(:, i) * b(i) end do write(*, *) a end program test Thanks in advance! The expression a * SPREAD(b,1,3) will produce the same result as your loop. I'll leave it to you and to others

Proper use of the PURE keyword Fortran

一笑奈何 提交于 2019-11-29 09:13:04
I'm currently delving into Fortran and I've come across the pure keyword specifying functions/subroutines that have no side effects. I have a book, Fortran 90/95 by S Chapman which introduces the pure keyword but strangely provides no "good coding practice" uses. I'm wondering how liberally one should use this keyword in their procedures. Just by looking around it's obvious to me that most procedures without side effects don't find it necessary to include the pure keyword. So where is it best used? Only in procedures one wants to completely guarantee have no side effects? Or perhaps in

What does => (equals greater than) mean in Fortran?

十年热恋 提交于 2019-11-29 06:39:38
I'm looking through some old Fortran 90 code and have come across the => symbol: var => item It looks like it's being used for some sort of assignment. Searching Google for "arrow symbol Fortran" or "equals greater than symbol Fortran" gives me no related material. => appears in six contexts as a syntactic element in modern Fortran, many, but not all, related to pointers: pointer assignment ; pointer initialization ; procedure (pointer) declaration ; type-bound procedure declaration ; association ; renaming . There is close connection between most of these. Loosely, in many => can be viewed as

How to check if Fortran array contains value?

此生再无相见时 提交于 2019-11-29 04:59:34
问题 I've seen this asked for other languages, but having just found out how nicely Fortran can handle arrays, I thought there might be an easy way to do this without loops. Currently I'm searching over a 3D array looking at 'nearest neighbours' to see if they contain the letter 'n', and whenever it finds this value, I want it to perform some clusterLabel assignment (which isn't relevant for this question) I wanted to use if(lastNeighArray.eq."n") then...<rest of code> but for obvious reasons it

How can I implement a linked list in fortran 2003-2008

折月煮酒 提交于 2019-11-29 04:43:28
I need to implement a link list data structure for my molecular dynamics code in fortran 2003/2008 I am using the newest fortran compilers (Intel). How do I come about implement the linked list in the best way possible I would prefer a lock-free no wait implementation if possible in Fortran. Thank you. It is easiest if you create a user defined type with your data items and the pointer to the next item. This is assuming a singly-linked list. e.g., type MyList_type integer :: FirstItem real :: SecondItem etc type (MyList_type), pointer :: next_ptr => null () end type MyList_type Then create the

how to call a Fortran90 function included in a module in c++ code?

时光毁灭记忆、已成空白 提交于 2019-11-29 02:34:28
I m including a fortran90 program that is not mine in my C++ project . In the first stept I try to call the function by their name_() and i get the error "undefined reference to mp_mpi_cartesian_init_ "by dispalying the symbol of the obj file (using nm) i found that the function are called by their module as module_function_ so i add the module name and i Get the same problem but between fortran obj such as "Constants.f90:(.text+0x36): undefined reference to __powi4i4" here is the c++ code : #include <iostream> #include <complex> using namespace std; extern"C" { void mod_save_wave_mp_read_it

How to write at specific lines in fortran

人走茶凉 提交于 2019-11-28 14:44:10
I want to copy a file from a folder and write at specific lines of the file using fortran. I am using Windows, GNU fortran compiler. Here is sample file and code. file1.txt 1 * 2 ** 3 *** 4 **** 5 ***** 6 ****** 7 ******* 8 ******** 9 ********* 10 ********** Here is code: I defined some variables. Only if two criteria match (particular variable value and line number), I want to write in the new text in the file. I tried using system command to copy, but it fails. Can anyone tell me correct way of using this? And program got run time error FORMAT present for unformatted text program read

Assumed string length input into a Fortran function

限于喜欢 提交于 2019-11-28 14:19:24
I am writing the following simple routine: program scratch character*4 :: word word = 'hell' print *, concat(word) end program scratch function concat(x) character*(*) x concat = x // 'plus stuff' end function concat The program should be taking the string 'hell' and concatenating to it the string 'plus stuff'. I would like the function to be able to take in any length string (I am planning to use the word 'heaven' as well) and concatenate to it the string 'plus stuff'. Currently, when I run this on Visual Studio 2012 I get the following error: Error 1 error #6303: The assignment operation or