コード例 #1
0
ファイル: system.c プロジェクト: healther/pynbody
struct Particle* randomsystem(const int n){
    mt_seed32(42424242);
    struct Particle* particles;

    particles = (struct Particle *)malloc(n*sizeof(struct Particle));
    for (int i = 0; i < n; ++i)
    {
        particles[i].position.x = mt_ldrand()-.5;
        particles[i].position.y = mt_ldrand()-.5;
        particles[i].position.z = mt_ldrand()-.5;
        particles[i].velocity.x = mt_ldrand()-.5;
        particles[i].velocity.y = mt_ldrand()-.5;
        particles[i].velocity.z = mt_ldrand()-.5;
        particles[i].acceleration.x = 0.;
        particles[i].acceleration.y = 0.;
        particles[i].acceleration.z = 0.;
        particles[i].mass = mt_ldrand();
        // particles[i].mass = 1.;
    }
    printf("Setup done\n");

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            if(i!=j){
                calculateForcePP(&particles[i], &particles[j]);
            }
        }
    }

    struct Force acc = {0.,0.,0.};
    for (int i = 0; i < n; ++i)
    {
        acc.x += particles[i].acceleration.x;
        acc.y += particles[i].acceleration.y;
        acc.z += particles[i].acceleration.z;
    }

    printf("%1.16e %1.16e %1.16e\n", acc.x, acc.y, acc.z);

    return particles;
}
コード例 #2
0
ファイル: system.c プロジェクト: healther/pynbody
struct Particle* randomsystemtree(const int n){
    mt_seed32(42424242);
    struct Particle* particles;

    particles = (struct Particle *)malloc(n*sizeof(struct Particle));
    for (int i = 0; i < n; ++i)
    {
        particles[i].position.x = mt_ldrand()-.5;
        particles[i].position.y = mt_ldrand()-.5;
        particles[i].position.z = mt_ldrand()-.5;
        particles[i].velocity.x = mt_ldrand()-.5;
        particles[i].velocity.y = mt_ldrand()-.5;
        particles[i].velocity.z = mt_ldrand()-.5;
        particles[i].acceleration.x = 0.;
        particles[i].acceleration.y = 0.;
        particles[i].acceleration.z = 0.;
        particles[i].mass = mt_ldrand();
        // particles[i].mass = 1.;
    }

    struct Tree* tree = maketree(particles, n, .3);
    generateMultipole(tree->root);

    int interactions = 0;
    for (int i = 0; i < n; ++i)
    {
        interactions += calculateForcePN(&particles[i], tree->root);
    }
    printf("%d / %d\n", interactions, n*n);

    struct Force acc = {0.,0.,0.};
    for (int i = 0; i < n; ++i)
    {
        acc.x += particles[i].acceleration.x;
        acc.y += particles[i].acceleration.y;
        acc.z += particles[i].acceleration.z;
    }

    printf("%1.16e %1.16e %1.16e\n", acc.x, acc.y, acc.z);
    return particles;
}
コード例 #3
0
int main(
    int			argc,		/* Argument count */
    char *		argv[])		/* Argument vector */
    {
    rd_empirical_control*
			control = NULL;	/* Control if empirical distr. */
    size_t		how_many;	/* How many numbers to generate */
    size_t		i;		/* Loop index */
    size_t		needed_params = 0;
					/* Number of params needed by distr */
    size_t		n_params;	/* Number of parameters */
    size_t		n_probs = 0;	/* Number of empirical probabilites */
    double *		params;		/* Parameters of distribution */
    double *		probs = NULL;	/* Probabilities for empirical */
    double		ran_value = 0.0;
					/* Value generated by PRNG */
    long		seed;		/* Seed for PRNG */
    double *		values = NULL;	/* Values for empirical */

    if (argc <= PARAM_OFFSET)
	usage();

    seed = atoi (argv[SEED_PARAM]);
    how_many = atoi (argv[COUNT_PARAM]);

    n_params = argc - PARAM_OFFSET;
    params = (double *) malloc (sizeof (double) * n_params);
    if (params == NULL)
	{
	(void) fprintf (stderr, "rdtest: can't malloc params\n");
	return 1;
	}
    for (i = 0;  i < n_params;  i++)
	params[i] = atof (argv[i + PARAM_OFFSET]);

    if (strcmp (argv[DISTR_PARAM], "iuniform") == 0)
	needed_params = 2;
    else if (strcmp (argv[DISTR_PARAM], "uniform") == 0)
	needed_params = 2;
    else if (strcmp (argv[DISTR_PARAM], "exponential") == 0)
	needed_params = 1;
    else if (strcmp (argv[DISTR_PARAM], "erlang") == 0)
	{
	if (n_params < 2  ||  params[0] < 1.0)
	    usage();
	needed_params = 2;
	}
    else if (strcmp (argv[DISTR_PARAM], "weibull") == 0)
	needed_params = 2;
    else if (strcmp (argv[DISTR_PARAM], "normal") == 0)
	needed_params = 2;
    else if (strcmp (argv[DISTR_PARAM], "lognormal") == 0)
	needed_params = 2;
    else if (strcmp (argv[DISTR_PARAM], "triangular") == 0)
	needed_params = 3;
    else if (strcmp (argv[DISTR_PARAM], "empirical") == 0)
	{
	if (n_params % 2 != 0  ||  n_params < 4)
	    usage();
	n_probs = n_params / 2;
	probs = (double *) malloc (sizeof (double) * n_probs);
	values = (double *) malloc (sizeof (double) * (n_probs + 1));
	if (probs == NULL  ||  values == NULL)
	    {
	    (void) fprintf (stderr, "rdtest: can't malloc probs/values\n");
	    return 1;
	    }
	for (i = 0;  i < n_probs;  i++)
	    {
	    values[i] = params[i * 2];
	    probs[i] = params[i * 2 + 1];
	    if (probs[i] < 0)
		{
		(void)fprintf(stderr, "rdtest: negative probability given\n");
		exit(2);
		}
	    }
	values[n_probs] = 0.0;		/* Just for cleanliness */
	needed_params = n_params;
	control = rd_empirical_setup(n_probs, probs, values);
	}
    else if (strcmp(argv[DISTR_PARAM], "continuous_empirical") == 0)
	{
	if (n_params % 2 == 0  ||  n_params < 5)
	    usage();
	n_probs = (n_params - 1) / 2;
	probs = (double *) malloc (sizeof (double) * n_probs);
	values = (double *) malloc (sizeof (double) * (n_probs + 1));
	if (probs == NULL  ||  values == NULL)
	    {
	    (void) fprintf (stderr, "rdtest: can't malloc probs/values\n");
	    return 1;
	    }
	for (i = 0;  i < n_probs;  i++)
	    {
	    values[i] = params[i * 2];
	    probs[i] = params[i * 2 + 1];
	    if (probs[i] < 0)
		{
		(void)fprintf(stderr, "rdtest: negative probability given\n");
		exit(2);
		}
	    }
	values[n_probs] = params[n_probs * 2];
	needed_params = n_params;
	control = rd_empirical_setup(n_probs, probs, values);
	}
    else
	usage();

    if (n_params != needed_params)
	usage();

    /*
     * Pick a seed
     */
    if (seed == 0)
	mt_goodseed();
    else
	mt_seed32(seed);

    for (i = 0;  i < how_many;  i++)
	{
	if (strcmp (argv[DISTR_PARAM], "iuniform") == 0)
	    ran_value = rd_iuniform ((long)params[0], (long)params[1]);
	else if (strcmp (argv[DISTR_PARAM], "uniform") == 0)
	    ran_value = rd_uniform (params[0], params[1]);
	else if (strcmp (argv[DISTR_PARAM], "exponential") == 0)
	    ran_value = rd_exponential (params[0]);
	else if (strcmp (argv[DISTR_PARAM], "erlang") == 0)
	    ran_value = rd_erlang ((int) params[0], params[1]);
	else if (strcmp (argv[DISTR_PARAM], "weibull") == 0)
	    ran_value = rd_weibull (params[0], params[1]);
	else if (strcmp (argv[DISTR_PARAM], "normal") == 0)
	    ran_value = rd_normal (params[0], params[1]);
	else if (strcmp (argv[DISTR_PARAM], "lognormal") == 0)
	    ran_value = rd_lognormal (params[0], params[1]);
	else if (strcmp (argv[DISTR_PARAM], "triangular") == 0)
	    ran_value = rd_triangular (params[0], params[1], params[2]);
	else if (strcmp (argv[DISTR_PARAM], "empirical") == 0)
	    ran_value = rd_double_empirical (control);
	else if (strcmp (argv[DISTR_PARAM], "continuous_empirical") == 0)
	    ran_value = rd_continuous_empirical (control);
	(void) printf ("%f\n", ran_value);
	}
    return 0;
    }