lundi 24 février 2020

Expected expression before "double" when using rand()?

Despite reading similar questions with the same topic, I seem to not get the reason my compiler is telling me "Expected expression before double".

Down there in my code, I am trying to create a program which generates a lattice, then the atoms will move in random velocities after performing a Box-Muller transformation. The new positions will then be recorded and exported into a .txt file, which will then be modified for VMD to read:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Kb 8.6173324e-5 //Boltzmann constant (in eV/K)
#define PI 3.14159265
#define C 299792.458  // in angstrom/ps
#define Temp 273.15 // in K
#define mass 3.350918e-26 //in KG

//this is the algorithm for Box-Muller transformation
double Box_Muller_Transformation(double box1, double box2)
{
//box1 and box2 are independent, random numbers randing from 0 to 1, the calculated result is num1.
double num1 = sqrt(-2*log(box1))*cos(2*PI*box2);
//returning result for future calling
return num1;
}

int a = 9, b = 2; //this is the lattice size, boundary is set as 100 in each direction
double t=0, num1=0, num2=0;
double box1 = double(rand()/RAND_MAX); //generating box1
double box2 = double(rand()/RAND_MAX); //generating box2

int main()
{
    //generating the 9 particles as a 2x2 lattice
    // making 2x2 array
    int i=0, j=0;
    int position[9][2];

    //i is x-coordinate of particle
    //j is y-coordinate of particle
    //k is z-coordinate of particle
    //a is the number of particle


    FILE *fp;
    fp = fopen ("lab2testing.txt", "w+"); //preparing a file to write in stuff

    for (i=0, a=0; i<9, a<9; i++, a++)
    {
            position[a][0]=i;
            position[a][1]=j;
            if(a==2||a==5)
            {
                i=0;
                j++;    
            }
            printf("%d,%d\n", position[a][0], position[a][1]);
    }

    //calculating the path of the article

    for (a=0, t=0; a<9, t=100; a++, t=t+0.01)
    {
        //calculating the new positions through euler's method

        num1= Box_Muller_Transformation(box1, box2);

        num2 = num1*sqrt(Kb*Temp/mass);

        position[a][0]=position[a][0]+0.01*num2;

        position[a][1]=position[a][1]+0.01*num2;

        //4 scenarios of getting out of bounds

        if (position[a][0]>50)
        {
            num2=-num2;
            position[a][0]=position[a][0]-0.01*num2;
        }
        if (position[a][0]<0)
        {
            num2=-num2;
            position[a][0]=-position[a][0];
        }
        if (position[a][1]>50)
        {
            num2=-num2;
            position[a][1]=position[a][1]-0.01*num2;
        }
        if (position[a][1]<0)
        {
            num2=-num2;
            position[a][1]=-position[a][1];
        }

        fprintf(fp, "%d \n%s \n", a, "particle");
        fprintf(fp, "%s %f %f\n ", "Ar", position[a][0], position[a][1]);
        printf("%f %f\n", position[a][0], position[a][1]);

    }

    return(0);
}

My problem is on line 21 and 22, the compiler said that it "expected expression before double" for "box1" and "box2", used for the transformation to get the velocity of the particles, but I do not know why is that.

double box1 = double(rand()/RAND_MAX); //generating box1
double box2 = double(rand()/RAND_MAX); //generating box2

The number types should be the same on both sides (double and double) and I planned to use that line to produce a number between 0 to 1 by letting the program generate a number, then dividing it by the largest number rand() can produce so it can theoretically produce a number from 0 to 1. However, I just don't understand why it won't compile. How should I modify my code to make it run?




Aucun commentaire:

Enregistrer un commentaire