A simple program with Monte Carlo

A

Aleramo

Can someone understand my errors in this program? in particular the
reason cos i receive the worning message.
/* Questo programma riguarda il calcolo del mutuo accoppiamento fra
patch dell'array. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_monte.h>
#include <gsl/gsl_monte_vegas.h>

#define IMPEDENZA_ONDA_SPAZIO_LIBERO 120*M_PI

double calcolo_ammettenza_parte_reale (double u_pr[], size_t dimr, void
*p_r);
double calcolo_ammettenza_parte_immag (double u_pc[], size_t dimc, void
*p_c);

int main ()
{
const double K_0 = 2*M_PI*10e9/3e8 ;
struct param_integrale { double a ; double b ; double c ; double f
; } parametri_slot = { K_0*1.186e-2, K_0*0.081e-2, K_0*(0.906 + 0.081),
K_0*0} ;
double res_reale, err_reale, res_immag, err_immag ;
double x_l[2] = {}, x_u[2] ; /* Ricordarsi di inizializzarli. */
const gsl_rng_type *T ;
gsl_rng *r ;
size_t calls = 100000 ;
gsl_monte_function ammettenza_reale =
{&calcolo_ammettenza_parte_reale, 2, &parametri_slot} ,
ammettenza_immag =
{&calcolo_ammettenza_parte_immag, 2, &parametri_slot} ;

gsl_rng_setup () ;
T = gsl_rng_default ;
r = gsl_rng_alloc (T) ;
gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (2) ;

do {
gsl_monte_vegas_integrate (&ammettenza_reale, x_l, x_u, 2,
calls, r, s, &res_reale, &err_reale) ;
} while (fabs (s->chisq - 1.0) > 0.5) ;
printf ("risultato = %.6f, sigma = %.6f, chisq/dof = %.1f\n",
res_reale, err_reale, s->chisq) ;

do {
gsl_monte_vegas_integrate (&ammettenza_immag, x_l, x_u, 2,
calls, r, s, &res_immag, &err_immag) ;
} while (fabs (s->chisq - 1.0) > 0.5) ;
printf ("risultato = %.6f, sigma = %.6f, chisq/dof = %.1f\n",
res_immag, err_immag, s->chisq) ;

return 0;
}

double calcolo_ammettenza_parte_reale (double u_pr[], size_t dimr, void
*p_r)
{
/* p è un puntatore generico che qui punta ad una struttura. */
struct param_integrale *p_slotr = (struct param_integrale *) p_r;
double primo_addendo, secondo_addendo, primo_molt, secondo_molt;

primo_addendo = ((pow (u_pr[0], 2)/2 - pow (u_pr[1], 2))* cos (sqrt
(pow(u_pr[0], 2)) + pow(u_pr[1], 2))) / pow (pow(u_pr[0], 2) +
pow(u_pr[1], 2), 2);
secondo_addendo = ((pow (u_pr[0], 2)/2) - pow (u_pr[0], 2)/(2*(pow
(u_pr[0], 2) + pow (u_pr[1], 2))) + pow (u_pr[1], 2)/( pow(u_pr[0], 2)
+ pow (u_pr[1], 2)) * sin (sqrt (pow(u_pr[0], 2)) + pow(u_pr[1], 2))) /
pow ( pow (u_pr[0], 2) + pow (u_pr[1], 2), 3/2);
primo_molt = p_slotr.b - fabs (u_pr[0] + p_slotr.c);
secondo_molt = p_slotr.a - fabs (u_pr[1] + p_slotr.f);

return (primo_addendo + secondo_addendo) * primo_molt *
secondo_molt;
}

double calcolo_ammettenza_parte_immag (double u_pc[], size_t dimd, void
*p_c)
{
/* p è un puntatore generico che qui punta ad una struttura. */
struct param_integrale *p_slotc = (struct param_integrale *) p_c;
double pr_addendo, sec_addendo, pr_molt, sec_molt;

pr_addendo = ((pow (u_pc[0], 2)/2) - pow (u_pc[0], 2)/(2*(pow
(u_pc[0], 2) + pow (u_pc[1], 2))) + pow (u_pc[1], 2)/( pow(u_pc[0], 2)
+ pow (u_pc[1], 2)) * cos (sqrt (pow(u_pc[0], 2)) + pow(u_pc[1], 2))) /
pow ( pow (u_pc[0], 2) + pow (u_pc[1], 2), 3/2);
sec_addendo = ((pow (u_pc[0], 2)/2 - pow (u_pc[1], 2))* sin (sqrt
(pow(u_pc[0], 2)) + pow(u_pc[1], 2))) / pow (pow(u_pc[0], 2) +
pow(u_pc[1], 2), 2);
pr_molt = p_slotc.b - fabs (u_pc[0] + p_slotc.c);
sec_molt = p_slotc.a - fabs (u_pc[1] + p_slotc.f);

return (pr_addendo + sec_addendo) * pr_molt * sec_molt;
}

The output at shell is:

marco@scatola:~$ gcc mutuoaccoppiamento.c -lgsl -lgslcblas -Wall -lm
mutuoaccoppiamento.c: In function `main':
mutuoaccoppiamento.c:27: warning: implicit declaration of function
`gsl_rng_setup'
mutuoaccoppiamento.c: In function `calcolo_ammettenza_parte_reale':
mutuoaccoppiamento.c:53: error: request for member `b' in something not
a structure or union
mutuoaccoppiamento.c:53: error: request for member `c' in something not
a structure or union
mutuoaccoppiamento.c:54: error: request for member `a' in something not
a structure or union
mutuoaccoppiamento.c:54: error: request for member `f' in something not
a structure or union
mutuoaccoppiamento.c: In function `calcolo_ammettenza_parte_immag':
mutuoaccoppiamento.c:67: error: request for member `b' in something not
a structure or union
mutuoaccoppiamento.c:67: error: request for member `c' in something not
a structure or union
mutuoaccoppiamento.c:68: error: request for member `a' in something not
a structure or union
mutuoaccoppiamento.c:68: error: request for member `f' in something not
a structure or union

I hope someone can help me.
 
M

Mark McIntyre

int main ()
{
const double K_0 = 2*M_PI*10e9/3e8 ;
struct param_integrale { double a ; double b ; double c ; double f
; } parametri_slot = { K_0*1.186e-2, K_0*0.081e-2, K_0*(0.906 + 0.081),

this struct definition is local to main(). The definition isn't
visible ouside main(), so that....
double calcolo_ammettenza_parte_reale (double u_pr[], size_t dimr, void
*p_r)
{
/* p è un puntatore generico che qui punta ad una struttura. */
struct param_integrale *p_slotr = (struct param_integrale *) p_r;

.... this function cannot see the struct definition, and you get an
error.
Move the structure definition ouside main(), and just create and
initialise the variable in main.
secondo_molt = p_slotr.a - fabs (u_pr[1] + p_slotr.f);

Also, you'll find you need -> not . here, since p_slotr is a pointer
to the struct.

Lastly...
double x_l[2] = {}, x_u[2] ; /* Ricordarsi di inizializzarli. */

you can't do that - there has to be a value in the braces.

Please turn up compiler warning levels to the maximum, and try with a
simpler programme first.
 
A

Aleramo

Thank u for everithing Mark!!! u are very clever!!!
I am Marco and i come from Italy and u?
double x_l[2] = {}, x_u[2] ; /* Ricordarsi di inizializzarli. */
I think u don't know my lenguage cos "/* Ricordarsi di inizializzarli.
*/" means: "U remember to inizialaze it".
I thought the symbols "->" and "." was equivalent but u showed me i
wrong.
Bye bye.
 
M

Mark McIntyre

double x_l[2] = {}, x_u[2] ; /* Ricordarsi di inizializzarli. */
I think u don't know my lenguage cos "/* Ricordarsi di inizializzarli.
*/" means: "U remember to inizialaze it".

Actually I read italian, but the point is, your code didn't compile.

You should always post the actual code you wrote, so that you do not
make different typing mistakes which mislead people trying to help you
..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top