Ejemplo n.º 1
0
//TODO: going to need logic to handle incomplete config files
void parse_file( char type, char *json, params_t *par ){
    /* Parse file and populate applicable data structures */
    uint64_t i;
    JSON_Value *root_value = NULL;
    JSON_Object *root_object;
    JSON_Array *array;

    if( type == 'f' )
        root_value = json_parse_file_with_comments( json );
    else
        root_value = json_parse_string_with_comments( json );

    root_object = json_value_get_object( root_value );

    par->nQ = (uint64_t) json_object_dotget_number( root_object, "scalars.nq" );
    par->L = (uint64_t) json_object_dotget_number( root_object, "scalars.lrgs" );
    par->res = (uint64_t) json_object_dotget_number( root_object, "scalars.res" );
    par->T = json_object_dotget_number( root_object, "scalars.t" );
    par->dt = json_object_dotget_number( root_object, "scalars.dt" );

    par->al   = (double *)malloc( (par->nQ)*sizeof(double) );
    par->de   = (double *)malloc( (par->nQ)*sizeof(double) );
    par->be   = (double *)malloc( ((par->nQ)*((par->nQ)-1)/2)*sizeof(double) );

    array = json_object_dotget_array( root_object, "coefficients.alpha" );
    if( array != NULL ){
        for( i = 0; i < json_array_get_count(array); i++ ){
            (par->al)[i] = -json_array_get_number( array, i );
        }
    }

    array = json_object_dotget_array( root_object, "coefficients.beta" );
    if( array != NULL ){
        for( i = 0; i < json_array_get_count(array); i++ ){
            (par->be)[i] = -json_array_get_number( array, i );
        }
    }

    array = json_object_dotget_array( root_object, "coefficients.delta" );
    if( array != NULL ){
        for( i = 0; i < json_array_get_count(array); i++ ){
            (par->de)[i] = -json_array_get_number( array, i );
        }
    }

    json_value_free( root_value );
}
Ejemplo n.º 2
0
/* Testing correctness of parsed values */
void test_suite_2(void) {
    JSON_Value *root_value;
    JSON_Object *object;
    JSON_Array *array;
    int i;
    const char *filename = "tests/test_2.txt";
    printf("Testing %s:\n", filename);
    root_value = json_parse_file(filename);
    TEST(root_value);
    TEST(json_value_get_type(root_value) == JSONObject);
    object = json_value_get_object(root_value);
    TEST(STREQ(json_object_get_string(object, "string"), "lorem ipsum"));
    TEST(STREQ(json_object_get_string(object, "utf string"), "lorem ipsum"));
    TEST(json_object_get_number(object, "positive one") == 1.0);
    TEST(json_object_get_number(object, "negative one") == -1.0);
    TEST(json_object_get_number(object, "hard to parse number") == -0.000314);
    TEST(json_object_get_boolean(object, "boolean true") == 1);
    TEST(json_object_get_boolean(object, "boolean false") == 0);
    TEST(json_value_get_type(json_object_get_value(object, "null")) == JSONNull);
    
    array = json_object_get_array(object, "string array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    
    array = json_object_get_array(object, "x^2 array");
    if (array != NULL) {
        for (i = 0; i < json_array_get_count(array); i++) {
            TEST(json_array_get_number(array, i) == (i * i));
        }
    } else {
        tests_failed++;
    }
    
    TEST(json_object_get_array(object, "non existent array") == NULL);
    TEST(STREQ(json_object_dotget_string(object, "object.nested string"), "str"));
    TEST(json_object_dotget_boolean(object, "object.nested true") == 1);
    TEST(json_object_dotget_boolean(object, "object.nested false") == 0);
    TEST(json_object_dotget_value(object, "object.nested null") != NULL);
    TEST(json_object_dotget_number(object, "object.nested number") == 123);
    
    TEST(json_object_dotget_value(object, "should.be.null") == NULL);
    TEST(json_object_dotget_value(object, "should.be.null.") == NULL);
    TEST(json_object_dotget_value(object, ".") == NULL);
    TEST(json_object_dotget_value(object, "") == NULL);
    
    array = json_object_dotget_array(object, "object.nested array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    TEST(json_object_dotget_boolean(object, "nested true"));    
    json_value_free(root_value);
}
Ejemplo n.º 3
0
/* Testing correctness of parsed values */
void test_suite_2(JSON_Value *root_value) {
    JSON_Object *root_object;
    JSON_Array *array;
    size_t i;
    TEST(root_value);
    TEST(json_value_get_type(root_value) == JSONObject);
    root_object = json_value_get_object(root_value);
    TEST(STREQ(json_object_get_string(root_object, "string"), "lorem ipsum"));
    TEST(STREQ(json_object_get_string(root_object, "utf string"), "lorem ipsum"));
    TEST(STREQ(json_object_get_string(root_object, "utf-8 string"), "あいうえお"));
    TEST(STREQ(json_object_get_string(root_object, "surrogate string"), "lorem𝄞ipsum𝍧lorem"));
    TEST(json_object_get_number(root_object, "positive one") == 1.0);
    TEST(json_object_get_number(root_object, "negative one") == -1.0);
    TEST(json_object_get_number(root_object, "hard to parse number") == -0.000314);
    TEST(json_object_get_boolean(root_object, "boolean true") == 1);
    TEST(json_object_get_boolean(root_object, "boolean false") == 0);
    TEST(json_value_get_type(json_object_get_value(root_object, "null")) == JSONNull);
    
    array = json_object_get_array(root_object, "string array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    
    array = json_object_get_array(root_object, "x^2 array");
    if (array != NULL) {
        for (i = 0; i < json_array_get_count(array); i++) {
            TEST(json_array_get_number(array, i) == (i * i));
        }
    } else {
        tests_failed++;
    }
    
    TEST(json_object_get_array(root_object, "non existent array") == NULL);
    TEST(STREQ(json_object_dotget_string(root_object, "object.nested string"), "str"));
    TEST(json_object_dotget_boolean(root_object, "object.nested true") == 1);
    TEST(json_object_dotget_boolean(root_object, "object.nested false") == 0);
    TEST(json_object_dotget_value(root_object, "object.nested null") != NULL);
    TEST(json_object_dotget_number(root_object, "object.nested number") == 123);
    
    TEST(json_object_dotget_value(root_object, "should.be.null") == NULL);
    TEST(json_object_dotget_value(root_object, "should.be.null.") == NULL);
    TEST(json_object_dotget_value(root_object, ".") == NULL);
    TEST(json_object_dotget_value(root_object, "") == NULL);
    
    array = json_object_dotget_array(root_object, "object.nested array");
    if (array != NULL && json_array_get_count(array) > 1) {
        TEST(STREQ(json_array_get_string(array, 0), "lorem"));
        TEST(STREQ(json_array_get_string(array, 1), "ipsum"));
    } else {
        tests_failed++;
    }
    TEST(json_object_dotget_boolean(root_object, "nested true"));
    
    TEST(STREQ(json_object_get_string(root_object, "/**/"), "comment"));
    TEST(STREQ(json_object_get_string(root_object, "//"), "comment"));
}
Ejemplo n.º 4
0
/*------------------------------------------------------------------
- Config file format - simplify, don't need xml, but like the structure
{
    "scalars" : {
        "nq" : 3,
        "lrgs" : 4,
        "print" : true
        "t" : 10.0,
        "dt" : 0.1
    },
    "coefficients" : {
        "alpha" : [0.112, 0.234, 0.253],
        "beta" : [0.453, 0.533, -0.732, 0.125, -0.653, 0.752],
        "delta" : [1.0, 1.0, 1.0]
    }
}
------------------------------------------------------------------*/
int main( int argc, char **argv ){
    double *hz, *hhxh;     /* hamiltonian components */
    double *al, *be, *de; 
    fftw_complex *psi;   /* State vector */
    fftw_complex factor;
    double T = 10.0, dt = 0.1;
    uint64_t i, j, k, bcount;
    uint64_t nQ=3, N, L=4, dim;
    int *fft_dims, prnt=0;
    uint64_t testi, testj;
    int dzi, dzj; //TODO: consider using smaller vars for flags and these
    fftw_plan plan;
    
    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                         Parse configuration file
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    //TODO: going to need logic to handle incomplete config files
    if( argc < 2 ){
        fprintf( stderr, "Need a json configuration file. Terminating...\n" );
        return 1;
    }

    /* Parse file and populate applicable data structures */
    {
        JSON_Value *root_value = NULL;
        JSON_Object *root_object;
        JSON_Array *array;

        root_value = json_parse_file_with_comments( argv[1] );
        root_object = json_value_get_object( root_value );

        nQ = (uint64_t) json_object_dotget_number( root_object, "scalars.nq" );
        prnt = json_object_dotget_boolean( root_object, "scalars.print" );
        L = (uint64_t) json_object_dotget_number( root_object, "scalars.lrgs" );
        T = json_object_dotget_number( root_object, "scalars.t" );
        dt = json_object_dotget_number( root_object, "scalars.dt" );

        al   = (double *)malloc( nQ*sizeof(double) );
        de   = (double *)malloc( nQ*sizeof(double) );
        be   = (double *)malloc( (nQ*(nQ-1)/2)*sizeof(double) );

        array = json_object_dotget_array( root_object, "coefficients.alpha" );
        if( array != NULL ){
            for( i = 0; i < json_array_get_count(array); i++ ){
                al[i] = -json_array_get_number( array, i );
            }
        }

        array = json_object_dotget_array( root_object, "coefficients.beta" );
        if( array != NULL ){
            for( i = 0; i < json_array_get_count(array); i++ ){
                be[i] = -json_array_get_number( array, i );
            }
        }

        array = json_object_dotget_array( root_object, "coefficients.delta" );
        if( array != NULL ){
            for( i = 0; i < json_array_get_count(array); i++ ){
                de[i] = -json_array_get_number( array, i );
            }
        }

        json_value_free( root_value );
    }

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        Compute the Hamiltonian and state vector for the simulation
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

    /*
        Create state vector and initialize to 1/sqrt(2^n)*(|00...0> + ... + |11...1>)
        TODO: keep track of local size and local base
    */
    dim = 1 << nQ;
    factor = 1.0/sqrt( dim );
    
    fft_dims = (int *)malloc( nQ*sizeof(int) );
    psi  = (fftw_complex *)malloc( (dim)*sizeof(fftw_complex) );
    hz   = (double *)calloc( (dim),sizeof(double) );
    hhxh = (double *)calloc( (dim),sizeof(double) );

    for( i = 0; i < nQ; i++ ){
        fft_dims[i] = 2;
    }

    plan = fftw_plan_dft( nQ, fft_dims, psi, psi, FFTW_FORWARD, FFTW_MEASURE );

    /*
        Assemble Hamiltonian and state vector
    */
    for( k = 0; k < dim; k++ ){
        //TODO: when parallelized, k in dzi test will be ~(k + base)

        bcount = 0;
        for( i = 0; i < nQ; i++ ){
            testi = 1 << (nQ - i - 1);
            dzi = ((k/testi) % 2 == 0) ? 1 : -1;

            hz[k] += al[i] * dzi;
            hhxh[k] += de[i] * dzi;

            for( j = i; j < nQ; j++ ){
                testj = 1 << (nQ - j - 1);
                dzj = ((k/testj) % 2 == 0) ? 1 : -1;

                hz[k] += be[bcount] * dzi * dzj;
                bcount++;
            }
        }
            
        psi[k] = factor;
    }

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                            Run the Simulation
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    fftw_complex cz, cx;
    double t;
    N = (uint64_t)(T / dt);
    for( i = 0; i < N; i++ ){
        t = i*dt;
        //t0 = (i-1)*dt;

        //Time-dependent coefficients
        cz = (-dt * I)*t/(2.0*T);
        cx = (-dt * I)*(1 - t/T);

        //Evolve system
        expMatTimesVec( psi, hz, cz, dim ); //apply Z part
        fftw_execute( plan );
        expMatTimesVec( psi, hhxh, cx, dim ); //apply X part
        fftw_execute( plan );
        expMatTimesVec( psi, hz, cz, dim ); //apply Z part
        
        /* 
            TODO: can probably get some minor speedup by incorporating this 
                  into expMatTimesVec if needed 
        */
        scaleVec( psi, 1.0/dim, dim );
    }
    fftw_destroy_plan( plan );

    /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                        Check solution and clean up
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
    //TODO: locally, collect all local largests on one
    //      node, find k largest from that subset
    if( prnt && nQ < 6 ){
        for( i = 0; i < dim; i++ ){
            printf( "psi[%d] = (%f, %f)\t%f\n", 
		    i,
		    creal( psi[i] ), 
		    cimag( psi[i] ), 
		    cabs( psi[i]*psi[i] ) );
        }
    } else {
        uint64_t *largest = (uint64_t *)calloc( L, sizeof(uint64_t) );
        findLargest( largest, psi, dim, L );
        for( i = 0; i < L; ++i ){
            printf( "psi[%d] = (%f, %f)\t%f\n",
		    i,
		    creal( psi[largest[L-1-i]] ), 
		    cimag( psi[largest[L-1-i]] ),
		    cabs( psi[largest[L-1-i]]*psi[largest[L-1-i]] ) );
        }
        free( largest );
    }

    /*
        Free work space.
    */
    fftw_free( psi );
    free( fft_dims );
    free( hz );
    free( hhxh );

    return 0;
}
Ejemplo n.º 5
0
int main(){
    int i, nQ, lrgs;
    double t, dt;
    double *al, *be, *de;
    JSON_Value *root_value = NULL;
    JSON_Object *root_object;
    //JSON_Object *scalar_object;
    //JSON_Object *coeff_object;
    JSON_Array *array;

    root_value = json_parse_file_with_comments( "config.json" );
    root_object = json_value_get_object( root_value );
    //scalar_object = json_value_get_object( root_object,  );
    //coeff_object = json_value_get_object( root_value );

    nQ = (int)json_object_dotget_number( root_object, "scalars.NQ" );
    lrgs = (int)json_object_dotget_number( root_object, "scalars.LRGS" );
    t = json_object_dotget_number( root_object, "scalars.T" );
    dt = json_object_dotget_number( root_object, "scalars.DT" );

    al = (double *)malloc( nQ*sizeof(double) );
    be = (double *)malloc( ((nQ*(nQ-1))/2)*sizeof(double) );
    de = (double *)malloc( nQ*sizeof(double) );

    array = json_object_dotget_array( root_object, "coefficients.ALPHA" );
    if( array != NULL ){
        for( i = 0; i < json_array_get_count(array); i++ ){
            al[i] = json_array_get_number( array, i );
        }
    }

    array = json_object_dotget_array( root_object, "coefficients.BETA" );
    if( array != NULL ){
        for( i = 0; i < json_array_get_count(array); i++ ){
            be[i] = json_array_get_number( array, i );
        }
    }

    array = json_object_dotget_array( root_object, "coefficients.DELTA" );
    if( array != NULL ){
        for( i = 0; i < json_array_get_count(array); i++ ){
            de[i] = json_array_get_number( array, i );
        }
    }

    json_value_free(root_value);

    printf("nQ = %d\n", nQ);
    printf("lrgs = %d\n", lrgs);
    printf("t = %f\n", t);
    printf("dt = %f\n\n", dt);

    for( i = 0; i < nQ; i++ ){
        printf("al[%d] = %f ", i, al[i]);
    }
    printf("\n\n");

    for( i = 0; i < (nQ*nQ-nQ)/2; i++ ){
        printf("be[%d] = %f ", i, be[i]);
    }
    printf("\n\n");

    for( i = 0; i < nQ; i++ ){
        printf("de[%d] = %f ", i, de[i]);
    }
    printf("\n\n");

    free(al); free(be); free(de);

    return 0;
}
Ejemplo n.º 6
0
int parse_blueprint_json(JSON_Object *blueprint, blueprint_t *bp)
{
	bstring Name, bp_name, name, game_version;
	uint32_t resource_cost[5];

	bp->revision = json_object_get_number(blueprint, "blueprintVersion");

	bp_name = bfromcstr(json_object_get_string(blueprint, "blueprintName"));
	name = bfromcstr(json_object_get_string(blueprint, "Name"));
	game_version = bfromcstr(json_object_get_string(blueprint, "GameVersion"));

	bstring param1 = bfromcstr(json_object_get_string(blueprint, "Parameter1"));
	bstring param2 = bfromcstr(json_object_get_string(blueprint, "Parameter2"));
	bstring lpos = bfromcstr(json_object_get_string(blueprint, "LocalPosition"));
	bstring lrot = bfromcstr(json_object_get_string(blueprint, "LocalRotation"));

	bp->name = name;
	bp->blueprint_name = bp_name;
	bp->Name = Name;
	bp->game_version = game_version;

	const char* cpos = json_object_get_string(blueprint, "LocalPosition");
	bstring pos = bfromcstr(cpos);
	struct bstrList *pval = bsplit(pos, ',');
	for (int i = 0; i < 3; i++)
	{
		char *ptr;
		char *str = bstr2cstr(pval->entry[i], '0');
		uint32_t n = strtol(str, &ptr, 10);
	}

	const char* crot = json_object_get_string(blueprint, "LocalRotation");
	bstring rot = bfromcstr(crot);
	struct bstrList *rval = bsplit(rot, ',');
	for (int i = 0; i < 4; i++)
	{
		char *ptr;
		char *str = bstr2cstr(rval->entry[i], '0');
		double dbl = strtod(str, &ptr);
		free(str);
		bp->local_rotation[i] = dbl;
	}

	bp->parameter1 = param1;
	bp->parameter2 = param2;

	bp->design_changed = json_object_get_boolean(blueprint, "designChanged");

	bp->id = json_object_get_number(blueprint, "Id");
	bp->force_id = json_object_get_number(blueprint, "ForceId");
	bp->item_number = json_object_get_number(blueprint, "ItemNumber");

	JSON_Array *jresource_cost = json_object_get_array(blueprint, "ResourceCost");
	for (int i = 0; i < 5; i++)
		bp->resource_cost[i] = (uint32_t) json_array_get_number(jresource_cost, i);

	JSON_Array *min_coords = json_object_get_array(blueprint, "MinCords");
	for (int i = 0; i < 3; i++)
		bp->min_coords[i] = (int32_t) json_array_get_number(min_coords, i);

	JSON_Array *max_coords = json_object_get_array(blueprint, "MaxCords");
	for (int i = 0; i < 3; i++)
		bp->max_coords[i] = (int32_t) json_array_get_number(max_coords, i);

	JSON_Array *csi = json_object_get_array(blueprint, "CSI");
	for (int i = 0; i < 40; i++)
		bp->constructable_special_info[i] = json_array_get_number(csi, i);

	bp->last_alive_block = (uint32_t) json_object_get_number(blueprint, "LastAliveBlock");

	JSON_Array *palette = json_object_get_array(blueprint, "COL");
	for(int i = 0; i < 32; i++)
	{
		uint32_t rbga = 0;

		const char* ccolor = json_array_get_string(palette, i);
		bstring color = bfromcstr(ccolor);

		// Create array of substrings
		struct bstrList *values = bsplit(color, ',');
		for (int n = 0; n < 4; n++)
		{
			char *ptr;
			char *str = bstr2cstr(values->entry[n], '0');
			double dbl = strtod(str, &ptr);
			free(str);
			bp->color_palette[i].array[n] = round(dbl * 255.0);
		}
		bstrListDestroy(values);
		bdestroy(color);
	}

	JSON_Array *material = json_object_get_array(blueprint, "BlockIds");
	JSON_Array *position = json_object_get_array(blueprint, "BLP");
	JSON_Array *rotation = json_object_get_array(blueprint, "BLR");
	JSON_Array *color = json_object_get_array(blueprint, "BCI");

	JSON_Array *bp1 = json_object_get_array(blueprint, "BP1");
	JSON_Array *bp2 = json_object_get_array(blueprint, "BP2");
	JSON_Array *block_string_ids = json_object_get_array(blueprint, "BlockStringDataIds");
	JSON_Array *block_data = json_object_get_array(blueprint, "BlockStringData");

	bp->total_block_count = (uint32_t) json_object_get_number(blueprint, "TotalBlockCount");
	bp->main_block_count = (uint32_t) json_object_get_number(blueprint, "BlockCount");
	bp->blocks = calloc(bp->main_block_count, sizeof(block_t));

	for (int i = 0; i < bp->main_block_count; i++)
	{
		block_t *act = &bp->blocks[i];

		act->material = (uint32_t) json_array_get_number(material, i);
		act->rotation = (uint32_t) json_array_get_number(rotation, i);
		act->color = (uint32_t) json_array_get_number(color, i);

		bstring pos_string = bfromcstr(json_array_get_string(position, i));
		struct bstrList *pos_list = bsplit(pos_string, ',');
		for (int n = 0; n < 3; n++)
		{
			char *ptr;
			char *str = bstr2cstr(pos_list->entry[n], '0');
			double dbl = strtod(str, &ptr);
			uint32_t val = round(dbl);
			act->position.array[n] = val;
			free(str);
		}
		bstrListDestroy(pos_list);
		bdestroy(pos_string);

		bstring bp1_string = bfromcstr(json_array_get_string(bp1, i));
		struct bstrList *bp1_values = bsplit(bp1_string, ',');
		for (int n = 0; n < 4; n++)
		{
			char *ptr;
			char *str = bstr2cstr(bp1_values->entry[n], '0');
			double dbl = strtod(str, &ptr);
			act->bp1[n] = dbl;
		}

		bstring bp2_string = bfromcstr(json_array_get_string(bp2, i));
		struct bstrList *bp2_values = bsplit(bp2_string, ',');
		for (int n = 0; n < 4; n++)
		{
			char *ptr;
			char *str = bstr2cstr(bp2_values->entry[n], '0');
			double dbl = strtod(str, &ptr);
			uint32_t val = round(dbl);
			act->bp2[n] = val;
		}

		// Only used for lookup, not saved after that since it has no further semantic value
		const char *cb1 = json_array_get_string(bp1, i);

		if (act->bp1[3] != 0)
		{
			for (int n = 0; n < json_array_get_count(block_string_ids); n++)
			{
				uint32_t test_id = json_array_get_number(block_string_ids, n);
				if (test_id == 0)
				{
					break;
				}
				if (test_id == act->bp1[3])
				{
					// BlockStringData at index n is the one we want
					act->string_data = bfromcstr(json_array_get_string(block_data, n));
				}
			}
		}
	}

	JSON_Array *subconstructables = json_object_get_array(blueprint, "SCs");
	bp->num_sc = json_array_get_count(subconstructables);
	bp->SCs = calloc(bp->num_sc, sizeof(blueprint_t));
	for (int i = 0; i < bp->num_sc; i++)
	{
		JSON_Value *sc_json = json_array_get_value(subconstructables, i);
		JSON_Object *sc_obj = json_value_get_object(sc_json);
		int retval = parse_blueprint_json(sc_obj, &bp->SCs[i]);
		if (retval != 0)
			return retval;
	}

	return 0;
}