# Fortran: gradient of 3D array

Page 1 of 1

## 8 Replies - 4878 Views - Last Post: 19 February 2008 - 06:33 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=42960&amp;s=b8a744c3208938274bae936852196ee4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Tim_A

Reputation: 0
• Posts: 4
• Joined: 11-February 08

# Fortran: gradient of 3D array

Posted 11 February 2008 - 06:35 AM

Hi everyone.

I'm a physics student, of course from time to time I have to do some programming, I just haven't cracked it yet, doesn't really come naturally to me.

Anyway, I'm writing a program modelling specific magnetic fields, and I have a 3D scalar field named flux(i,j,k).

My task now is to take the gradient of this array to convert each point to the magnetic field. I can picture quite easily how I would do this in a 1-dimensional case, just sum a number of points and divide by the distance between the end points. I can't quite seem to translate this to 3D in my mind, though.

Any hints or suggestions or pseudocode would be really helpful.

Many thanks,
Tim.

Is This A Good Question/Topic? 0

## Replies To: Fortran: gradient of 3D array

### #2 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: Fortran: gradient of 3D array

Posted 15 February 2008 - 06:51 PM

Two questions:
1. Are you working with the Cartesian (or rectangular) coordinates or some other one?
2. Are you saying that you have data i.e. the values of flux at various points and not the expression (i.e. function) for calculating flux and therefore (atleast for 1D) you are approximating the derivative at each point?

### #3 Tim_A

Reputation: 0
• Posts: 4
• Joined: 11-February 08

## Re: Fortran: gradient of 3D array

Posted 18 February 2008 - 04:12 PM

Louisda16th, on 15 Feb, 2008 - 06:51 PM, said:

1. Are you working with the Cartesian (or rectangular) coordinates or some other one?
2. Are you saying that you have data i.e. the values of flux at various points and not the expression (i.e. function) for calculating flux and therefore (atleast for 1D) you are approximating the derivative at each point?

1. Cartesian coordinates.

2. Yes, I have the data for the values, not the function, so the gradient will be an approximation.

### #4 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: Fortran: gradient of 3D array

Posted 18 February 2008 - 04:26 PM

Sorry I forgot to ask earlier. What procedure do you use in the 1D case? Do you divide the difference in flux values by difference in position?

### #5 Tim_A

Reputation: 0
• Posts: 4
• Joined: 11-February 08

## Re: Fortran: gradient of 3D array

Posted 18 February 2008 - 05:06 PM

yes, that's right isn't it?

is it just a case of doing pythagoras? if so, i'm gonna feel rather stupid, but at least it's nice and simple

### #6 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: Fortran: gradient of 3D array

Posted 18 February 2008 - 05:28 PM

Well in that case why can't you implement the procedure for the 3D case? After all,
gradV = dV/dx i + dV/dy j + dV/dz k

Store V, x, y and z in separate arrays in the same order. Use a loop to access the current and next elements and then calculate each derivative. If you want the gradient at a particular point, find twp points which lie equidistant with respect to the point you are concerned with. Remember this procedure will give you the derivative of the point in the middle of the two points you are using.
Hope this helps.

### #7 Tim_A

Reputation: 0
• Posts: 4
• Joined: 11-February 08

## Re: Fortran: gradient of 3D array

Posted 18 February 2008 - 05:31 PM

I'm with you. That does help, thanks a lot.

It really is pretty obvious and simple, I just needed a kick-start I guess, I should be ok with it now.

### #8 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: Fortran: gradient of 3D array

Posted 18 February 2008 - 05:33 PM

I want to try this in the general curvilinear coordinate system sometime. Now that would be a challenge .
Field theory can be a big pain sometimes .

### #9 Louisda16th

• dream.in.assembly.code

Reputation: 15
• Posts: 1,967
• Joined: 03-August 06

## Re: Fortran: gradient of 3D array

Posted 19 February 2008 - 06:33 AM

So could you write the code? If yes it would be nice if you could post it here. People would benefit by knowing how you solved your problem.
Thanks .