LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-08-2014, 12:24 PM   #1
Nenita
LQ Newbie
 
Registered: Nov 2014
Posts: 7

Rep: Reputation: Disabled
Post Help Me: 2D FDTD Electromagnetic Wave Propagation code in C program


Hi everyone!! My name is Nenita, I'm looking for help on 2D FDTD Electromagnetic Wave Propagation code in C program. Please anyone with knowledge of this please help me!
 
Old 11-08-2014, 12:33 PM   #2
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
What have you done so far?
 
Old 11-08-2014, 12:38 PM   #3
Nenita
LQ Newbie
 
Registered: Nov 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
This is what I've done so far, but when I run the program, it does not show a wave graph. It's just a flat surface.

Code:
# include <stdio.h>
# include <stdlib.h>
# include <math.h>

# define IE 60
# define JE 60

int main ()
{
  double ez[IE][JE], hx[IE][JE], hy[IE][JE];
  int l, n, i, j, ic, jc, nsteps;
  double dx, dy, dt, T, epsz, pi, sigma, mu;
  double t0, spread, pulse;
  double c  =  2.99792458e8;  // propagation velocity
  double freq  = 1.0e9;
  double alamda  =  c/freq;
  FILE *fp, *fopen();

  ic  =  IE/2;
  jc  =  JE/2;
  dx  =  .01;        /* cell size */
  dy  =  .01;        /* cell size */
  dt  =  1.0/freq/6e8;     /* Time steps */
  epsz  =  8.8e-12;
  pi  = 3.14159;
  mu  = 1.0e-7*4.0*pi;
  sigma  = 1.0e-8 ;

  for( j = 0; j < JE; j++) {
    //printf("%2d", j);
    for(i = 0; i < IE; i++) {
      ez[i][j]  =  0.;
      hx[i][j]  =  0.;
      hy[i][j]  =  0.;
    }
    printf("\n");
  }

  t0 = 20.0;
  spread = 6.0;
  T = 0;
  nsteps = 1;

  while(nsteps > 0){
    printf("nsteps -> ");
    scanf("%d", &nsteps);
    printf("%d \n", nsteps);

    for(n = 1; n <= nsteps; n++) {
      T = T + 1;

        /*  - Start of Main FDTD loop  - */

      /*  -  Put a Gaussian pulse in the middle  -  */
    
      pulse = sin(2*pi*1500*1e6*dt);
      ez[ic][jc] = pulse;

      /*  -  Calculate Ez field  -  */

     for(j = 1; j < JE; j++){
        for(i = 1; i < IE; i++) {
          ez[i][j] = (1.0 - sigma*dt/(2.0*epsz))/(1.0+sigma*dt)/(2.0*epsz)*ez[i][j]+(dt/epsz)
            /(1.0+sigma*dt/(2.0*epsz*(1/dx))*(hy[i][j] - hy[i - 1][j]) - (dt/epsz)/((1.0+sigma*dt)/(2.0*epsz))*(1/dy)
            *(hx[i][j] - hx[i][j - 1]));
        }}

      /*  -  Calculate Hx field  -  */
      for(j = 0; j < JE - 1; j++){
        for(i = 0; i < IE - 1; i++){
          hx[i][j] = hx[i][j] - (dt/mu*1/dy)*(ez[i][j+1] - ez[i][j]);
        }
      }
      
      /* Calculate Hy field */
      for(j = 0; j < JE - 1; j++){
        for(i = 0; i <= IE - 1; i++) {
       hy[i][j] = hy[i][j]+(dt/mu*1/dx)*(ez[i+1][j] - ez[i][j]);
        }
      }
    }

    /*  -  End of main FDTD loop  -  */

    for(j = 1; j < jc; j++) {
      printf("%2d", j);
      for(i = 1; i < ic; i++) {
        printf("%5.2f", ez[2*i][2*j]);
      }
      printf("\n");
    }
    printf("T = %5.0f \n", T);

    /* Write E field out to file "Ez" */

    fp = fopen("Ez", "w");
    for(j = 0; j < JE; j++) {
      for(i = 0; i < IE; i++) {
        fprintf(fp,"%d\t%d\t%6.3f\n",i, j, ez[i][j]);
      }
      fprintf(fp, "\n");
    }
    fclose(fp);
  }
}

Last edited by Nenita; 11-08-2014 at 01:21 PM. Reason: Did not put in code tag
 
Old 11-08-2014, 01:36 PM   #4
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
I know nothing about this, but just eyeballing your code, the ez formula looks suspect to me.
Code:
ez[i][j] = (1.0 - sigma*dt/(2.0*epsz))/(1.0+sigma*dt)/(2.0*epsz)*ez[i][j]+(dt/epsz)
            /(1.0+sigma*dt/(2.0*epsz*(1/dx))*(hy[i][j] - hy[i - 1][j]) - (dt/epsz)/((1.0+sigma*dt)/(2.0*epsz))*(1/dy)
            *(hx[i][j] - hx[i][j - 1]));
All of the elements of the arrays ez, hx, and hy are 0.
 
Old 11-08-2014, 01:59 PM   #5
Nenita
LQ Newbie
 
Registered: Nov 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by norobro View Post
I know nothing about this, but just eyeballing your code, the ez formula looks suspect to me.
Code:
ez[i][j] = (1.0 - sigma*dt/(2.0*epsz))/(1.0+sigma*dt)/(2.0*epsz)*ez[i][j]+(dt/epsz)
            /(1.0+sigma*dt/(2.0*epsz*(1/dx))*(hy[i][j] - hy[i - 1][j]) - (dt/epsz)/((1.0+sigma*dt)/(2.0*epsz))*(1/dy)
            *(hx[i][j] - hx[i][j - 1]));
All of the elements of the arrays ez, hx, and hy are 0.

I'm also very new to the topic I've tried re-writing ez, hx and hy formulas but I'm stuck so I reached out to other programmers who may be able to help me find what the problem is.
 
Old 11-08-2014, 02:22 PM   #6
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Put some printfs in there to output the values as you go. If you know the correct values you should be able to find out what is wrong.
 
Old 11-08-2014, 02:38 PM   #7
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
The basics of writing such a program:

- Make sure you have the formulas correct in writing, not in your program
- Calculate some values by hand so you know what the matrix element values must be
- Translate the formulas in C
- Put printf statements after each calculation which print the index variables (i and j), the calculation result and further any input values to formulas which are variable as well.

Anything less than that tends to demonstrate lazyness.

jlinkels
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Source code for Back Propagation Network in NS2.35 R.Mosheca Linux - Newbie 0 08-07-2014 01:44 PM
Acoustic simulation program or (sound wave simulation program) tarotint Linux - Software 2 02-24-2010 04:04 AM
Snakes and Electromagnetic fields rjcrews General 27 12-26-2006 12:36 AM
LXer: Is your code ready for the next wave in commodity computing? LXer Syndicated Linux News 0 07-10-2006 07:54 AM
program converting AIFF to WAVE format and/or vice versa monohouse Linux - Software 1 02-12-2005 06:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration