Giter Site home page Giter Site logo

imsim's Introduction

IMSim

Simple, older and non-complete C++ simulation library as a university project for FIT VUT Brno.

Example

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "IMSim.h"

using namespace std;
using namespace IMSim;

const double g = -9.81;
const double dimnish = -0.8;

class Ball: public Process {
	public:
		Ball(Simulation *simulation, BLOCK_VALUE initialPosition): Process(simulation) {
			this->force = B_INTEG_IN( NUM(g), 0, 0.0 );
			this->movement = B_INTEG_IN(this->force, initialPosition, 0.0 );
			
			this->cond = new Condition(this->movement, 0.0, CALLBACK(Ball::jump));
			ConditionDescending* condFix = new ConditionDescending(this->movement, -0.00001, CALLBACK(Ball::fix));
			
			this->getBlockContainer()->addBlock(this->movement);
			this->getConditionsContainer()->addCondition(this->cond);
			this->getConditionsContainer()->addCondition(condFix);
		}
		
	private:
		void action() {}
		
		void jump() {
			std::cout << "# jumping" << std::endl;
			
			this->movement->getIntegrator()->setCurrent(0, this->now());
			
			BLOCK_VALUE up = this->force->value(this->now()) * dimnish;
			this->force->getIntegrator()->setCurrent(up, this->now());
		}
		
		void fix() {
			std::cout << "# fixing" << std::endl;
			
			this->getSimulation()->abort();
			
			this->cond->disable();
			
			delete this->force;
			delete this->movement;
			this->movement = NUM(0);
		}
		
		Condition* cond;
		Block* movement;
		Block* force;
	
};

int main(int argc, char** argv) {
	
	// init
	Simulator *sim = new Simulator();
	Simulation *mySim = sim->createSimulation("Ball example");
	mySim->setStep(1e-8, 1e-3);
	
	// setup
	mySim->setOutput(&std::cout, Simulation::OUTPUT_VERBOSE | Simulation::OUTPUT_DEBUG);		
	(new Ball(mySim, 1.0))->schedule();
	
	// run
	try{
		mySim->run(0.0, 4);
	} catch( std::runtime_error e ) {
		(*mySim->getOutput()) << std::endl << "ERROR: " << e.what() << std::endl << std::endl;
	}
			
	// cleanup
	delete mySim;
	delete sim;
	return EXIT_SUCCESS;
}

imsim's People

Contributors

noxart avatar

Stargazers

M Haidar Hanif avatar

Watchers

 avatar James Cloos avatar Rogue Cipher avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.