Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,487 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,693 people online right now. Registration is fast and FREE... Join Now!




Fortran: gradient of 3D array

 
Reply to this topicStart new topic

Fortran: gradient of 3D array

Tim_A
11 Feb, 2008 - 05:35 AM
Post #1

New D.I.C Head
*

Joined: 11 Feb, 2008
Posts: 4

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.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Fortran: Gradient Of 3D Array
15 Feb, 2008 - 05:51 PM
Post #2

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

Tim_A
RE: Fortran: Gradient Of 3D Array
18 Feb, 2008 - 03:12 PM
Post #3

New D.I.C Head
*

Joined: 11 Feb, 2008
Posts: 4

QUOTE(Louisda16th @ 15 Feb, 2008 - 06:51 PM) *
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.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Fortran: Gradient Of 3D Array
18 Feb, 2008 - 03:26 PM
Post #4

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
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?
User is offlineProfile CardPM
+Quote Post

Tim_A
RE: Fortran: Gradient Of 3D Array
18 Feb, 2008 - 04:06 PM
Post #5

New D.I.C Head
*

Joined: 11 Feb, 2008
Posts: 4

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 smile.gif
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Fortran: Gradient Of 3D Array
18 Feb, 2008 - 04:28 PM
Post #6

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Tim_A
RE: Fortran: Gradient Of 3D Array
18 Feb, 2008 - 04:31 PM
Post #7

New D.I.C Head
*

Joined: 11 Feb, 2008
Posts: 4

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.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Fortran: Gradient Of 3D Array
18 Feb, 2008 - 04:33 PM
Post #8

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
I want to try this in the general curvilinear coordinate system sometime. Now that would be a challenge biggrin.gif.
Field theory can be a big pain sometimes tongue.gif.
User is offlineProfile CardPM
+Quote Post

Louisda16th
RE: Fortran: Gradient Of 3D Array
19 Feb, 2008 - 05:33 AM
Post #9

 
Group Icon

Joined: 3 Aug, 2006
Posts: 1,790



Thanked: 1 times
Dream Kudos: 755
My Contributions
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 smile.gif.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:25PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month