Ejemplo n.º 1
0
void sync_network::calculate_phases(const solve_type solver, const double t, const double step, const double int_step) {
	std::vector<double> next_phases(size(), 0);
	std::vector<void *> argv(2, NULL);

	argv[0] = (void *) this;

	unsigned int number_int_steps = (unsigned int) (step / int_step);

	for (unsigned int index = 0; index < size(); index++) {
		argv[1] = (void *) &index;

		switch(solver) {
			case solve_type::FAST: {
				double result = m_oscillators[index].phase + phase_kuramoto(t, m_oscillators[index].phase, argv);
				next_phases[index] = phase_normalization(result);
				break;
			}
			case solve_type::RK4: {
				differ_state<double> inputs(1, m_oscillators[index].phase);
				differ_result<double> outputs;

				runge_kutta_4(m_callback_solver, inputs, t, t + step, number_int_steps, false, argv, outputs);
				next_phases[index] = phase_normalization( outputs[0].state[0] );

				break;
			}
			case solve_type::RKF45: {
				differ_state<double> inputs(1, m_oscillators[index].phase);
				differ_result<double> outputs;

				runge_kutta_fehlberg_45(m_callback_solver, inputs, t, t + step, 0.00001, false, argv, outputs);
				next_phases[index] = phase_normalization( outputs[0].state[0] );

				break;
			}
			default: {
				throw std::runtime_error("Unknown type of solver");
			}
		}
	}

	/* store result */
	for (unsigned int index = 0; index < size(); index++) {
		m_oscillators[index].phase = next_phases[index];
	}
}
Ejemplo n.º 2
0
void him::m_signal(int n, t_float *const *in, t_float *const *out)
{
    t_float * out0 = out[0];
    t_float * out1 = out[1];
    t_float * out2 = out[2];
    t_float * out3 = out[3];
    t_float * out4 = out[4];
    t_float * out5 = out[5];


    if (regtime)
    {
        for (int i=0; i!=n; ++i)
        {
            runge_kutta_4(dt);
            (*(out0)++)=data[0];
            (*(out1)++)=data[1];
            (*(out2)++)=data[2];
            (*(out3)++)=data[3];
            (*(out4)++)=data[0]*data[2];
            (*(out5)++)=(data[0]*data[0]-data[2]*data[2])*0.5;
        }
    }
    else
    {
        for (int i=0; i!=n; ++i)
        {
            runge_kutta_4(dt/(2*sqrt(data[0]*data[0]+data[2]*data[2])));
            (*(out0)++)=data[0];
            (*(out1)++)=data[1];
            (*(out2)++)=data[2];
            (*(out3)++)=data[3];
            (*(out4)++)=data[0]*data[2];
            (*(out5)++)=(data[0]*data[0]-data[2]*data[2])*0.5;
        }
    }

    if (xfade)
    {
        /* fading */
        out0 = out[0];
        out1 = out[1];
        out2 = out[2];
        out3 = out[3];
        out4 = out[4];
        out5 = out[5];

        t_float * fader = m_fader + n - 1;
        for (int i=0; i!=n; ++i)
        {
            (*(out0)++) *= *fader;
            (*(out1)++) *= *fader;
            (*(out2)++) *= *fader;
            (*(out3)++) *= *fader;
            (*(out4)++) *= *fader;
            (*(out5)++) *= *fader--;
        }

        if (newsystem)
        {
            reset();
            newsystem = false;
        }

        E = newE;
        dt = newdt;
        regtime = newregtime;

        out0 = out[0];
        out1 = out[1];
        out2 = out[2];
        out3 = out[3];
        out4 = out[4];
        out5 = out[5];

        fader = m_fader;
        if (regtime)
        {
            for (int i=0; i!=n; ++i)
            {
                runge_kutta_4(dt);
                (*(out0)++)+= data[0]* *fader;
                (*(out1)++)+= data[1]* *fader;
                (*(out2)++)+= data[2]* *fader;
                (*(out3)++)+= data[3]* *fader;
                (*(out4)++)+= data[0]*data[2]* *fader;
                (*(out5)++)+= (data[0]*data[0]-data[2]*data[2])*0.5* *fader++;
            }
        }
        else
        {
            for (int i=0; i!=n; ++i)
            {
                runge_kutta_4(dt/(2*sqrt(data[0]*data[0]+data[2]*data[2])));
                (*(out0)++)+= data[0]* *fader;
                (*(out1)++)+= data[1]* *fader;
                (*(out2)++)+= data[2]* *fader;
                (*(out3)++)+= data[3]* *fader;
                (*(out4)++)+= data[0]*data[2]* *fader;
                (*(out5)++)+= (data[0]*data[0]-data[2]*data[2])*0.5* *fader++;
            }
        }

        xfade = false;
    }

}
int main()
{
    printf("Welcome to Chiu Industries.  Pendulum Calculator.  NO Driving Force Mode. \n");
    FILE *output_file;
    //declarations of variables
    int number_of_steps;
    double initial_x, initial_v, final_t, E_initial;
    double h;
    double y[2], dydt[2], yout[2];
    double t;
    double gamma;
    double period_initial = 10.0;
    int printInterval = 1;
    int printCounter = 0;
    double count;

   initial_v = -0.1;
    //output_file = fopen("finalq1a_rk4_vinitial_0pt1_gamma_neg0pt1_v1.dat", "w");
    output_file = fopen("dragonfly.dat", "w");
    //for(gamma=-10; gamma<11; gamma++)


    gamma = -0.1;
    for(count = -10; count<10; count++)
    {

        //fprintf(output_file, "#%12.10E \n", gamma);

        final_t = period_initial*10.0;
        h = 0.05;
        number_of_steps = final_t/h;
        initial_x = 0.0;
        //initial_v = 0.1;

        assert(number_of_steps >0);
        //initialise position and velocity
        y[0] = initial_x;
        y[1] = initial_v;
        E_initial = 0.5*y[0]*y[0] + 0.5*y[1]*y[1];
        t=0.0;

        //output(output_file, h, t, y, E_initial, initial_v);



        while(t<=final_t)
        {
            derivatives(t,y,dydt, gamma);
            //euler(y, dydt, 2, t, h, yout);
            runge_kutta_4(y, dydt, 2, t, h, yout, derivatives);
            y[0]=yout[0];
            y[1]=yout[1];
            if(printCounter == printInterval)
            {
            output(output_file, h, t, y, E_initial, initial_v);
            printCounter = 0;
            }
            else{}
            printCounter++;

            t+=h;

        }
     initial_v = initial_v+0.01;
    }
    fclose(output_file);



    return 0;


} //end main program
int main(void)
{
  /***READ SCALE FACTOR DATA AND PREPARE OBJECTS FOR INTERPOLATION ***/
  int i;            //For array manipulation
  
  /*Pointer to scale_factor.data file*/
  FILE *frw;        
  frw = fopen("scale_factor.dat","r");

  /*Variables and arrays to read the data*/
  double cosmictime[NLINESFRW], conftime, scale_factor[NLINESFRW], der_scale_factor[NLINESFRW];

  /*Reading the data*/
  for(i=0; i<NLINESFRW; i++)
    {
      fscanf(frw,"%lf %lf %lf %lf", &cosmictime[i], &conftime, &scale_factor[i], &der_scale_factor[i]);
    }

  /*Free space in memory*/
  fclose(frw);
  
  /*** Initializes objects for interpolation. 1 is for interpolation of scale factor, 2 is for interpolation of derivative of scale factor ***/

  /*Allocate space in memory*/
  gsl_interp_accel *acc1 = gsl_interp_accel_alloc();  //Acceleration type object (for index lookup)
  gsl_interp_accel *acc2 = gsl_interp_accel_alloc();
  gsl_spline *spline1 = gsl_spline_alloc(gsl_interp_cspline, NLINESFRW);  //Spline type object (define interpolation type and space in memory, works for both)
  gsl_spline *spline2 = gsl_spline_alloc(gsl_interp_cspline, NLINESFRW);

  /*Initializes objects for interpolation*/
  gsl_spline_init(spline1, cosmictime, scale_factor, NLINESFRW);  //Initializes spline object for data cosmictime, scale_factor of size NLINES
  gsl_spline_init(spline2, cosmictime, der_scale_factor, NLINESFRW);  //Initializes spline object for data cosmictime, der_scale_factor of size NLINES

  /************************************************************************************/
  
  /***SOLVES GEODESIC EQUATIONS FOR PERTURBED FRW UNIVERSE WITH STATIC PLUMMER POTENTIAL ***/

  /*Initial conditions*/
  mydbl ti = 7.0 ,x0, r = -500.0, p0 = 1.0e-3, pr, lambda = 0.0, energy1, energy, v, difft, difference;
  double difftfrw, aem, aobs;
  x0 = C*ti;
  aem = interpolator(spline1, (double)(1.0*ti), acc1);
  pr = condition_factor(r, aem)*p0;
  energy1 = C*energy_factor(r)*p0;
  v = violation(r, p0, pr, aem);
  difft = (energy1 - energy1)/energy1;
  difftfrw = (aem/aem) - 1.0;
  difference = difft - (mydbl)(1.0*difftfrw);
  

  /*Pointer to file where solution of differential equation will be saved.*/
  FILE *geodesic;
  geodesic = fopen("geodesic_solution.dat","w");

  /*Write line of initial values in file*/
  fprintf(geodesic, "%16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8e %16.8Le\n", lambda, x0, r, p0, pr, energy1, v, difft, difftfrw, difference);

  long long int ii;

  /*Solution of the differential equation*/
  for(ii=0; ii< NSTEPS; ii++)
    {
      runge_kutta_4(spline1, acc1, spline2, acc2, &x0, &r, &p0, &pr, &lambda);
      if((ii%(NSTEPS/NLINES)) == 0)
	{
	  energy = C*energy_factor(r)*p0;
	  difft = (energy - energy1)/energy1;
	  ti = x0/C;
	  aobs = interpolator(spline1, (double)(1.0*ti), acc1);
	  v = violation(r, p0, pr, aobs);
	  difftfrw = (aem/aobs) - 1.0;
	  difference = difft - (mydbl)(1.0*difftfrw);
	  fprintf(geodesic, "%16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8Le %16.8e %16.8Le\n", lambda, x0, r, p0, pr, energy, v, difft, difftfrw, difference);
	} 
    }

  /************************************************************************************/

  /*** Releasing all used space in memory ***/
  fclose(geodesic); //Close file storing the results
  gsl_spline_free(spline1);  //Free memory of spline object
  gsl_spline_free(spline2);
  gsl_interp_accel_free(acc1);  //Free memory of accel object
  gsl_interp_accel_free(acc2);
}
Ejemplo n.º 5
0
void legion_network::calculate_states(const legion_stimulus & stimulus, const solve_type solver, const double t, const double step, const double int_step) {
	std::vector<void *> argv(2, NULL);
	std::vector<differ_result<double> > next_states(size());

	argv[0] = (void *) this;

	unsigned int number_int_steps = (unsigned int) (step / int_step);

	for (unsigned int index = 0; index < size(); index++) {
		argv[1] = (void *) &index;

		differ_state<double> inputs { m_oscillators[index].m_excitatory, m_oscillators[index].m_inhibitory };
		if (m_params.ENABLE_POTENTIAL) {
			inputs.push_back(m_oscillators[index].m_potential);
		}

		switch(solver) {
			case solve_type::FAST: {
				throw std::runtime_error("Forward Euler first-order method is not supported due to low accuracy.");
			}

			case solve_type::RK4: {
				if (m_params.ENABLE_POTENTIAL) {
					runge_kutta_4(&legion_network::adapter_neuron_states, inputs, t, t + step, number_int_steps, false /* only last states */, argv, next_states[index]);
				}
				else {
					runge_kutta_4(&legion_network::adapter_neuron_simplify_states, inputs, t, t + step, number_int_steps, false /* only last states */, argv, next_states[index]);
				}

				break;
			}

			case solve_type::RKF45: {
				if (m_params.ENABLE_POTENTIAL) {
					runge_kutta_fehlberg_45(&legion_network::adapter_neuron_states, inputs, t, t + step, 0.00001, false /* only last states */, argv, next_states[index]);
				}
				else {
					runge_kutta_fehlberg_45(&legion_network::adapter_neuron_simplify_states, inputs, t, t + step, 0.00001, false /* only last states */, argv, next_states[index]);
				}
				
				break;
			}

			default: {
				throw std::runtime_error("Unknown type of solver");
			}
		}

		std::vector<unsigned int> * neighbors = get_neighbors(index);
		double coupling = 0.0;

		for (std::vector<unsigned int>::const_iterator index_neighbor_iterator = neighbors->begin(); index_neighbor_iterator != neighbors->end(); index_neighbor_iterator++) {
			coupling += m_dynamic_connections[index][*index_neighbor_iterator] * heaviside(m_oscillators[*index_neighbor_iterator].m_excitatory - m_params.teta_x);
		}

		delete neighbors;

		m_oscillators[index].m_buffer_coupling_term = coupling - m_params.Wz * heaviside(m_global_inhibitor - m_params.teta_xz);
	}

	differ_result<double> inhibitor_next_state;
	differ_state<double> inhibitor_input { m_global_inhibitor };

	switch (solver) {
		case solve_type::RK4: {
			runge_kutta_4(&legion_network::adapter_inhibitor_state, inhibitor_input, t, t + step, number_int_steps, false /* only last states */, argv, inhibitor_next_state);
			break;
		}
		case solve_type::RKF45: {
			runge_kutta_fehlberg_45(&legion_network::adapter_inhibitor_state, inhibitor_input, t, t + step, 0.00001, false /* only last states */, argv, inhibitor_next_state);
			break;
		}
	}

	m_global_inhibitor = inhibitor_next_state[0].state[0];

	for (unsigned int i = 0; i < size(); i++) {
		m_oscillators[i].m_excitatory = next_states[i][0].state[0];
		m_oscillators[i].m_inhibitory = next_states[i][0].state[1];

		if (m_params.ENABLE_POTENTIAL) {
			m_oscillators[i].m_potential = next_states[i][0].state[2];
		}

		m_oscillators[i].m_coupling_term = m_oscillators[i].m_buffer_coupling_term;
		m_oscillators[i].m_noise = m_noise_distribution(m_generator);
	}
}
int main()
{
    printf("Welcome to Chiu Industries.  q1b finding critical damping value vs initial velocity duffing Pendulum Calculator.  NO Driving Force Mode. \n");
    FILE *output_file;
    FILE *output_file_damp;
    //declarations of variables
    int number_of_steps;
    double initial_x, initial_v, final_t, E_initial;
    double h;
    double y[2], dydt[2], yout[2];
    double t;
    double gamma;
    double period_initial = 10.0;
    int printInterval = 10;
    int printCounter = 0;
    double count;
    int negValueFinder = 0;


    initial_v = -50.1;
    int outsidecount;
    output_file_damp = fopen("damping_vi_5.dat", "w");
    for(outsidecount = 0; outsidecount<11; outsidecount++)
    {
        double damp = 10.0;
        printf("\n %d \n", outsidecount);

        //output_damp(output_file_damp, initial_v, damp);
        //output_file = fopen("finalq1a_rk4_vinitial_0pt1_gamma_neg0pt1_v1.dat", "w");

//THIS IS WHRE COUNT USED TO BE BELOW
        gamma = 0.1;
        for(count = 0; count<3000; count++)
        {
            negValueFinder = 0;
            //THIS IS WHERE COUNT USED TO BE ABOVE


//    filenamemaker(output_file, damp);
            double d = damp;
            char somenumber[50];
            sprintf(somenumber,"%2.5f", d);
            char str_output[20]= {0}, str_two[]=".dat";
            strcpy(str_output, "finalq1b_damp_");       // copies "one" into str_output
            strcat(str_output, somenumber);
            strcat(str_output, "_vi_");
            strcat(str_output, ".dat");    //  attaches str_two to str_output
            //printf("The Output string is: %s", str_output);
           // output_file = fopen(str_output, "w");




            t = 0.0;
           // fprintf(output_file, "Damping constant is: \n");
           // fprintf(output_file, "#%12.5E \n", damp);
            //fprintf(output_file, "#%12.10E \n", gamma);

            final_t = period_initial*10.0;
            h = 0.05;
            number_of_steps = final_t/h;
            initial_x = 0.0;
            //initial_v = 0.1;

            assert(number_of_steps >0);
            //initialise position and velocity
            y[0] = initial_x;
            y[1] = initial_v;
            E_initial = 0.5*y[0]*y[0] + 0.5*y[1]*y[1];
            t=0.0;

            //output(output_file, h, t, y, E_initial, initial_v);



            while(t<=final_t)
            {
                derivatives(t,y,dydt, damp);
                //euler(y, dydt, 2, t, h, yout);
                runge_kutta_4(y, dydt, 2, t, h, yout, derivatives);
                y[0]=yout[0];
                y[1]=yout[1];

                if(y[0]<0)
                {
                    negValueFinder++;
                }
                if(printCounter == printInterval)
                {
                   // output(output_file, h, t, y, E_initial, damp);
                    printCounter = 0;
                }
                else {}
                printCounter++;

                t+=h;

            }
            if(negValueFinder==0)
            {
                printf("At value of %lf is critical damping!", damp);
               // fprintf(output_file, "\n At value of %lf is critical damping!", damp);
                 output_damp(output_file_damp, initial_v, damp);
                 break;
                //fclose(output_file);
                //return 0;
            }
            //initial_v = initial_v+1.0;
            damp = damp + 0.01;
            //fclose(output_file);
        }

        initial_v=initial_v+10.0;

    }
     fclose(output_file_damp);




    return 0;


} //end main program