Ejemplo n.º 1
0
void ChPythonEngine::ImportSolidWorksSystem(const char* solidworks_py_file, ChSystem& msystem) throw(ChException)
{
	std::ostringstream sstream;

	//sstream << "from " << std::string(solidworks_py_file) << " import exported_items\n";

	sstream << "import builtins  \n";
	sstream << "import imp  \n";
	sstream << "import os  \n";
	sstream << "mdirname, mmodulename= os.path.split('" << std::string(solidworks_py_file) << "')  \n";
	sstream << "builtins.exported_system_relpath = mdirname + '/'  \n";
	sstream << "fp, pathname, description = imp.find_module(mmodulename,[builtins.exported_system_relpath])  \n";
	sstream << "try:  \n";
	sstream << "    imported_mod = imp.load_module('imported_mod', fp, pathname, description)  \n";
	sstream << "finally:  \n";
	sstream << "    if fp:  \n";
	sstream << "        fp.close()  \n";
	sstream << "exported_items = imported_mod.exported_items  \n";

	this->Run(sstream.str().c_str());
	
	PyObject * module = PyImport_AddModule("__main__"); // borrowed reference
	if (!module)
		throw ChException("ERROR. No Python __main__ module?"); 

	PyObject * dictionary = PyModule_GetDict(module);   // borrowed reference
	if (!dictionary) 
		throw ChException("ERROR. No Python dictionary?");                                 

	PyObject * result = PyDict_GetItemString(dictionary, "exported_items");   // borrowed reference
	if (!result) 
		throw ChException("ERROR. Missing Python object 'exported_items' in SolidWorks file");

	if (PyList_Check(result))
	{
		int nitems = PyList_Size(result);
		//GetLog() << "N.of list items: " << nitems << "\n";
		for (int i = 0; i< nitems; i++)
		{
			PyObject* mobj = PyList_GetItem(result,i);
			if (mobj)
			{	
				// GetLog() << "   Python type: " << mobj->ob_type->tp_name << "\n";
				
				SwigPyObject * mswigobj  = SWIG_Python_GetSwigThis(mobj);
				if (mswigobj) 
				{
					void* objptr = mswigobj->ptr;
					ChSharedPtr<ChPhysicsItem>* pt_to_shp = (ChSharedPtr<ChPhysicsItem>*)objptr;	
				
						/// Add the ChPhysicsItem to the ChSystem
					msystem.Add( (*pt_to_shp) );
				} 
				else
				{
					throw ChException("ERROR. Only shared pointers to ChPhysicsItem subclasses can be inside exported_items.");
				}
			}
		}

		msystem.Setup();
		msystem.Update();

	}
	else
	{
		throw ChException("ERROR. exported_items python object is not a list.");
	}

	
}
Ejemplo n.º 2
0
void create_wall_bodies(ChSystem& mphysicalSystem)
{
	// Create a material that will be shared between bricks
	ChSharedPtr<ChMaterialSurface> mmaterial(new ChMaterialSurface);
	mmaterial->SetFriction(0.4f);
	mmaterial->SetCompliance (0.0000005f);
	mmaterial->SetComplianceT(0.0000005f);
	mmaterial->SetDampingF(0.2f);
		
	// Create bricks
	for (int ai = 0; ai < 1; ai++)  // N. of walls
	{ 
		for (int bi = 0; bi < 10; bi++)  // N. of vert. bricks
		{ 
			for (int ui = 0; ui < 15; ui++)  // N. of hor. bricks
			{ 
				ChSharedPtr<ChBodyEasyBox> mrigidBody(new ChBodyEasyBox(
											3.96, 2, 4, // x,y,z size
											 100,		// density
											true,		// collide enable?
											true));		// visualization?
				mrigidBody->SetPos(ChVector<>(-8+ui*4.0+2*(bi%2),  1.0+bi*2.0, ai*9));
				mrigidBody->SetMaterialSurface(mmaterial); // use shared surface properties
				
				mphysicalSystem.Add(mrigidBody);
				
				// optional, attach a texture for better visualization
				ChSharedPtr<ChTexture> mtexture(new ChTexture());
				mtexture->SetTextureFilename("../data/cubetexture_borders.png");
				mrigidBody->AddAsset(mtexture);
			}
		}
	}

	// Create the floor using
	// fixed rigid body of 'box' type:

	ChSharedPtr<ChBodyEasyBox> mrigidFloor(new ChBodyEasyBox(
											250, 4, 250, // x,y,z size
											1000,		// density
											true,		// collide enable?
											true));		// visualization?
	mrigidFloor->SetPos(ChVector<>(0,-2,0));
	mrigidFloor->SetMaterialSurface(mmaterial);
	mrigidFloor->SetBodyFixed(true);
	
	mphysicalSystem.Add(mrigidFloor);


	
	// Create a ball that will collide with wall
	ChSharedPtr<ChBodyEasySphere> mrigidBall(new ChBodyEasySphere(
											4,			// radius
											8000,		// density
											true,		// collide enable?
											true));		// visualization?
	mrigidBall->SetPos(ChVector<>(0,-2,0));
	mrigidBall->SetMaterialSurface(mmaterial);
	mrigidBall->SetPos(ChVector<>(0, 3, -8));
	mrigidBall->SetPos_dt(ChVector<>(0,0,16)); // set initial speed 
	mrigidBall->GetMaterialSurface()->SetFriction(0.4f); // use own (not shared) matrial properties
	mrigidBall->GetMaterialSurface()->SetCompliance(0.0);
	mrigidBall->GetMaterialSurface()->SetComplianceT(0.0);
	mrigidBall->GetMaterialSurface()->SetDampingF(0.2f);

	mphysicalSystem.Add(mrigidBall);

	// optional, attach a texture for better visualization
	ChSharedPtr<ChTexture> mtextureball(new ChTexture());
	mtextureball->SetTextureFilename("../data/bluwhite.png");
	mrigidBall->AddAsset(mtextureball);


}
Ejemplo n.º 3
0
int main(int argc, char* argv[]) {
    // Create a ChronoENGINE physical system
    ChSystem mphysicalSystem;

    // Create the Irrlicht visualization (open the Irrlicht device,
    // bind a simple user interface, etc. etc.)
    ChIrrApp application(&mphysicalSystem, L"Meshless deformable material", core::dimension2d<u32>(800, 600), false);

    // Easy shortcuts to add camera, lights, logo and sky in Irrlicht scene:
    ChIrrWizard::add_typical_Logo(application.GetDevice());
    ChIrrWizard::add_typical_Sky(application.GetDevice());
    ChIrrWizard::add_typical_Lights(application.GetDevice());
    ChIrrWizard::add_typical_Camera(application.GetDevice(), core::vector3df(0, 3, -3));

    // CREATE A FLOOR

    ChSharedPtr<ChBodyEasyBox> mfloorBody( new ChBodyEasyBox(20,1,20,1000,true,true));
    my_system.Add(mfloorBody);
    mfloorBody->SetBodyFixed(true);
    mfloorBody->SetPos(ChVector<>(0, -5, 0));

    ChSharedPtr<ChTexture> mtexture( new ChTexture(GetChronoDataFile("concrete.jpg").c_str()));
    mfloorBody->AddAsset(mtexture);

   
    // CREATE THE ELASTOPLASTIC MESHLESS CONTINUUM

    // Create elastoplastic matter
    ChSharedPtr<ChMatterMeshless> mymatter(new ChMatterMeshless);

    // Use the FillBox easy way to create the set of nodes in the meshless matter
    mymatter->FillBox(ChVector<>(4, 2, 4),                          // size of box
                      4.0 / 10.0,                                   // resolution step
                      1000,                                         // initial density
                      ChCoordsys<>(ChVector<>(0, -3.9, 0), QUNIT),  // position & rotation of box
                      true,                                         // do a centered cubic lattice initial arrangement
                      2.1);                                         // set the kernel radius (as multiples of step)

    GetLog() << "Added " << mymatter->GetNnodes() << " nodes \n";

    // Set some material properties of the meshless matter

    ChSharedPtr<ChContinuumDruckerPrager> mmaterial(new ChContinuumDruckerPrager);
    mymatter->ReplaceMaterial(mmaterial);
    mmaterial->Set_v(0.35);
    mmaterial->Set_E(30000.0);
    mmaterial->Set_elastic_yeld(0);
    mmaterial->Set_alpha(30 * CH_C_DEG_TO_RAD);
    // mmaterial->Set_from_MohrCoulomb(30 * CH_C_DEG_TO_RAD, 1000);
    mmaterial->Set_dilatancy(30 * CH_C_DEG_TO_RAD);
    mmaterial->Set_flow_rate(50000000.0);
    mmaterial->Set_hardening_limit(mmaterial->Get_elastic_yeld());
    mmaterial->Set_hardening_speed(100000000);

    /*
        ChSharedPtr<ChContinuumPlasticVonMises> mmaterial(new ChContinuumPlasticVonMises);
        mymatter->ReplaceMaterial(mmaterial);
        mmaterial->Set_v(0.38);
        mmaterial->Set_E(60000.0);
        mmaterial->Set_elastic_yeld(0.06);
        mmaterial->Set_flow_rate(300);
    */

    mymatter->SetViscosity(5000);

    // Add the matter to the physical system
    mymatter->SetCollide(true);
    mphysicalSystem.Add(mymatter);

    // Join some nodes of meshless matter to a ChBody
    /*
    ChSharedPtr<ChIndexedNodes> mnodes = mymatter;
    for (int ij = 0; ij < 120; ij++)
    {
        ChSharedPtr<ChLinkPointFrame> myjointnodebody(new ChLinkPointFrame);
        myjointnodebody->Initialize(mnodes,
                                    ij,
                                    mfloorBody->GetBody());
        mphysicalSystem.Add(myjointnodebody);
    }
    */

    // IMPORTANT!
    // This takes care of the interaction between the particles of the meshless material
    ChSharedPtr<ChProximityContainerMeshless> my_sph_proximity(new ChProximityContainerMeshless);
    mphysicalSystem.Add(my_sph_proximity);


    // CREATE A SPHERE PRESSING THE MATERIAL:

    ChSharedPtr<ChBodyEasySphere> msphere( new ChBodyEasySphere(2, 7000, true,true));
    my_system.Add(msphere);
    msphere->SetPos(ChVector<>(0, -0.5, 0));

    

    // Use this function for adding a ChIrrNodeAsset to all items
    // Otherwise use application.AssetBind(myitem); on a per-item basis.
    application.AssetBindAll();

    // Use this function for 'converting' assets into Irrlicht meshes
    application.AssetUpdateAll();


    // Modify some setting of the physical system for the simulation, if you want

    mphysicalSystem.SetMaxItersSolverSpeed(25);  // lower the LCP iters, no needed here

    //
    // THE SOFT-REAL-TIME CYCLE
    //

    static int printed_prox = 0;
    application.SetStepManage(true);
    application.SetTimestep(0.002);

    while (application.GetDevice()->run()) {
        application.BeginScene(true, true, SColor(255, 140, 161, 192));

        application.DrawAll();

        ChSystem::IteratorOtherPhysicsItems myiter = mphysicalSystem.IterBeginOtherPhysicsItems();

        while (myiter != mphysicalSystem.IterEndOtherPhysicsItems()) {
            if ((*myiter).IsType<ChMatterMeshless>()) {
                ChSharedPtr<ChMatterMeshless> mymatter((*myiter).DynamicCastTo<ChMatterMeshless>());

                for (unsigned int ip = 0; ip < mymatter->GetNnodes(); ip++) {
                    ChSharedPtr<ChNodeMeshless> mnode(mymatter->GetNode(ip).DynamicCastTo<ChNodeMeshless>());
                    ChVector<> mv = mnode->GetPos();
                    float rad = (float)mnode->GetKernelRadius();
                    core::vector3df mpos((irr::f32)mv.x, (irr::f32)mv.y, (irr::f32)mv.z);
                    core::position2d<s32> spos =
                        application.GetSceneManager()->getSceneCollisionManager()->getScreenCoordinatesFrom3DPosition(
                            mpos);
                    application.GetVideoDriver()->draw2DRectangle(
                        video::SColor(100, 200, 200, 230),
                        core::rect<s32>(spos.X - 2, spos.Y - 2, spos.X + 2, spos.Y + 2));
                    /*
                    application.GetVideoDriver()->setTransform(video::ETS_WORLD, core::matrix4());
                    application.GetVideoDriver()->draw3DBox( core::aabbox3d<f32>(
                                    (irr::f32)mv.x-rad ,(irr::f32)mv.y-rad , (irr::f32)mv.z-rad    ,
                                    (irr::f32)mv.x+rad ,(irr::f32)mv.y+rad , (irr::f32)mv.z+rad )   ,
                                    video::SColor(300,200,200,230) );
                    */

                    /*
                    double strain_scale =10;
                    ChIrrTools::drawSegment(application.GetVideoDriver(), mnode->GetPos(),
                    mnode->GetPos()+(VECT_X*mnode->p_strain.XX()* strain_scale), video::SColor(255,255,0,0),false);
                    ChIrrTools::drawSegment(application.GetVideoDriver(), mnode->GetPos(),
                    mnode->GetPos()+(VECT_Y*mnode->p_strain.YY()* strain_scale), video::SColor(255,0,255,0),false);
                    ChIrrTools::drawSegment(application.GetVideoDriver(), mnode->GetPos(),
                    mnode->GetPos()+(VECT_Z*mnode->p_strain.ZZ()* strain_scale), video::SColor(255,0,0,255),false);
                    */

                    /*
                    double stress_scale =0.008;
                    ChIrrTools::drawSegment(application.GetVideoDriver(), mnode->GetPos(),
                    mnode->GetPos()+(VECT_X*mnode->e_stress.XX()* stress_scale), video::SColor(100,255,0,0),false);
                    ChIrrTools::drawSegment(application.GetVideoDriver(), mnode->GetPos(),
                    mnode->GetPos()+(VECT_Y*mnode->e_stress.YY()* stress_scale), video::SColor(100,0,255,0),false);
                    ChIrrTools::drawSegment(application.GetVideoDriver(), mnode->GetPos(),
                    mnode->GetPos()+(VECT_Z*mnode->e_stress.ZZ()* stress_scale), video::SColor(100,0,0,255),false);
                    */

                    double stress_scale = 0.00001;
                    ChIrrTools::drawSegment(
                        application.GetVideoDriver(), mnode->GetPos(),
                        mnode->GetPos() + (VECT_X * mnode->e_stress.GetInvariant_I1() * stress_scale),
                        video::SColor(100, 255, 0, 0), false);

                    // GetLog() << "Mass i="<< ip << "   m=" << mnode->GetMass() << "\n";
                    // ChIrrTools::drawSegment(application.GetVideoDriver(), mnode->GetPos(),
                    // mnode->GetPos()+(mnode->UserForce * 0.1), video::SColor(100,0,0,0),false);
                }
            }
            ++myiter;
        }

        application.DoStep();

        application.EndScene();
    }

    return 0;
}
Ejemplo n.º 4
0
void create_jengatower_bodies(ChSystem& mphysicalSystem)
{
	// Create a material that will be shared between bricks
	ChSharedPtr<ChMaterialSurface> mmaterial(new ChMaterialSurface);
	mmaterial->SetFriction(0.4f);
	mmaterial->SetCompliance (0.0000005f);
	mmaterial->SetComplianceT(0.0000005f);
	mmaterial->SetDampingF(0.2f);

	// Create bricks
	for (int bi = 0; bi < 12; bi+=2) 
	{ 
		ChSharedPtr<ChBodyEasyBox> mrigidBody1(new ChBodyEasyBox(
											2, 2, 14,   // x,y,z size
											 100,		// density
											true,		// collide enable?
											true));		// visualization?
		mrigidBody1->SetPos( ChVector<>(-5, 1.0+bi*2.0,  0) );
		mrigidBody1->SetMaterialSurface(mmaterial); // use shared surface properties	
		mphysicalSystem.Add(mrigidBody1);


		ChSharedPtr<ChBodyEasyBox> mrigidBody2(new ChBodyEasyBox(
											2, 2, 14,   // x,y,z size
											 100,		// density
											true,		// collide enable?
											true));		// visualization?
		mrigidBody2->SetPos( ChVector<>( 5, 1.0+bi*2.0,  0) );
		mrigidBody2->SetMaterialSurface(mmaterial); // use shared surface properties	
		mphysicalSystem.Add(mrigidBody2);


		ChSharedPtr<ChBodyEasyBox> mrigidBody3(new ChBodyEasyBox(
											14, 2, 2,   // x,y,z size
											 100,		// density
											true,		// collide enable?
											true));		// visualization?
		mrigidBody3->SetPos( ChVector<>(0, 3.0+bi*2.0,  5) );
		mrigidBody3->SetMaterialSurface(mmaterial); // use shared surface properties	
		mphysicalSystem.Add(mrigidBody3);


		ChSharedPtr<ChBodyEasyBox> mrigidBody4(new ChBodyEasyBox(
											14, 2, 2,   // x,y,z size
											 100,		// density
											true,		// collide enable?
											true));		// visualization?
		mrigidBody4->SetPos( ChVector<>(0, 3.0+bi*2.0,  -5) );
		mrigidBody4->SetMaterialSurface(mmaterial); // use shared surface properties	
		mphysicalSystem.Add(mrigidBody4);
	}

	// Create the floor using
	// fixed rigid body of 'box' type:

	ChSharedPtr<ChBodyEasyBox> mrigidFloor(new ChBodyEasyBox(
											250, 4, 250, // x,y,z size
											1000,		// density
											true,		// collide enable?
											true));		// visualization?
	mrigidFloor->SetPos(ChVector<>(0,-2,0));
	mrigidFloor->SetMaterialSurface(mmaterial);
	mrigidFloor->SetBodyFixed(true);
	
	mphysicalSystem.Add(mrigidFloor);


	
	// Create a ball that will collide with tower
	ChSharedPtr<ChBodyEasySphere> mrigidBall(new ChBodyEasySphere(
											4,			// radius
											1000,		// density
											true,		// collide enable?
											true));		// visualization?
	mrigidBall->SetMaterialSurface(mmaterial);
	mrigidBall->SetPos(ChVector<>(0, 3, -8));
	mrigidBall->SetPos_dt(ChVector<>(0,0,2)); // set initial speed 
	mrigidBall->GetMaterialSurface()->SetFriction(0.4f); // use own (not shared) matrial properties
	mrigidBall->GetMaterialSurface()->SetCompliance(0.0);
	mrigidBall->GetMaterialSurface()->SetComplianceT(0.0);
	mrigidBall->GetMaterialSurface()->SetDampingF(0.2f);

	mphysicalSystem.Add(mrigidBall);

	// optional, attach a texture for better visualization
	ChSharedPtr<ChTexture> mtextureball(new ChTexture());
	mtextureball->SetTextureFilename("../data/bluwhite.png");
	mrigidBall->AddAsset(mtextureball);


}