void ChIntegrableIIorder::StateIncrement(ChState& y_new, // resulting y_new = y + Dy const ChState& y, // initial state y const ChStateDelta& Dy // state increment Dy ) { if (y.GetRows() == this->GetNcoords_x()) { // Incrementing the x part only, user provided only x in y={x, dx/dt} StateIncrementX(y_new, y, Dy); return; } if (y.GetRows() == this->GetNcoords_y()) { // Incrementing y in y={x, dx/dt}. // PERFORMANCE WARNING! temporary vectors allocated on heap. This is only to support // compatibility with 1st order integrators. ChState mx(this->GetNcoords_x(), y.GetIntegrable()); ChStateDelta mv(this->GetNcoords_v(), y.GetIntegrable()); mx.PasteClippedMatrix(y, 0, 0, this->GetNcoords_x(), 1, 0, 0); mv.PasteClippedMatrix(y, this->GetNcoords_x(), 0, this->GetNcoords_v(), 1, 0, 0); ChStateDelta mDx(this->GetNcoords_v(), y.GetIntegrable()); ChStateDelta mDv(this->GetNcoords_a(), y.GetIntegrable()); mDx.PasteClippedMatrix(Dy, 0, 0, this->GetNcoords_v(), 1, 0, 0); mDv.PasteClippedMatrix(Dy, this->GetNcoords_v(), 0, this->GetNcoords_a(), 1, 0, 0); ChState mx_new(this->GetNcoords_x(), y.GetIntegrable()); ChStateDelta mv_new(this->GetNcoords_v(), y.GetIntegrable()); StateIncrementX(mx_new, mx, mDx); // increment positions mv_new = mv + mDv; // increment speeds y_new.PasteMatrix(mx_new, 0, 0); y_new.PasteMatrix(mv_new, this->GetNcoords_x(), 0); return; } throw ChException("StateIncrement() called with a wrong number of elements"); }
void ChPythonEngine::Run(const char* program) throw(ChException) { /* // Redirect stdout to one std::string buffer. std::streambuf *msgOutBuffer, *msgOutBufferBackup; std::stringstream smessageOutput; msgOutBufferBackup = std::cout.rdbuf(); msgOutBuffer = smessageOutput.rdbuf(); std::cout.rdbuf(msgOutBuffer); */ // Redirect stderr to one std::string buffer. std::streambuf *msgErrBuffer, *msgErrBufferBackup; std::stringstream smessageError; msgErrBufferBackup = std::cerr.rdbuf(); msgErrBuffer = smessageError.rdbuf(); std::cerr.rdbuf(msgErrBuffer); if (PyRun_SimpleString(program) != 0) { PyErr_Print(); throw ChException(smessageError.str()); } // Set to previous channels! //std::cout.rdbuf(msgOutBufferBackup); std::cerr.rdbuf(msgErrBufferBackup); }
bool ChCollisionModel::AddConvexHullsFromFile(ChStreamInAscii& mstream, const ChVector<>& pos, const ChMatrix33<>& rot) { std::vector<ChVector<double> > ptlist; char bufdata[200]; int linechar = 0; while (!mstream.End_of_stream()) { // read one line linechar = 0; while (!mstream.End_of_stream()) { try { mstream >> bufdata[linechar]; } catch (ChException mex) { }; if ((bufdata[linechar] == (char)13) || (bufdata[linechar] == (char)10)) { bufdata[linechar] = 0; break; } linechar++; if (linechar >= 200) throw(ChException("Too long line in parsing")); } bufdata[linechar + 1] = 0; bool parsedline = false; if (bufdata[0] != *"#") { parsedline = true; } if (strcmp(bufdata, "hull") == 0) { if (ptlist.size()) this->AddConvexHull(ptlist, pos, rot); ptlist.clear(); parsedline = true; } float vx, vy, vz; if (sscanf(bufdata, "%g %g %g", &vx, &vy, &vz) == 3) { ptlist.push_back(ChVector<>(vx, vy, vz)); parsedline = true; } } if (ptlist.size()) this->AddConvexHull(ptlist, pos, rot); ptlist.clear(); return true; }
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."); } }