derived-types

Using 2d array vs array of derived type in Fortran 90

血红的双手。 提交于 2019-12-07 02:46:57
问题 Assuming you want a list of arrays, each having the same size. Is it better performance-wise to use a 2D array : integer, allocatable :: data(:,:) or an array of derived types : type test integer, allocatable :: content(:) end type type(test), allocatable :: data(:) Of course, for arrays of different sizes, we don't have a choice. But how is the memory managed between the 2 cases ? Also, is one of them good code practice ? 回答1: In general, you want to use the simplest data structure that

Fortran save procedure as property in derived type

安稳与你 提交于 2019-12-06 16:10:16
Is it possible to store a procedure as a property of a derived type? I was thinking of something along the lines of: module funcs_mod public :: add contains function add(y,z) result (x) integer,intent(in) :: y,z integer :: x x = y + z end function end module module type_A_mod use funcs_mod public :: type_A,set_operator type type_A procedure(),pointer,nopass :: operator end type contains subroutine set_operator(A,operator) external :: operator type(type_A),intent(inout) :: A A%operator => operator end subroutine function operate(A,y,z) result(x) type(type_A),intent(in) :: A integer,intent(in) :

What is the difference between darray and subarray in mpi?

家住魔仙堡 提交于 2019-12-06 01:14:33
I have a parallel I/O project for parallel programming class, and I have to implement derived datatypes. I didn't clearly understand the difference between darray and subarray. Can darray be derived from dynamically allocated arrays or not? And what is the main difference? Subarray lets you describe a single block/slice of a larger multidimensional array. If every MPI task has a single slice/block of a large global array, (or if you are communicating chunks of local arrays between tasks) then MPI_Type_create_subarray is the way to go; the syntax is very straightforward. For solving things like

Array of derived type: select entry

荒凉一梦 提交于 2019-12-05 18:28:57
Currently in my code I have a 2D array integer, allocatable :: elements(:,:) and define some constants integer, parameter :: TYP = 1 integer, parameter :: WIDTH = 2 integer, parameter :: HEIGHT = 3 ! ... integer, parameter :: NUM_ENTRIES = 10 and allocate something like allocate(elements(NUM_ENTRIES,10000)) so I can access elements like write(*,*) elements(WIDTH,100) ! gives the width of the 100th element Now I would like to have not only integer but a mixture of types for every element. So I define a derived type type Element logical active integer type real width ! etc end type and use an

Using 2d array vs array of derived type in Fortran 90

大憨熊 提交于 2019-12-05 08:20:51
Assuming you want a list of arrays, each having the same size. Is it better performance-wise to use a 2D array : integer, allocatable :: data(:,:) or an array of derived types : type test integer, allocatable :: content(:) end type type(test), allocatable :: data(:) Of course, for arrays of different sizes, we don't have a choice. But how is the memory managed between the 2 cases ? Also, is one of them good code practice ? In general, you want to use the simplest data structure that suits your problem. If a 2d rectangular array meets your needs - and for a huge number of scientific computing

How to override method with derived return type in C#?

六月ゝ 毕业季﹏ 提交于 2019-12-02 11:46:52
I want to override a virtual method with a derived class type. What's the current best way to do this? So far I've found two approaches: Use an abstract base class for each derived type; bridge with protected methods. Use a protected implementation with a public accessor. Base case (no solution implemented, Clone always returns base type A1 ): public class A1 { public int X1 { get; set; } public A1(int x1) { this.X1 = x1; } public virtual A1 Clone() { return new A1(X1); } } public class A2 : A1 { public int X2 { get; set; } public A2(int x1, int x2) : base(x1) { this.X2 = x2; } public override

A Fortran function/subroutine that could return either a real, an integer or a string.

久未见 提交于 2019-12-02 05:27:28
I would like to know how to create a function that either returns a real, an integer or a string. For example, the call would be write(*,*)dt%get() where get() would return : an integer if dt%isInteger = .true. a real if dt%isReal = .true. a character if dt%isStr = .true. I believe this might be possible by using an abstract interface to make procedure get() point to either procedure getInteger() , getReal() or getStr() but the abstract interface definition needs to define the ouput type which is, in my case, variable. Here is the related code: type :: dt real(dp) :: realValue integer ::

Pass derived type as array

淺唱寂寞╮ 提交于 2019-12-02 05:18:41
问题 In Fortran, one can operate on arrays, but how can one treat the indices of a derived type as part of an array too? Code would explain what I want to do best: type mytype integer :: b(3,3) real :: c(4) endtype integer :: a(3,3) real :: d(2,4) type(mytype) :: mat(2) !do stuff so that 'mat' gets values .... !usually one does this a = matmul(mat(1)%b, transpose(mat(2)%b)) !multiplying two 3x3 matrices !but how does one do this? Note the "array" d = matmul(mat(:)%c, mat(:)%c) I assumed that the

Error in Derived type declaration: Variable at (1) in this context must be constant

戏子无情 提交于 2019-12-02 03:31:13
I have a derived type declared in a module like this: MODULE dmotifs TYPE :: PRM INTEGER, PRIVATE :: nsp=4,nrx=8,maxprx=4 REAL, PRIVATE :: cref=1e-6,tref=1 REAL, DIMENSION(nrx,maxprx) :: k REAL :: input END TYPE PRM CONTAINS SUBROUTINE unreg(y,param,r,s) TYPE(PRM), INTENT(IN) :: param REAL, DIMENSION(param%nsp), INTENT(IN) :: y INTEGER, DIMENSION(param%nsp,param%nrx), INTENT(OUT) :: s=0 REAL, DIMENSION(param%nrx,1), INTENT(OUT) :: r=0 REAL :: mOut, mCtrl, pOut, pCtrl mOut=y(ind_mOut) mCtrl=y(ind_mCtrl) pOut=y(ind_pOut) pCtrl=y(ind_pCtrl) ! <some operations on "r" and "s"> RETURN END SUBROUTINE

Pass derived type as array

天涯浪子 提交于 2019-12-02 01:01:50
In Fortran, one can operate on arrays, but how can one treat the indices of a derived type as part of an array too? Code would explain what I want to do best: type mytype integer :: b(3,3) real :: c(4) endtype integer :: a(3,3) real :: d(2,4) type(mytype) :: mat(2) !do stuff so that 'mat' gets values .... !usually one does this a = matmul(mat(1)%b, transpose(mat(2)%b)) !multiplying two 3x3 matrices !but how does one do this? Note the "array" d = matmul(mat(:)%c, mat(:)%c) I assumed that the final line is analogous to a 2x4 matrix being multiplied with itself. However, when I try to compile,