Skip to content

Commit

Permalink
added verbose option to AcadosOcpSolver build (#928)
Browse files Browse the repository at this point in the history
* added verbose option to AcadosOcpSolver build

* minor formatting

* test non verbose

---------

Co-authored-by: Jonathan Frey <jonathanpaulfrey@gmail.com>
  • Loading branch information
tudoroancea and FreyJo committed May 19, 2023
1 parent e601ace commit 09ffa61
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 20 deletions.
Expand Up @@ -144,7 +144,7 @@ def main(discretization='shooting_nodes'):
json_path = os.path.join(acados_path, 'interfaces/acados_template/acados_template')
with open(json_path + '/simulink_default_opts.json', 'r') as f:
simulink_opts = json.load(f)
ocp_solver = AcadosOcpSolver(ocp, json_file = 'acados_ocp.json', simulink_opts = simulink_opts)
ocp_solver = AcadosOcpSolver(ocp, json_file = 'acados_ocp.json', simulink_opts = simulink_opts, verbose=False)

# ocp_solver = AcadosOcpSolver(ocp, json_file = 'acados_ocp.json')

Expand Down
33 changes: 26 additions & 7 deletions interfaces/acados_template/acados_template/acados_ocp_solver.py
Expand Up @@ -40,6 +40,8 @@
import importlib
import shutil

from subprocess import DEVNULL, call, STDOUT

from ctypes import POINTER, cast, CDLL, c_void_p, c_char_p, c_double, c_int, c_int64, byref

from copy import deepcopy
Expand Down Expand Up @@ -863,27 +865,44 @@ def generate(cls, acados_ocp: AcadosOcp, json_file='acados_ocp_nlp.json', simuli


@classmethod
def build(cls, code_export_dir, with_cython=False, cmake_builder: CMakeBuilder = None):
def build(cls, code_export_dir, with_cython=False, cmake_builder: CMakeBuilder = None, verbose: bool = True):
"""
Builds the code for an acados OCP solver, that has been generated in code_export_dir
:param code_export_dir: directory in which acados OCP solver has been generated, see generate()
:param with_cython: option indicating if the cython interface is build, default: False.
:param cmake_builder: type :py:class:`~acados_template.builders.CMakeBuilder` generate a `CMakeLists.txt` and use
the `CMake` pipeline instead of a `Makefile` (`CMake` seems to be the better option in conjunction with
`MS Visual Studio`); default: `None`
:param verbose: indicating if build command is printed
"""
code_export_dir = os.path.abspath(code_export_dir)
cwd = os.getcwd()
os.chdir(code_export_dir)
if with_cython:
os.system('make clean_ocp_cython')
os.system('make ocp_cython')
call(
['make', 'clean_all'],
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
call(
['make', 'ocp_cython'],
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
else:
if cmake_builder is not None:
cmake_builder.exec(code_export_dir)
else:
os.system('make clean_ocp_shared_lib')
os.system('make ocp_shared_lib')
call(
['make', 'clean_ocp_shared_lib'],
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
call(
['make', 'ocp_shared_lib'],
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
os.chdir(cwd)


Expand Down Expand Up @@ -911,7 +930,7 @@ def create_cython_solver(cls, json_file):
acados_ocp_json['dims']['N'])


def __init__(self, acados_ocp: AcadosOcp, json_file='acados_ocp_nlp.json', simulink_opts=None, build=True, generate=True, cmake_builder: CMakeBuilder = None):
def __init__(self, acados_ocp: AcadosOcp, json_file='acados_ocp_nlp.json', simulink_opts=None, build=True, generate=True, cmake_builder: CMakeBuilder = None, verbose=True):

self.solver_created = False
if generate:
Expand All @@ -928,7 +947,7 @@ def __init__(self, acados_ocp: AcadosOcp, json_file='acados_ocp_nlp.json', simul
code_export_directory = acados_ocp_json['code_export_directory']

if build:
self.build(code_export_directory, with_cython=False, cmake_builder=cmake_builder)
self.build(code_export_directory, with_cython=False, cmake_builder=cmake_builder, verbose=verbose)

# prepare library loading
lib_prefix = 'lib'
Expand Down
28 changes: 21 additions & 7 deletions interfaces/acados_template/acados_template/acados_sim_solver.py
Expand Up @@ -39,6 +39,8 @@

import numpy as np

from subprocess import DEVNULL, call, STDOUT

from ctypes import POINTER, cast, CDLL, c_void_p, c_char_p, c_double, c_int, c_bool, byref
from copy import deepcopy

Expand Down Expand Up @@ -241,18 +243,30 @@ def generate(cls, acados_sim: AcadosSim, json_file='acados_sim.json', cmake_buil


@classmethod
def build(cls, code_export_dir, with_cython=False, cmake_builder: CMakeBuilder = None):
def build(cls, code_export_dir, with_cython=False, cmake_builder: CMakeBuilder = None, verbose: bool = True):
# Compile solver
cwd = os.getcwd()
os.chdir(code_export_dir)
if with_cython:
os.system('make clean_sim_cython')
os.system('make sim_cython')
call(
['make', 'clean_sim_cython'],
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
call(
['make', 'sim_cython'],
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
else:
if cmake_builder is not None:
cmake_builder.exec(code_export_dir)
cmake_builder.exec(code_export_dir, verbose=verbose)
else:
os.system('make sim_shared_lib')
call(
['make', 'sim_shared_lib'],
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
os.chdir(cwd)


Expand All @@ -271,7 +285,7 @@ def create_cython_solver(cls, json_file):
AcadosSimSolverCython = getattr(acados_sim_solver_pyx, 'AcadosSimSolverCython')
return AcadosSimSolverCython(acados_sim_json['model']['name'])

def __init__(self, acados_sim, json_file='acados_sim.json', generate=True, build=True, cmake_builder: CMakeBuilder = None):
def __init__(self, acados_sim, json_file='acados_sim.json', generate=True, build=True, cmake_builder: CMakeBuilder = None, verbose: bool = True):

self.solver_created = False
self.acados_sim = acados_sim
Expand All @@ -285,7 +299,7 @@ def __init__(self, acados_sim, json_file='acados_sim.json', generate=True, build
self.generate(acados_sim, json_file=json_file, cmake_builder=cmake_builder)

if build:
self.build(code_export_dir, cmake_builder=cmake_builder)
self.build(code_export_dir, cmake_builder=cmake_builder, verbose=True)

# prepare library loading
lib_prefix = 'lib'
Expand Down
25 changes: 20 additions & 5 deletions interfaces/acados_template/acados_template/builders.py
Expand Up @@ -34,7 +34,7 @@

import os
import sys
from subprocess import call
from subprocess import DEVNULL, call, STDOUT


class CMakeBuilder:
Expand Down Expand Up @@ -78,7 +78,7 @@ def get_cmd2_build(self):
def get_cmd3_install(self):
return f'cmake --install "{self._build_dir}"'

def exec(self, code_export_directory):
def exec(self, code_export_directory, verbose=True):
"""
Execute the compilation using `CMake` with the given settings.
:param code_export_directory: must be the absolute path to the directory where the code was exported to
Expand All @@ -96,17 +96,32 @@ def exec(self, code_export_directory):
os.chdir(self._build_dir)
cmd_str = self.get_cmd1_cmake()
print(f'call("{cmd_str})"')
retcode = call(cmd_str, shell=True)
retcode = call(
cmd_str,
shell=True,
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
if retcode != 0:
raise RuntimeError(f'CMake command "{cmd_str}" was terminated by signal {retcode}')
cmd_str = self.get_cmd2_build()
print(f'call("{cmd_str}")')
retcode = call(cmd_str, shell=True)
retcode = call(
cmd_str,
shell=True,
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
if retcode != 0:
raise RuntimeError(f'Build command "{cmd_str}" was terminated by signal {retcode}')
cmd_str = self.get_cmd3_install()
print(f'call("{cmd_str}")')
retcode = call(cmd_str, shell=True)
retcode = call(
cmd_str,
shell=True,
stdout=None if verbose else DEVNULL,
stderr=None if verbose else STDOUT
)
if retcode != 0:
raise RuntimeError(f'Install command "{cmd_str}" was terminated by signal {retcode}')
except OSError as e:
Expand Down

0 comments on commit 09ffa61

Please sign in to comment.