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

To sort in an lexicographic order the objects in dict() #78

Closed
rzyu45 opened this issue Jun 1, 2024 · 2 comments · Fixed by #87
Closed

To sort in an lexicographic order the objects in dict() #78

rzyu45 opened this issue Jun 1, 2024 · 2 comments · Fixed by #87
Assignees

Comments

@rzyu45
Copy link
Member

rzyu45 commented Jun 1, 2024

For example, I first run

>>> from Solverz import iVar, Para, Ode
>>> x = iVar('x')
>>> y = iVar('y')
>>> lam = Para('lam')
>>> Ode('b', x + y * lam, diff_var=x).SYMBOLS
{'lam': lam, 'x': x, 'y': y}

Then I run, in a new python console,

>>> from Solverz import iVar, Para, Ode
>>> x = iVar('x')
>>> y = iVar('y')
>>> lam = Para('lam')
>>> Ode('b', x + y * lam, diff_var=x).SYMBOLS
{'y': y, 'x': x, 'lam': lam}

The order of symbols in Eqn.SYMBOLS dict is not fixed. It is hard to write tests in such cases. Also, since the printing order of variables in the the generated modules is changing, we cannot reuse the compiled python functions if we reprint these modules.

I believe this should be attributed to sympy. To resolve this, we should use the sort() function to sort the items in Eqn.SYMBOLS in lexicographic order.

rzyu45 added a commit to rzyu45/Solverz-dev that referenced this issue Jun 1, 2024
@rzyu54 rzyu54 closed this as completed in f77864e Jun 3, 2024
@rzyu45 rzyu45 reopened this Jun 3, 2024
@rzyu45 rzyu45 changed the title To sort the symbols in lexicographic order To sort in an lexicographic order the objects in dict() Jun 12, 2024
@rzyu45 rzyu45 changed the title To sort in an lexicographic order the objects in dict() To sort in an lexicographic order the objects in dict() Jun 12, 2024
@rzyu45 rzyu45 self-assigned this Jun 12, 2024
@rzyu45
Copy link
Member Author

rzyu45 commented Jun 12, 2024

The locations of jac blocks in the Jac.blocks dict() vary in each run.

m = Model()
m.x = Var('x', [1, 1])
m.c = Param('c', 2)
m.f1 = Eqn('f1', 1 + exp(m.x[0]) + sin(m.x[1]))
m.f2 = Eqn('f2', m.x[0] + m.c / 2 * m.x[1] ** 2)

F, y0 = m.create_instance()

current_file_path = os.path.abspath(__file__)
current_folder = os.path.dirname(current_file_path)

test_folder_path = current_folder + '\\Solverz_test_ae_module_printer_with_hvp'
sys.path.extend([test_folder_path])

pyprinter = module_printer(F,
                           y0,
                           'test_module',
                           jit=True,
                           directory=test_folder_path,
                           make_hvp=True)
pyprinter.render()

from test_module import mdl, y
from test_module.num_func import Hvp_, inner_Hvp, inner_Hvp0, inner_Hvp1, inner_Hvp2

assert inspect.getsource(Hvp_) == expected_Hvp_
assert inspect.getsource(inner_Hvp) == expected_inner_Hvp
assert inspect.getsource(inner_Hvp0.func_code) == expected_inner_Hvp0
assert inspect.getsource(inner_Hvp1.func_code) == expected_inner_Hvp1
assert inspect.getsource(inner_Hvp2.func_code) == expected_inner_Hvp2

In the above example, the inner_Hvp0 and inner_Hvp1 interchanges occasionally. The test cannot be passed.
To resolve this, we ought to sort the jac blocks according to the keys. That is, first by the SolVar and then by the equation name.

@rzyu45
Copy link
Member Author

rzyu45 commented Jun 12, 2024

Partially resolved in 9cf7ccf

@rzyu45 rzyu45 linked a pull request Aug 5, 2024 that will close this issue
@rzyu54 rzyu54 closed this as completed Aug 6, 2024
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

Successfully merging a pull request may close this issue.

2 participants