×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# How to initialize array dynamically to find out grid independent solution

## How to initialize array dynamically to find out grid independent solution

(OP)
Hi All,

I am trying to figure out how to allocate the size for Array dynamically, as in I want it to be from 0 to a Number till grid independent solution is reached by a Condition.

So below is my program, I run it many time for different values of N. But then I don't want to do this, could you please help me with this

Thank You!

P.S: I have tried different ways but then since the entire program depends on N. I am not able to figure out what to do!

program HW1_P1_a
Integer, Parameter :: N=250 !Globally used variable, hence PARAMETER
Double Precision Del_X,A(N),B(N),C(N),D(N),PA,QA,RA,PB,QB,RB
Double Precision L,M,Theta_B,Theta_L, THETA(N)
Integer I,T_B,T_L,T_INF
! "Details about Arguments used: All must be Double Precision (except N)"
! N Number of mesh points
! Del_X Mesh size
! A() Lower diagonal of tridiagonal matrix
! B() Main diagonal of tridiagonal matrix
! C() Upper diagonal of tridiagonal matrix
! D() Right side vector (do not adjust for bcs)
! PA Left b.c.(K=1): Coefficient of Theta
! QA Left b.c.(K=1): Coefficient of dTheta/Del_X
! RA Left b.c.(K=1): Right hand side
! PB Right b.c.(K=N): Coefficient of Theta
! QB Right b.c.(K=N): Coefficient of dTheta/Del_X
! RB Right b.c.(K=N): Right hand side
! L Length of Fin
! T_B Temperature at Base of Fin
! T_L Temperature at the Tip of Fin
! T_inf Temperature of surrounding air
! Theta_B Temperature excess at Fin Base
! Theta_L Temperature excess at Fin Tip
! P Perimeter
! h Heat Transfer Coeffecient
! Ac Cross Sectional Area of Fin
! K Thermal Conductivity

! Inititalising values of the variables, "Given Conditions"
T_B=200
T_L=60
T_INF=20
L=0.1
P=0.09
Ac=7.E-4
h=15
K=80

! Equation for Excess Temperature

Theta_B=T_B-T_INF
Theta_L=T_L-T_INF

! Equation for Delta X and M

Del_X=L/(N-1) !Mesh size
M=SQRT((h*P)/(K*Ac))

! Fin equation derivatives are approximated using second order accurate finite difference and multiplying with (Del_X)^2

g=M**2
f=0

! Elements of the Matrix

DO I=1,N

A(i) = 1 - (Del_X/2)*f
B(i) = -(2 + (Del_X**2)*g)
C(i) = 1 + (Del_X/2)*f
D(i) = 0

ENDDO

! Dirchlet Boundary Condition at the Base

PA=1
QA=0
RA=Theta_B

! Dirclet Boundary condition at the Tip

PB=1
QB=0
RB=Theta_L

call THOMAS(N,Del_X,A,B,C,D,PA,QA,RA,PB,QB,RB,THETA)

Write(*,*) 'Values of Theta by Numerical Solutions for N:',N

DO I=1,N

write (*,*) (I*Del_X),THETA(I)

ENDDO
End HW1_P1_a

!**********************************************************************
!
! SUBROUTINE: THOMAS
!
! DESCRIPTION: Use the Thomas algorithm to solve a system of
! linear algebraic equations where the
! coefficient matrix is tridiagonal. Boundary
! conditions can be Dirichlet, Neumann or Mixed.
!
! Left boundary condition is (X = Xmin):
!
! PA*Theta + QA*dTheta/Del_X = RA
!
! Right boundary condition is (X = Xmax):
!
! PB*Theta + QB*dTheta/Del_X = RB
!
! System of Equations:
!
! B1 C1 0 0 . . 0 = D1
! A2 B2 C2 0 . .
! 0 A3 B3 C3 . .
! . . . .
! . Ai Bi Ci = Di
! 0 . . . . An Bn = Dn
!
!
! RETURN ARGUMENTS:
! THETA() Function value
!
! VARIABLES:
!
! **********************************************************************
!
SUBROUTINE THOMAS(N,Del_X,A,B,C,D,PA,QA,RA,PB,QB,RB,THETA)

IMPLICIT DOUBLE PRECISION (A-H,O-Z)

PARAMETER (KDIM=10001)

DIMENSION A(*),B(*),C(*),D(*),THETA(*)
DIMENSION F(KDIM),DELTA(KDIM)

! -- Adjust coefficients for boundary conditions

B(1) = QA*B(1) + 2.D0*Del_X*PA*A(1)
C(1) = QA*(C(1) + A(1))
D(1) = QA*D(1) + 2.D0*Del_X*RA*A(1)

A(N) = QB*(A(N) + C(N))
B(N) = QB*B(N) - 2.D0*Del_X*PB*C(N)
D(N) = QB*D(N) - 2.D0*Del_X*RB*C(N)

! -- Forward elimination

F(1) = C(1)/B(1)
DELTA(1) = D(1)/B(1)
DO K=2,N
X1 = B(K) - A(K)*F(K-1)
X1 = 1.D0/X1
F(K) = C(K)*X1
DELTA(K) = X1*(D(K) - A(K)*DELTA(K-1))
END DO

! -- Back substitution

THETA(N) = DELTA(N)
DO K=N-1,1,-1
THETA(K) = DELTA(K) - F(K)*THETA(K+1)
END DO

RETURN
END

### RE: How to initialize array dynamically to find out grid independent solution

Depends on which version of Fortran you're using. If you use from F95 onwards, you could use ALLOCATE to allocate the arrays. F90 is a bit restrictive on its use of ALLOCATE.

Look up the ALLOCATABLE declaration and ALLOCATE function. To avoid memory leaks, DEALLOCATE when you've finished.

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

#### Resources

White Paper - How ESI is Helping Move New Medical Device Product to Market Quicker & More Cost Effic
Early Supplier Involvement has long been a strategy employed by manufacturers to produce innovative products. Now, it almost seems like a necessity. Because decisions made in the design phase can positively affect product quality and costs, this can help add value to OEM bottom lines. This white paper will discuss many facets of ESI, including why itâ€™s so valuable today, what challenges limit the benefits of ESI, how cost is impacted, and more. Download Now
White Paper - Moving to a Driverless Future
This white paper describes what we see as the best practices to support a sustainable engineering process for autonomous vehicle design. It exposes how to use simulation and testing in common frameworks to enable design exploration, verification and validation for the development of autonomous cars at a system, software and full-vehicle level to drive a mature product development process for automated driving. Download Now
Guide - When to Outsource Manufacturing to a Service Provider
As a 3D printer owner, you may be already reaping the benefits of 3D printing, such as rapid production, design freedoms and easy access to fresh prints of the machine. We dive into scenarios to help you decide when you need to outsource your project to a service provider. Download Now
Research Report - How Engineers are Using Remote Access
Remote access enables engineers to work from anywhere provided they have an internet connection. We surveyed our audience of engineers, designers and product managers to learn how they use remote access within their organizations. We wanted to know which industries have adopted remote access, which software they are using, and what features matter most. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!