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.

# Fortran: gradient of 3D array

Page 1 of 1## 8 Replies - 4813 Views - Last Post: 19 February 2008 - 06:33 AM

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

### #2

## 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?

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

## 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?

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

## 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

## 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

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

## 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

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.

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

## 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.

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

### #8

## 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 .

Field theory can be a big pain sometimes .

### #9

## 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 .

Thanks .

Page 1 of 1