Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with sm.pi #499

Open
Peter230655 opened this issue May 13, 2024 · 5 comments
Open

Problem with sm.pi #499

Peter230655 opened this issue May 13, 2024 · 5 comments

Comments

@Peter230655
Copy link
Contributor

Peter230655 commented May 13, 2024

I made some simulation, where I used sm.pi to form the EOMs using Kane's method as usual.
I then tried to integrate it, using pydy.System
When I added this line:

sys.generate_ode_function(generator='cython', verbose=False)

I got an error, without this line, it worked fine.
24-0ß5-13 error with sm_pi.txt

Timo then suggested I add these lines:

old_c_source_template = CMatrixGenerator._c_source_template
CMatrixGenerator._c_source_template = "#define M_PI 3.14159265358979323846\n" + old_c_source_template

This solved the problem for me. (alternatively, when I replaced sm.pi by np.pi it also worked)
So, while my problem was solved, maybe it could be corrected permanently.
Thanks for looking into it!

@moorepants
Copy link
Member

This also seems to be present on Windows, not Linux.

@Peter230655
Copy link
Contributor Author

This also seems to be present on Windows, not Linux.

I do use Windows.

@moorepants
Copy link
Member

Error from the file:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\codegen\ode_function_generators.py:907, in generate_ode_function(*args, **kwargs)
    905 try:
    906     # See if user passed in a custom class.
--> 907     g = generator(*args, **kwargs)
    908 except TypeError:
    909     # See if user passed in a string.

TypeError: 'str' object is not callable

During handling of the above exception, another exception occurred:

CalledProcessError                        Traceback (most recent call last)
File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\codegen\cython_code.py:222, in CythonMatrixGenerator.compile(self, tmp_dir, verbose)
    220 cmd = [sys.executable, self.prefix + '_setup.py', 'build_ext',
    221        '--inplace']
--> 222 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
    223 if verbose:

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\subprocess.py:465, in check_output(timeout, *popenargs, **kwargs)
    463     kwargs['input'] = empty
--> 465 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
    466            **kwargs).stdout

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\subprocess.py:569, in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    568     if check and retcode:
--> 569         raise CalledProcessError(retcode, process.args,
    570                                  output=stdout, stderr=stderr)
    571 return CompletedProcess(process.args, retcode, stdout, stderr)

CalledProcessError: Command '['c:\\Users\\Peter\\anaconda3\\envs\\sympy-dev\\python.exe', 'pydy_codegen_0_setup.py', 'build_ext', '--inplace']' returned non-zero exit status 1.

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
Cell In[5], line 47
     43 sys.times = np.linspace(0., intervall, int(schritte*intervall))
     45 #=================================================================================================
     46 #=================================================================================================
---> 47 sys.generate_ode_function(generator='cython', verbose=False) 
     48 #=================================================================================================
     49 #=================================================================================================
     51 resultat = sys.integrate()

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\system.py:481, in System.generate_ode_function(self, **kwargs)
    477     print("User supplied 'specifieds' kwarg was disregarded.")
    479 kwargs.update(self._kwargs_for_gen_ode_func())
--> 481 self._evaluate_ode_function = generate_ode_function(
    482     *self._args_for_gen_ode_func(),
    483     **kwargs)
    485 return self.evaluate_ode_function

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\codegen\ode_function_generators.py:917, in generate_ode_function(*args, **kwargs)
    915         raise NotImplementedError(msg)
    916     else:
--> 917         return g.generate()
    918 else:
    919     return g.generate()

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\codegen\ode_function_generators.py:622, in ODEFunctionGenerator.generate(self)
    620     self.generate_full_rhs_function()
    621 elif self.system_type == 'full mass matrix':
--> 622     self.generate_full_mass_matrix_function()
    623 elif self.system_type == 'min mass matrix':
    624     self.generate_min_mass_matrix_function()

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\codegen\ode_function_generators.py:700, in CythonODEFunctionGenerator.generate_full_mass_matrix_function(self)
    697     self._set_eval_array(self._cythonize_symbolic_lusolve(outputs,
    698                                                           self.inputs))
    699 else:
--> 700     self._set_eval_array(self._cythonize(outputs, self.inputs))

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\codegen\ode_function_generators.py:654, in CythonODEFunctionGenerator._cythonize(self, outputs, inputs)
    650 def _cythonize(self, outputs, inputs):
    651     g = CythonMatrixGenerator(inputs, outputs,
    652                               prefix=self._options['prefix'],
    653                               cse=self._options['cse'])
--> 654     return g.compile(tmp_dir=self._options['tmp_dir'],
    655                      verbose=self._options['verbose'])

File c:\Users\Peter\anaconda3\envs\sympy-dev\Lib\site-packages\pydy\codegen\cython_code.py:227, in CythonMatrixGenerator.compile(self, tmp_dir, verbose)
    225     cython_module = importlib.import_module(self.prefix)
    226 except:
--> 227     raise Exception('Failed to compile and import Cython module.')
    228 finally:
    229     sys.path.remove(codedir)

Exception: Failed to compile and import Cython module.

@moorepants
Copy link
Member

From https://stackoverflow.com/a/26065595:

Math Constants are not defined in Standard C/C++. To use them, you must first define _USE_MATH_DEFINES and then include cmath or math.h.

@oliverlee
Copy link
Contributor

C++20 has some math constants:
https://en.cppreference.com/w/cpp/numeric/constants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants