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

Add vars eqs #22

Merged
merged 9 commits into from
Jun 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .style.yapf
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ join_multiple_lines=False
spaces_around_default_or_named_assign=False

# Use spaces around the power operator.
spaces_around_power_operator=False
spaces_around_power_operator=True

# The number of spaces required before a trailing comment.
spaces_before_comment=2
Expand Down
10 changes: 10 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ Physics

.. automodule:: essm.equations.physics.thermodynamics
:members:


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add new section here ...

Internals
=========

.. automodule:: essm.variables._core
:members:

.. automodule:: essm.equations._core
:members:
12 changes: 9 additions & 3 deletions essm/_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ def __str__(self):
result += '\n\n__all__ = (\n{0}\n)'.format(
'\n'.join(
" '{0}',".format(var['name']) for var in self.vars))
reformatted_result = FormatCode(result, style_config=STYLE_YAPF)
return reformatted_result[0]
result = FormatCode(result, style_config=STYLE_YAPF)[0]
return result

def var(
self,
Expand Down Expand Up @@ -277,8 +277,10 @@ def eq(self, name, expr, doc='', parents=None, variables=None):

# Serialize the internal variables.
writer = VariableWriter()
internal_variables = set()
for variable in variables:
writer.var(**variable)
internal_variables.add(variable['name'])
variables = re.sub(
r'^',
4 * ' ',
Expand All @@ -290,8 +292,11 @@ def eq(self, name, expr, doc='', parents=None, variables=None):
self._imports[key] |= value
else:
variables = ''
writer = None
internal_variables = set()

context = {
'_variable_writer': writer,
"name": name,
"doc": doc,
"expr": expr,
Expand All @@ -301,7 +306,8 @@ def eq(self, name, expr, doc='', parents=None, variables=None):

# register all imports
for arg in expr.args():
if arg in Variable.__registry__:
if str(arg) not in internal_variables and\
arg in Variable.__registry__:
self._imports[Variable.__registry__[arg].__module__].add(
str(arg))

Expand Down
56 changes: 54 additions & 2 deletions essm/equations/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,59 @@


class EquationMeta(type):
"""Equation interface."""
r"""Equation interface.

Defines an equation with a docstring and internal variables,
if needed.

Example:

.. code-block:: python

from ..variables.units import meter, second

class test(Equation):
\"\"\"Test equation.\"\"\"

class d(Variable):
\"\"\"Test variable.\"\"\"
unit = meter

class t(Variable):
\"\"\"Test variable.\"\"\"
unit = second

class v(Variable):
\"\"\"Test variable.\"\"\"
unit = meter/second

expr = v == d / t

:raises ValueError: if the units are inconsistent.

Example:

.. code-block:: python

from ..variables.units import meter, second

class test(Equation):
\"\"\"Test equation with inconsistent units.\"\"\"

class d(Variable):
\"\"\"Test variable.\"\"\"
unit = meter

class t(Variable):
\"\"\"Test variable.\"\"\"
unit = second

class v(Variable):
\"\"\"Test variable.\"\"\"
unit = meter/second

expr = v == d * t
"""

def __new__(cls, name, parents, dct):
"""Build and register new variable."""
Expand Down Expand Up @@ -76,4 +128,4 @@ def __doc__(self):
return self.definition.__doc__


__all__ = ('Equation', )
__all__ = ('Equation', 'EquationMeta')
2 changes: 1 addition & 1 deletion essm/style.yapf
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ join_multiple_lines=False
spaces_around_default_or_named_assign=False

# Use spaces around the power operator.
spaces_around_power_operator=False
spaces_around_power_operator=True

# The number of spaces required before a trailing comment.
spaces_before_comment=2
Expand Down
27 changes: 26 additions & 1 deletion tests/test_equations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

import pytest

from essm._generator import EquationWriter
from essm.equations import Equation
from essm.variables import Variable
from essm.variables.units import joule, kelvin, meter, mole, second
from sage.all import solve
from sage.all import solve, var


class demo_g(Variable):
Expand Down Expand Up @@ -93,3 +94,27 @@ class demo_double(Equation): # ignore: W0232
expr = demo_g = demo_g

assert Equation.__registry__[demo_double].__doc__ == 'Second.'


def test_equation_writer(tmpdir):
"""EquationWriter creates importable file with internal variables."""
g = {}
d = var('d')
t = var('t')
writer_td = EquationWriter(docstring='Test of Equation_writer.')
writer_td.eq(
'demo_fall',
demo_g == d / t ** 2,
doc='Test equation.\n\n (Some reference)\n ',
variables=[{
"name": "d",
"value": '0.9',
"units": meter,
"latexname": 'p_1'}, {
"name": "t",
"units": second,
"latexname": 'p_2'}])
eq_file = tmpdir.mkdir('test').join('test_equations.py')
writer_td.write(eq_file.strpath)
execfile(eq_file.strpath, g)
assert g['demo_fall'].definition.d.definition.default == 0.9