Metadata-Version: 2.1
Name: pymoca
Version: 0.11.0.dev5
Summary: A Modelica to computer algebra system (CAS) translator.
Author: Pymoca Contributors
Maintainer-email: Jack Vreeken <jack@vreeken.me>, Kent Rutan <gs1150e@icloud.com>
License: Copyright (c) 2016-2021, James Goppert and the Pymoca contributors.
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        * Redistributions of source code must retain the above copyright notice, this
          list of conditions and the following disclaimer.
        
        * Redistributions in binary form must reproduce the above copyright notice,
          this list of conditions and the following disclaimer in the documentation
          and/or other materials provided with the distribution.
        
        * Neither the name of pymoca nor the names of its
          contributors may be used to endorse or promote products derived from
          this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
        
Project-URL: homepage, https://github.com/pymoca/pymoca
Keywords: modelica,simulation,compiler
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Other
Classifier: Topic :: Software Development
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Topic :: Scientific/Engineering :: Visualization
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX
Classifier: Operating System :: Unix
Classifier: Operating System :: MacOS
Classifier: Topic :: Software Development :: Code Generators
Classifier: Topic :: Software Development :: Compilers
Classifier: Topic :: Software Development :: Embedded Systems
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.8.2
Requires-Dist: antlr4-python3-runtime==4.13.*
Provides-Extra: casadi
Requires-Dist: casadi>=3.4.0; extra == "casadi"
Requires-Dist: setuptools>=60.0.0; extra == "casadi"
Provides-Extra: lxml
Requires-Dist: lxml>=3.5.0; extra == "lxml"
Requires-Dist: scipy>=0.13.3; extra == "lxml"
Provides-Extra: sympy
Requires-Dist: sympy>=0.7.6.1; extra == "sympy"
Requires-Dist: scipy>=0.13.3; extra == "sympy"
Requires-Dist: jinja2>=2.10.1; extra == "sympy"
Provides-Extra: examples
Requires-Dist: jupyterlab; extra == "examples"
Requires-Dist: matplotlib; extra == "examples"
Requires-Dist: control<=0.10.0,>=0.9.3.post2; extra == "examples"
Provides-Extra: all
Requires-Dist: pymoca[casadi,examples,lxml,sympy]; extra == "all"

# <img alt="Pymoca" src="branding/icons/pymocalogo.svg" height="60">

A Modelica to computer algebra system (CAS) translator written in Python.

[![CI](https://github.com/pymoca/pymoca/workflows/CI/badge.svg)](https://github.com/pymoca/pymoca/actions?query=workflow%3ACI)
[![Coverage](https://codecov.io/gh/pymoca/pymoca/branch/master/graph/badge.svg)](https://codecov.io/gh/pymoca/pymoca)
[![DOI](https://zenodo.org/badge/20664755.svg)](https://zenodo.org/badge/latestdoi/20664755)

## Overview
Pymoca can be used in applications that need to translate [Modelica](https://modelica.org) mathematical models into other forms. Pymoca can "flatten" a model containing a connected set of components defined by object-oriented Modelica classes into a set of variables and simultaneous equations that are easier to further process for analysis or simulation. It is particularly suited to provide Modelica models in symbolic form to [computer algebra systems](https://en.wikipedia.org/wiki/Computer_algebra_system). A common use in this context is to provide differential and algebraic equations for use in [optimal control problems](https://en.wikipedia.org/wiki/Optimal_control). Pymoca can translate Modelica to [CasADi](https://web.casadi.org), [SymPy](https://www.sympy.org), and [ModelicaXML](https://github.com/modelica-association/ModelicaXML), but most development and usage has been with CasADi.

## Install

For parser support without backend dependencies:
```bash
pip install pymoca
```

Other options are:
```bash
pip install "pymoca[casadi]"    # CasADi backend dependencies
pip install "pymoca[sympy]"     # SymPy backend dependencies
pip install "pymoca[lxml]"      # ModelicaXML backend dependencies

pip install "pymoca[examples]"  # To run Jupyter notebook examples in the repo

pip install "pymoca[all]"       # All of the above
```

## Usage

Pymoca reads and understands Modelica code (`pymoca.parser`) and provides access to an internal representation of the code called an Abstract Syntax Tree or AST (`pymoca.ast`). The AST is further processed to generate output in various formats (`pymoca.backends`). The `pymoca.tree` module provides functionality to transform the AST into a form that can be more easily used by the backends to generate the target output. In particular, `pymoca.tree` provides classes and functions to convert a hierarchical, object-oriented Modelica model of connected components into a "flat" system of equations and associated variables, parameters, and constants. Pymoca error checking is not always complete or easy to understand, so it is better to develop the Modelica code with other tools and then use Pymoca for translation.

The [test suite](test) contains examples showing how to use Pymoca and the subset of Modelica that it currently supports.

Here is an example using a simple spring and damper model from the test suite:

```Python
from pprint import pprint

import pymoca.parser
import pymoca.backends.casadi.generator as casadi_backend


MODELICA_MODEL = """
model Spring
    Real x, v_x;
    parameter Real c = 0.1;
    parameter Real k = 2;
equation
    der(x) = v_x;
    der(v_x) = -k*x - c*v_x;
end Spring;
"""

print("Modelica Model:\n", MODELICA_MODEL)

print("\nEquations from the parsed AST in a JSON representation:")
ast = pymoca.parser.parse(MODELICA_MODEL)
pprint(ast.to_json(ast.classes["Spring"].equations))

print("\nGenerated CasADi model:")
casadi_model = casadi_backend.generate(ast, "Spring")
print(casadi_model)
```

Some more interesting examples are in Jupyter notebooks:

* [Casadi Example](test/notebooks/Casadi.ipynb)
* [Sympy Example](test/notebooks/Spring.ipynb)

## Roadmap

See the [GitHub Projects](https://github.com/orgs/pymoca/projects) for plans. In particular, see the [Info Panel in the Modelica Flattening project](https://github.com/orgs/pymoca/projects/1/views/1?pane=info) for an overview of a project getting some current focus. Breaking API changes are expected.

<!--- vim:ts=4:sw=4:expandtab:
!-->
