LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-05-2007, 01:08 AM   #1
paragkalra
Member
 
Registered: Sep 2006
Location: Nagpur, Maharashtra, India
Distribution: Debian, Ubuntu, Redhat, Fedora, SLES, OpenSUSE, FreeBSD, Mac OS X
Posts: 221

Rep: Reputation: 31
Red face GCC vs VISUAL STUDIO


I need your help very urgently.......

Actually my HOD (head of department) is doing PHD....

He has a .c file which can be successfully compiled on LINUX using gcc....

I myself have successfully compiled it on REDHAT

Now he wants to compile the same program on Windows using VISUAL STUDIO...

Is there any way out to solve this problem...

Actually its throwing some error...

Actually I have very little knowledge oF MICROSOFT PRODUCTS ....

Please kindly try to solve my problem as early as possible.....It's very urgent....

Here's the code:

/* cap2d.c
-------
This program takes arguments D and k, the parameters for the
Wierstraus-Mandelbrot (W-M) function. A two-dimensional unit
surface is generated with fract2d(), defined in wm.c. A second
flat surface is placed at a distance CAP_D above the first. Using
the method of finite differences, Laplace's equation is found, and
the capacitance (in the form of electrical flux per unit area) is
written to the file named in OUTFILE as Matlab commands.

As the program runs, it prints the iteration count and the finite
difference tolerance to stdout.

To compile:

gcc capacitance.c wm2d.c -lm -O2 -o ./cap2d

To run:

./cap2d D k

(where 1 < D < 2 and |1-k| < |1-D|.)\n"
*/

#include "wm2d.h"

// For convenience, try keeping NPTS = 1.0 / SCALE
#define NPTS 100 // Surfaces have NPTS x NPTS nodes
#define SCALE 0.01 // Separation of each node

#define L_OS 0.9 // Undershoot factor. See iterate(), below
#define L_IT 50000 // Number of finite difference iterations

#define CAP_D 2.0 // Mean separation of the two surfaces
#define CAP_V 1.0 // Surfaces have potential +/- CAP_V

#define OUTFILE "temp.m"
#define USAGE "Incorrect arguments. Usage: cap2d D k"

/* Macro for periodic boundary conditions, called by iterate(). */
#define WRAP(a) (((a) + NPTS) % NPTS)

/* Function prototypes. Watch out for use of both floats and doubles.
The array phi[][][] uses floats to speed up iterate(), while the
matrix inversion in fract2d() needs doubles to avoid overflow. */
float laplace (double **zlo, double **zhi);
inline float iterate(float ***phi1, float ***phi2, int **surfmin, int **surfmax);
void outfile(double D, float flux);
void lfree (float ***phi1, float ***phi2, int **surfmin, int **surfmax);


int main (int argc, char **argv) {

int i, j;
float flux;
double wm_H, wm_k, **zlo, **zhi;

if (argc == 3) {
wm_H = 2.0 - strtod(argv[1], NULL);
wm_k = strtod(argv[2], NULL);
} else
error(1, 0, USAGE);

/* Each surface is stored in an NPTS x NPTS 2-D array. The array
indices (times SCALE) are the x and y coordinates. The array
contents are the z-coordinates. */

zlo = (double **)xmalloc(NPTS * sizeof(double *));
zhi = (double **)xmalloc(NPTS * sizeof(double *));
for (i = 0; i < NPTS; i++) {
zlo[i] = (double *)xmalloc(NPTS * sizeof(double));
zhi[i] = (double *)xmalloc(NPTS * sizeof(double));
}

/* Fill zlo using the W-M function, with mean zero. */
fract2d(zlo, wm_H, wm_k, NPTS, SCALE);

/* Fill zhi with a simple flat surface z = CAP_D. */
for (i = 0; i < NPTS; i++)
for (j = 0; j < NPTS; j++)
zhi[i][j] = CAP_D;

flux = laplace (zlo, zhi);
outfile(2.0-wm_H, flux);

freezpt(zlo, NPTS);
freezpt(zhi, NPTS);

exit(0);
}


/* This function creates a 3-D array representing the potential in the
cavity between the surfaces. Using the finite difference method, we
solve Laplace's equation for the cavity.

The function returns the flux, which is
flux = (sum) E . dA
= A * (sum) dE/dz
= 1 * ((sum)dE) / SCALE

Note that the capacitance is simply
C = flux / ( CAP_V * epsilon_0) */
float laplace (double **zlo, double **zhi) {

int i, j, k, m, n, nz;
float tmp;
double zmin, zmax;

int **surfmin = xmalloc(NPTS * sizeof (int *));
int **surfmax = xmalloc(NPTS * sizeof (int *));

float ***phi1 = xmalloc(NPTS * sizeof (float **));
float ***phi2 = xmalloc(NPTS * sizeof (float **));
float ***phi3;

zmin = HUGE_VAL;
zmax = 0.0;
for (i = 0; i < NPTS; i++)
for (j = 0; j < NPTS; j++) {
if (zmin > zlo[i][j]) zmin = zlo[i][j];
if (zmax < zhi[i][j]) zmax = zhi[i][j];
}

nz = 1 + (int)((zmax - zmin)/SCALE);

for (i = 0; i < NPTS; i++) {
surfmin[i] = (int *)xmalloc(NPTS * sizeof (int));
surfmax[i] = (int *)xmalloc(NPTS * sizeof (int));
phi1[i] = (float **)xmalloc(NPTS * sizeof (float *));
phi2[i] = (float **)xmalloc(NPTS * sizeof (float *));

for (j = 0; j < NPTS; j++) {
phi1[i][j] = (float *)xmalloc(nz * sizeof(float));
phi2[i][j] = (float *)xmalloc(nz * sizeof(float));
}
}

for (i = 0; i < NPTS; i++) {
for (j = 0; j < NPTS; j++) {

/* surfmin and surfmax contain the index for the lower and upper
surfaces respectively. For example, phi[i][j][surfmin[i][j]]
gives the potential of the lower surface. */
surfmin[i][j] = (int)((zlo[i][j]-zmin)/SCALE);
surfmax[i][j] = (int)((zhi[i][j]-zmin)/SCALE);

if (surfmin[i][j] > surfmax[i][j])
error(1, 0, "Surfaces of capacitor meet.");

/* The nodes outside the cavity (including the surface nodes)
are maintained at +/- CAP_V. */
for (m = 0; m <= surfmin[i][j]; m++)
*(phi1[i][j] + m) = *(phi2[i][j] + m) = - CAP_V;

for (n = nz - 1; n >= surfmax[i][j]; n--)
*(phi1[i][j] + n) = *(phi2[i][j] + n) = CAP_V;

/* As an initial guess, we let the potential drop linearly from
the lowest point (zmin) to the highest point (zmax). */
tmp = 1.0 / (n - m);
for (k = m + 1; k < n; k++)
phi1[i][j][k] = 2 * CAP_V * (k - m) * tmp - CAP_V;
}
}

/* Perform finite difference iterations. tmp is the
"badness-of-fit," which should converge to zero. If it starts
blowing up or oscillating wildly, decrease L_OS. */
for (i = 0; i < L_IT; i++) {
tmp = iterate(phi1, phi2, surfmin, surfmax);
phi3 = phi1; phi1 = phi2; phi2 = phi3;
printf ("\r[%5d] %.2f ", i, tmp);
fflush(stdout);
}
printf("\n");

/* Compute and return the flux. */
tmp = 0.0;
k = nz - 5;
for (i=0; i < NPTS; i++)
for (j=0; j < NPTS; j++)
if (k < surfmin[i][j] || k > surfmax[i][j])
error(1, 0, "Surfaces of capacitor meet.");
else
tmp += (phi1[i][j][k+1] - phi1[i][j][k]);

lfree(phi1, phi2, surfmin, surfmax);
return (tmp * SCALE);
}


/* This function performs the finite difference step:

phi_{n+1} (x,y,z) = phi_{n} (x,y,z) + L_OS * A(x,y,z)

where L_OS is the (under/over)relaxation and

A(x,y,z) = 1/6 * ( phi_{n} (x+h, y, z) + phi_{n} (x-h, y, z)
+ phi_{n} (x, y+h, z) + phi_{n} (x, y-h, z)
+ phi_{n} (x, y, z+h) + phi_{n} (x, y, z-h) )
- phi_{n} (x,y,z)

phi_{n} is stored in phi1. The function stores phi_{n+1} in phi2. */

inline float iterate(float ***phi1, float ***phi2, int **surfmin, int **surfmax) {

int i, j, k;
float tmp, tol = 0.0;

for (i = 0; i < NPTS; i++)
for (j = 0; j < NPTS; j++)
for (k = surfmin[i][j] + 1; k < surfmax[i][j]; k++) {
tmp = phi1[i][j][k-1] + phi1[i][j][k+1];
tmp += phi1[WRAP(i-1)][j][k] + phi1[WRAP(i+1)][j][k];
tmp += phi1[i][WRAP(j-1)][k] + phi1[i][WRAP(j+1)][k];
tmp *= 0.1666667;
tmp -= phi1[i][j][k];
tol += tmp;
phi2[i][j][k] = phi1[i][j][k] + L_OS * tmp;
}
return tol;
}

void outfile(double D, float flux) {

FILE *out = fopen(OUTFILE, "a");

fprintf(out, "D = %f : ", D);
fprintf(out, "flux = %f\n", flux);

fclose(out);
}

/* Frees memory allocated in laplace(). */
void lfree (float ***phi1, float ***phi2, int **surfmin, int **surfmax) {

int i, j;
for (i = 0; i < NPTS; i++) {
for (j = 0; j < NPTS; j++) {
free(phi1[i][j]);
free(phi2[i][j]);
}
free(phi1[i]);
free(phi2[i]);
free(surfmin[i]);
free(surfmax[i]);
}
free(phi1);
free(phi2);
free(surfmin);
free(surfmax);
}


HERE's the ERROR:

------ Build started: Project: cap2d, Configuration: Debug Win32 ------
Compiling...
cap2d.c
e:\d drive backup\parag\final_year_project\code\wm2d.h(2) : fatal error C1083: Cannot open include file: 'math.h': No such file or directory
Build log was saved at "file://e:\D drive backup\parag\final_year_project\code\Debug\BuildLo g.htm"
cap2d - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 
Old 01-05-2007, 01:21 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
#include <math.h> in "wm2d.h"

Here's the entire problem:
Quote:
------ Build started: Project: cap2d, Configuration: Debug Win32 ------
Compiling...
cap2d.c
e:\d drive backup\parag\final_year_project\code\wm2d.h(2) : fatal error C1083: Cannot open include file: 'math.h': No such file or directory
One of your professor's header files, "wm2d.h", #include's a system header file, "math.h". Visual Studio can't find "math.h". That's all!

Every properly installed C/C++ compiler, Gnu or Visual studio, should have "math.h". So there are only two possibilities that I can think of:

a) Visual Studio wasn't installed correctly
<= Solution: re-install VC++
... or ...
b) "math.h" wasn't #include'ed correctly ni "wm2d.h"

wm2d.h should have these lines:
Code:
#include <windows.h>
#include <math.h>
'Hope that helps .. PSM
 
  


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
Stupid Question: Microsoft Visual Studio 6.0 (A.K.A. Visual Basic 6) Will it run? S0RD3N Linux - Software 8 05-07-2008 08:42 PM
IDE like visual studio zakkeyboorock Programming 5 03-22-2006 01:12 PM
visual studio on wine supermatt Linux - Software 5 03-03-2004 04:16 PM
Visual Studio like IDE jpc82 Programming 6 02-13-2004 06:12 AM
Weaning myself from Visual Studio kleptophobiac Programming 2 01-25-2004 10:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:29 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