I am trying to implement the GSL uniform random number generator into my C++ program. Below is my entire program and I am having difficulties getting it to run properly. Any help would be greatly appreciated. Thanks for your time!

``````#include <stdio.h>
#include <iostream>
#include <math.h>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <gsl\gsl_rng.h>

int main()
{
double CeOld;
double CgOld;
double r;
double Pe;

double Omega = .5;
double gamma = 1;
double Ce = 1/sqrt(2);
double Cg = 1/sqrt(2);
double T  = 0.0;
double dT = 0.05;
double CeDot;
double CgDot;
double Sum;

ofstream position;
ofstream momentum;
positiondata.open("position.txt");
momentumdata.open("momentum.txt");

const gsl_rng_type * T;
gsl_rng * q;
gsl_rng_env_setup();
T = gsl_rng_default;
q = gsl_rng_alloc (T);

printf("Cg=%lf\n",Cg);

for(int i = 1; i < 10000; i++)
{
T=T+dT;
CgOld=Cg;
CeOld=Ce;
CeDot = (Omega/2)*Cg-(gamma/2)*Ce;
CgDot = -((Omega/2)*Ce);
r = gsl_rng_uniform(q);
if (r < (abs(Ce)*abs(Ce)*dT))
{
Cg = CeOld;
Ce = 0;
}
else{

Cg = CgOld+CgDot*dT;
Ce = CeOld+CeDot*dT;
}
Pe=abs(Ce)*abs(Ce);
printf("Pe=%lf\n",Pe);
Sum=sqrt(pow(Ce, 2.0)+ pow (Cg, 2.0));
Ce=Ce/Sum;
Cg=Cg/Sum;

};
return 0;

}``````
2
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by dmanw100

I know the problem has to do with GSL in general. This is one of the error messages I receive when I try to compile my code.

C:\Documents and Settings\Luke\Desktop\main.cpp|8|gsl\gsl_rng.h: No such file or directory|
C:\Documents and Settings\Luke\Desktop\main.cpp||In function `int main()':|
C:\Documents and Settings\Luke\Desktop\main.cpp|27|error: `ofstream' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|27|error: expected `;' before "position"|
C:\Documents and Settings\Luke\Desktop\main.cpp|28|error: expected `;' before "momentum"|
C:\Documents and Settings\Luke\Desktop\main.cpp|29|error: `positiondata' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|30|error: `momentumdata' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|32|error: expected initializer before '*' token|
C:\Documents and Settings\Luke\Desktop\main.cpp|33|error: `gsl_rng' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|33|error: `q' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|34|error: `gsl_rng_env_setup' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|35|error: `gsl_rng_default' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|36|error: `gsl_rng_alloc' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|47|error: `gsl_rng_uniform' was not declared in this scope|
C:\Documents and Settings\Luke\Desktop\main.cpp|48|warning: passing `double' for converting 1 of `int abs(int)'|
C:\Documents and Settings\Luke\Desktop\main.cpp|48|warning: passing `double' for converting 1 of `int abs(int)'|
C:\Documents and Settings\Luke\Desktop\main.cpp|59|warning: passing `double' for converting 1 of `int abs(int)'|
C:\Documents and Settings\Luke\Desktop\main.cpp|59|warning: passing `double' for converting 1 of `int abs(int)'|
||=== Build finished: 13 errors, 4 warnings ===|

position and positiondata should be the same? Also the compiler can't find the gsl directory. You're also passing doubles into the abs() function when it wants an int. Try #include "gsl\gsl_rng.h"

I already have #include "gsl\gsl_rng.h" in my program but I don't know why its still not working :(

Try moving the gsl directory to the project directory. I believe "gsl\gsl_rng.h" will make the compiler look in the current directory while <gsl\gsl_rng.h> will make it look in its default directory. You should not be getting the

C:\Documents and Settings\Luke\Desktop\main.cpp|8|gsl\gsl_rng.h: No such file or directory|

message if it can find the header file.

And fstream should work where ofstream failed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.