fortran2003

Generic type-bound procedures with procedure arguments

本小妞迷上赌 提交于 2019-12-10 17:34:15
问题 I am trying to write a generic type-bound procedure that takes different callback functions as parameters. When compiling following code (with ifort 12.1.3) I get the warning below: module test type :: a_type contains procedure :: t_s => at_s procedure :: t_d => at_d generic :: t => t_s,t_d end type a_type abstract interface integer function cb_s(arg) real(4) :: arg end function cb_s integer function cb_d(arg) real(8) :: arg end function cb_d end interface contains subroutine at_s(this,cb)

Fortran polymorphism, functions and allocation

送分小仙女□ 提交于 2019-12-10 13:23:21
问题 I am quite a beginner in OOP with Fortran and I am trying to write a program with procedures that deal with polymorphic variables as arguments. Although my original code is much more complicated (many procedures, several derived types etc), I could isolate a simple example of my problem, say: I have a procedure that copies a polymorphic variable and slightly modifies this copy. I was able to successfully write my test program using a subroutine : MODULE my_module type :: my_type real :: data

How to specify procedures to be executed depending on data type of variable

三世轮回 提交于 2019-12-10 11:27:11
问题 I'm writing a module that access images and reads pixel values. The values in the images are usually of different data types ( integer(2) , integer(4) , ...). Up to now, type image is defined in the following way: type, public :: image logical :: initialized = .false. character(256) :: path = "" ! Path to image integer :: dimensions(3) = -1 ! Dimensions of image integer :: datatype = 0 ! Datatype contains procedure :: initialize procedure :: pxvalues procedure :: getMeta end type image My

How to access (dynamically allocated) Fortran arrays in C

五迷三道 提交于 2019-12-09 05:05:57
问题 My main question is why arrays do such weird things and whether there is any way at all to do the following in a "clean" way. I currently have a C program foo.c interfacing a Fortran program bar.f90 via dlopen/dlsym , roughly like in the code below: foo.c: #include <dlfcn.h> #include <stdio.h> int main() { int i, k = 4; double arr[k]; char * e; void * bar = dlopen("Code/Test/bar.so", RTLD_NOW | RTLD_LOCAL); void (*allocArray)(int*); *(void **)(&allocArray) = dlsym(bar, "__bar_MOD_allocarray")

Is it possible to declare a matrix as a derived type in Fortran?

非 Y 不嫁゛ 提交于 2019-12-08 06:46:01
问题 Is it possible to declare a matrix as a derived type in Fortran? For example, can something be done so that the call class(four_by_four_matrix) :: A call A%inv is valid? Where inv is declared as a procedure of four_by_four_matrix ? 回答1: The answer to the question "is it possible?" is yes, it is possible. Just put a 2d array into your type: type four_by_four_matrix real(rp) :: arr(4,4) contains procedure :: inv => four_by_four_matrix_inv end type contains subroutine four_by_four_matrix_inv

Have a function in fortran return a reference that can be placed on the left-hand-side of an assignment

拥有回忆 提交于 2019-12-07 17:40:38
问题 As stated in the title, I want to directly modify data that I access through a pointer retrieved from a function. Having a reference returned by a function appearing on the l.h.s. of an assignment(=) is no issue in C++ but the following minimal example in fortran errors out: module test_mod implicit none integer, target :: a=1, b=2, c=3 ! some member variables contains function get(i) integer, pointer :: get integer, intent(in) :: i select case (i) case (1) get => a case (2) get => b case (3)

Have a function in fortran return a reference that can be placed on the left-hand-side of an assignment

独自空忆成欢 提交于 2019-12-05 20:15:48
As stated in the title, I want to directly modify data that I access through a pointer retrieved from a function. Having a reference returned by a function appearing on the l.h.s. of an assignment(=) is no issue in C++ but the following minimal example in fortran errors out: module test_mod implicit none integer, target :: a=1, b=2, c=3 ! some member variables contains function get(i) integer, pointer :: get integer, intent(in) :: i select case (i) case (1) get => a case (2) get => b case (3) get => c end select end function get end module test_mod program test use test_mod implicit none

Type bound procedure overloading in Fortran 2003

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 13:02:28
I've been programming in Java for a few years; however, I'm now taking a course which uses Fortran as example code (77 standard). Although I've always viewed Fortran as an ancient language, I decided to try out the latest implementation of the 2003 standard using the gfortran compiler to see its merits for myself. So far, I've been surprised with the modern features, but I have run into one issue which is demonstrated by the example below. module mod1 type type1 real :: x real :: y contains procedure :: compute end type type1 contains subroutine compute(this) class(type1) :: this this%y = this

Nested derived type with overloaded assignment

Deadly 提交于 2019-12-04 04:43:27
I have a derived type ( wrapper ) containing an other derived type ( over ). For the latter the assignment operator have been overloaded. As the assignment of derived types happens per default componentwise, I'd expect that assigning two instances of wrapper would invoke the overloaded assignment for over at some point. However, using the program below, it does not seem to be the case. The overloaded assignment is only invoked if I also overload the assignment for wrapper containing an explicit assignment between instances of over (by uncommenting the commented code lines). Why? I find it

How to access (dynamically allocated) Fortran arrays in C

倖福魔咒の 提交于 2019-12-03 04:00:49
My main question is why arrays do such weird things and whether there is any way at all to do the following in a "clean" way. I currently have a C program foo.c interfacing a Fortran program bar.f90 via dlopen/dlsym , roughly like in the code below: foo.c: #include <dlfcn.h> #include <stdio.h> int main() { int i, k = 4; double arr[k]; char * e; void * bar = dlopen("Code/Test/bar.so", RTLD_NOW | RTLD_LOCAL); void (*allocArray)(int*); *(void **)(&allocArray) = dlsym(bar, "__bar_MOD_allocarray"); void (*fillArray)(double*); *(void **)(&fillArray) = dlsym(bar, "__bar_MOD_fillarray"); void (