Fortran subroutine incoming variable array to be implemented in module

Source: Internet
Author: User
Tags gfortran

I told you how the subroutine can't pass a mutable array.

When writing a Fortran program, to achieve a specific function of the matrix, such as the Gauss method to solve the linear equations, many times the subroutine does not know the size of the matrix, such as the finite element program first to use the program to obtain the total stiffness matrix and then gross position just passed in the sub-program to solve the matrix equation. So implementing subroutine parameters is a function of a variable array to place the subroutine in the module. Specific as follows:

Main Program 1 (kk1.f90):

1 Program main 2 implicit none 3 real,allocatable::a (:,:) 4 INTEGER::NP 5 Allocate (A (2,3)) 6 A (1,:) = (/1.2,3.4,5.6/) 7 A (2,:) = (/1.2,3.4,5.6/) 8 call Trya (A,NP) 9 write (*,*) NP10 end Program Main

Sub-Program 1 (TRY1.F90):

1 subroutine Trya (A,NP) 2 implicit None3 real,intent (in), Allocatable,dimension (:,:):: A4 integer,intent (out):: NP5 NP = Size (a,2) 6 End subroutine Trya

Obviously the purpose of the program is to pass in a pre-unknown size matrix into the subroutine, subroutine function is to find out the number of columns returned matrix.

Compile with Gfortran: Gfortran try1.f90 Kk1.f90-o Try again run try result is 16368800 obviously error.

If the handle is placed in the module, the following:

Main program (KK.F90):

1 program main 2 use try 3 implicit none 4 real,allocatable::a (:,:) 5 INTEGER::NP 6 Allocate (A (2,3)) 7 A (1,:) = (/1.2,3.4,5.6/) 8 A (2,:) = (/1.2,3.4,5.6/) 9 call Trya (A,NP) ten write (*,*) NP11 end Program Main

Sub-Program (TRY.F90):

1 module Try 2 Implicit none 3 contains 4 subroutine Trya (A,NP) 5 implicit none 6 real,intent (in), Allocatable,dimension (:, :):: A 7 integer,intent (out):: NP 8 NP = size (a,2) 9 End subroutine trya10 End Module try

Fortran subroutine incoming variable array to be implemented in module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.