Author Topic: Pass a list of strings in python to a C++ class  (Read 3236 times)

Ignasi de Pouplana

  • Newbie
  • *
  • Posts: 21
Pass a list of strings in python to a C++ class
« on: June 21, 2016, 08:35:34 AM »
Hello, I am trying to improve my arc-length strategy by taking advantage of the new SubModelParts. The strategy should be able to access the nodes of a certain group of SubModelParts (the ones corresponding to the loads) at each iteration of the solution step.

I thought about defining a member variable, e.g. std::vector<std::string> mSubModelPartNames, with the name of those SubModelParts and then call them by means of ModelPart& GetSubModelPart(std::string const& SubModelPartName).

The thing is, since I already have a python list with the name of those SubModelParts in the "solver_settings" of the ProjectParameters.json, I wanted to pass that list to the constructor of the arc-length strategy in C++...

Is that possible? And if so, which is the best way to do it?

Thanks in advance,

Ignasi

Miguel Angel Celigueta

  • Administrator
  • Newbie
  • *****
  • Posts: 38
Re: Pass a list of strings in python to a C++ class
« Reply #1 on: June 21, 2016, 09:50:52 AM »
Hi Ignasi,
We are passing python lists to C++ in the DEM Application, and we are doing it like this:

We send the argument as a Python standard list, then we receive it as an argument of type 'boost::python::list& arg1'.
You can loop through the list using a standard for:
Code: [Select]
for(int i=0; i<boost::python::len(arg1); i++) { }And for every component, you can extract the value by doing:
Code: [Select]
boost::python::extract<double>(arg1[i])If your list contains strings, probably you should do
Code: [Select]
boost::python::extract<std::string>(arg1[i]) or
Code: [Select]
boost::python::extract<const std::string>(arg1[i]), but I did not test this.

The bad thing of this approach is that you must know the type of each component of the list. So it is not as robust as I woul like, but it is working for us.

Best regards,
Miguel Angel

Ignasi de Pouplana

  • Newbie
  • *
  • Posts: 21
Re: Pass a list of strings in python to a C++ class
« Reply #2 on: June 21, 2016, 11:08:28 AM »
Thanks for answering so fast.

I have used boost::python::list& and boost::python::extract<std::string> and it has compiled :)

pooyan

  • Global Moderator
  • Newbie
  • *****
  • Posts: 33
Re: Pass a list of strings in python to a C++ class
« Reply #3 on: June 21, 2016, 11:28:15 PM »
Hi Ignasi,

I suggest you to use the Parameter class which is done exactly to pass the jason parameters. your class should accept a Parameter which points to the sup part of the json where contains the information about your arc lenght.

Moreover, I didn't understood why you want to store the names? you may already save the model part pointers. Isn't it?

Ignasi de Pouplana

  • Newbie
  • *
  • Posts: 21
Re: Pass a list of strings in python to a C++ class
« Reply #4 on: June 22, 2016, 03:13:58 AM »
Ok, I will try passing a Parametrrs object.

And you are right, it's better to store a list with the SubModelPars instead of the names.

Thanks for your advice.