Example #1
0
double totalEnergy(particle * particles, int n){
	double out = 0;
	double kinetic = kineticEnergy(particles, n);
	double potential = potentialEnergy(particles, n);
	//printf("kinetic = %g, potential = %g\n", kinetic, potential);
	out += kineticEnergy(particles, n);
	out += potentialEnergy(particles, n);
	return out;
}
 static double potentialEnergy_proxy(const ModelHandler & model,
                                     DataHandler & data,
                                     const VectorXd_fx & q,
                                     const bool update_kinematics = true)
 {
   return potentialEnergy(*model,*data,q,update_kinematics);
 }
Example #3
0
void StatisticsSampler::saveToFile(System &system, ofstream &file)
{
    // Convert all energy per atom, use eV
    file << setw(15) << UnitConverter::timeToSI(system.steps()) << "      "
         << setw(15) << UnitConverter::timeToSI(system.time()) << "      "
         << setw(15) << UnitConverter::temperatureToSI(temperature()) << "      "
         << setw(15) << UnitConverter::energyToEv(kineticEnergy()/system.atoms().size()) << "      "
         << setw(15) << UnitConverter::energyToEv(potentialEnergy()/system.atoms().size()) << "      "
         << setw(15) << UnitConverter::energyToEv(totalEnergy()/system.atoms().size()) << "      "
         << setw(15) << diffusionConstant() << "      "
         << setw(15) << m_rSquared << std::endl;
}
Example #4
0
int testEnergy(){
	int failed = 0;
	
	int n = 3;
	particle * particles = malloc(n * sizeof(particle));
	initialize_linearx(particles, n);
	double kinetic = kineticEnergy(particles, n);
	if(kinetic != 0){
		failed ++;
		printf("nonzero kinetic energy %lf found\n", kinetic);
	}
	double potential = potentialEnergy(particles, n);
	if(potential != -0.25){
		failed ++;
		printf("unexpected potential energy %lf found. expected -0.25\n", potential);
	}
	//double distances, expect force ~2^-9
	particles[0]. x = 0;
	particles[1].x = 2;
	particles[2].x = 4;
	potential = potentialEnergy(particles, n);
	if(potential != -0.00048828125){
		failed ++;
		printf("unexpected potential energy %lf found. expected -0.00048828125\n", potential);
	}
	//equilateral triangle
	initialize_equilateral(particles, n);
	potential = potentialEnergy(particles, n);
	if(potential != 1.375){
		failed ++;
		printf("unexpected potential energy %g found. expected 1.375\n", potential - 1.375);
	}
	
	
	free(particles);
	if(failed == 0){
		printf("energy test passed\n");
	}
	return !failed;
}
Example #5
0
void test_07() {
    //// WRITES SYSTEM ENERGY TO FILE

    int width = 600;
    int height = 400;
    Object* A = new Player;
    Object* B = new Object;
    Object* C = new Object;
    Object* D = new Object;

    Window window = Window(width,height,NULL,30,vis::AUTO_SIZE_UNIVERSE);

    // Set them apart, and on a collision course
    A->set_position(2, 0);
    A->set_velocity(0,0);
    A->set_radius(0.5);
    A->set_mass(5);
    A->bouncyness = 1;

    B->set_position(2, 2);
    B->set_velocity(0, -1);
    B->set_mass(2);
    //B->bouncyness = 0.7;

    C->set_position(-2, -2);
    C->set_mass(2);
    //C->bouncyness = 0.7;

    D->set_position(-4, -4);
    D->set_mass(2);
    //D->bouncyness = 0.7;



    // Generate a universe
    Universe universe(width/window.pixRatio, height/window.pixRatio);
    window.bindUniverse(&universe);
    // Add them to the universe
    universe.add_object(A);
    universe.add_object(B);
    universe.add_object(C);
    universe.add_object(D);

    vec2d posA;
    vec2d posB;


    std::ofstream outputfile;
    outputfile.open("test_07.csv");

    Universe* uni = &universe;
    do{

        for(int ii = 0; ii < universe.objects.size(); ii++){
            double vel2 = len_squared(universe.objects[ii]->get_velocity());
            outputfile << 0.5*universe.objects[ii]->get_mass()*vel2 << ",";

            double ener = 0;
            for (int qq = 0; qq < universe.objects.size(); ++qq ) {
                // Make sure you are not calculating yourself
                if (qq == ii) {
                    // This is myself, skip this loop iteration
                    continue;
                }
                // Here add up the contribution to the acceleration
                ener += potentialEnergy(universe.objects[ii], universe.objects[qq],uni);
            }
            //// Made the potential energy per object only store half, as for each potential between 2 objects 2 objects store it but there is only one potential.
            if(ii == universe.objects.size()-1){
                outputfile << ener/2 << ";" << std::endl;
            }else{
                outputfile << ener/2 << ",";

            }
        }
        // Clear the buffers to set values (in our case only colour buffer needs to be cleared)
        glClear(GL_COLOR_BUFFER_BIT);

        // Draw the universe's objects on top of that
        window.drawObjectList(universe.objects);

        // Do a physics iteration
        universe.simulate_one_time_unit(window.fps);

        // Swap buffers
        glfwSwapBuffers(window.GLFWpointer);
        glfwPollEvents();

        window.pace_frame();

    } // Check if the ESC key was pressed or the window was closed
    while( glfwGetKey(window.GLFWpointer, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
           glfwWindowShouldClose(window.GLFWpointer) == 0 );

    outputfile.close();
    // Close OpenGL window and terminate GLFW
    glfwTerminate();

}