pyfopid 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. pyfopid-0.1.0/.github/ISSUE_TEMPLATE/bug_report.md +26 -0
  2. pyfopid-0.1.0/.github/ISSUE_TEMPLATE/config.yml +1 -0
  3. pyfopid-0.1.0/.github/ISSUE_TEMPLATE/feature_request.md +16 -0
  4. pyfopid-0.1.0/.github/pull_request_template.md +12 -0
  5. pyfopid-0.1.0/.github/workflows/publish.yml +44 -0
  6. pyfopid-0.1.0/.github/workflows/tests.yml +24 -0
  7. pyfopid-0.1.0/.gitignore +12 -0
  8. pyfopid-0.1.0/CHANGELOG.md +13 -0
  9. pyfopid-0.1.0/CITATION.cff +16 -0
  10. pyfopid-0.1.0/CODE_OF_CONDUCT.md +24 -0
  11. pyfopid-0.1.0/CONTRIBUTING.md +63 -0
  12. pyfopid-0.1.0/LICENSE +28 -0
  13. pyfopid-0.1.0/PKG-INFO +151 -0
  14. pyfopid-0.1.0/README.md +126 -0
  15. pyfopid-0.1.0/examples/closed_loop.py +60 -0
  16. pyfopid-0.1.0/pyproject.toml +47 -0
  17. pyfopid-0.1.0/src/pyfopid/__init__.py +19 -0
  18. pyfopid-0.1.0/src/pyfopid/approx.py +103 -0
  19. pyfopid-0.1.0/src/pyfopid/controller.py +434 -0
  20. pyfopid-0.1.0/src/pyfopid/operators.py +176 -0
  21. pyfopid-0.1.0/tests/test_approx.py +66 -0
  22. pyfopid-0.1.0/tests/test_controller.py +325 -0
  23. pyfopid-0.1.0/tests/test_fomcon_reference.py +92 -0
  24. pyfopid-0.1.0/tests/test_operators.py +144 -0
  25. pyfopid-0.1.0/validation/fomcon/README.md +49 -0
  26. pyfopid-0.1.0/validation/fomcon/data/case1_oustaloup.csv +727 -0
  27. pyfopid-0.1.0/validation/fomcon/data/case2_fopid_params.csv +2 -0
  28. pyfopid-0.1.0/validation/fomcon/data/case2_fopid_response.csv +5002 -0
  29. pyfopid-0.1.0/validation/fomcon/data/environment.txt +7 -0
  30. pyfopid-0.1.0/validation/fomcon/data/log.txt +2 -0
  31. pyfopid-0.1.0/validation/fomcon/generate_reference.m +92 -0
@@ -0,0 +1,26 @@
1
+ ---
2
+ name: Bug report
3
+ about: Report incorrect results, errors or problems with the generated C code
4
+ title: "[Bug] "
5
+ labels: bug
6
+ ---
7
+
8
+ **Description**
9
+ A clear description of the problem.
10
+
11
+ **Minimal example**
12
+ ```python
13
+ # smallest script that reproduces the problem
14
+ ```
15
+
16
+ **Expected result**
17
+
18
+ **Obtained result**
19
+ (paste the full error message or output)
20
+
21
+ **Environment**
22
+ - pyfopid version:
23
+ - Python version:
24
+ - NumPy / SciPy / python-control versions:
25
+ - Operating system:
26
+ - For C export: microcontroller and compiler:
@@ -0,0 +1 @@
1
+ blank_issues_enabled: true
@@ -0,0 +1,16 @@
1
+ ---
2
+ name: Feature request
3
+ about: Propose a new function, option or improvement
4
+ title: "[Feature] "
5
+ labels: enhancement
6
+ ---
7
+
8
+ **What would you like to do?**
9
+ Describe the task or the use case.
10
+
11
+ **Proposed solution**
12
+ How the feature could work, including the API if you have one in mind.
13
+
14
+ **References**
15
+ Papers, toolboxes (for example FOMCON or FOTF) or formulas that define the
16
+ expected behaviour.
@@ -0,0 +1,12 @@
1
+ ## Summary
2
+ What does this pull request change, and why?
3
+
4
+ ## Related issue
5
+ Closes #
6
+
7
+ ## Checklist
8
+ - [ ] `python -m pytest` passes locally
9
+ - [ ] Tests added or updated for the change
10
+ - [ ] Docstrings, README and `CHANGELOG.md` updated if the public API or behaviour changed
11
+ - [ ] For numerical changes: tests against FOMCON reference data still pass
12
+ - [ ] For C export changes: generated code still matches the Python implementation
@@ -0,0 +1,44 @@
1
+ name: Publish to PyPI
2
+
3
+ on:
4
+ release:
5
+ types: [published]
6
+
7
+ jobs:
8
+ test:
9
+ runs-on: ubuntu-latest
10
+ steps:
11
+ - uses: actions/checkout@v4
12
+ - uses: actions/setup-python@v5
13
+ with:
14
+ python-version: "3.12"
15
+ - run: python -m pip install -e ".[test]"
16
+ - run: python -m pytest
17
+
18
+ build:
19
+ needs: test
20
+ runs-on: ubuntu-latest
21
+ steps:
22
+ - uses: actions/checkout@v4
23
+ - uses: actions/setup-python@v5
24
+ with:
25
+ python-version: "3.12"
26
+ - run: python -m pip install build
27
+ - run: python -m build
28
+ - uses: actions/upload-artifact@v4
29
+ with:
30
+ name: dist
31
+ path: dist/
32
+
33
+ publish:
34
+ needs: build
35
+ runs-on: ubuntu-latest
36
+ environment: pypi
37
+ permissions:
38
+ id-token: write
39
+ steps:
40
+ - uses: actions/download-artifact@v4
41
+ with:
42
+ name: dist
43
+ path: dist/
44
+ - uses: pypa/gh-action-pypi-publish@release/v1
@@ -0,0 +1,24 @@
1
+ name: tests
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ jobs:
9
+ test:
10
+ runs-on: ${{ matrix.os }}
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ os: [ubuntu-latest, windows-latest, macos-latest]
15
+ python-version: ["3.10", "3.11", "3.12", "3.13"]
16
+ steps:
17
+ - uses: actions/checkout@v4
18
+ - uses: actions/setup-python@v5
19
+ with:
20
+ python-version: ${{ matrix.python-version }}
21
+ - name: Install
22
+ run: python -m pip install -e ".[test]"
23
+ - name: Test
24
+ run: python -m pytest
@@ -0,0 +1,12 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ build/
5
+ dist/
6
+ .pytest_cache/
7
+ .venv/
8
+ venv/
9
+ .ipynb_checkpoints/
10
+ *.h
11
+ !examples/*.h
12
+ .DS_Store
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (2026-12-04)
4
+
5
+ - Grünwald-Letnikov weights, derivatives and integrals with short-memory option.
6
+ - Caputo derivative for arbitrary positive order with initial conditions.
7
+ - Oustaloup approximation of `s^alpha` for any real order.
8
+ - Discrete-time `FOPID` controller:
9
+ - accumulated fractional integral `s^-λ = s^-1 · s^(1-λ)` (default) or direct GL;
10
+ - derivative on error or on measurement, optional first-order filter;
11
+ - conditional-integration anti-windup, optional integral clamp;
12
+ - bumpless `reset(error, measurement, keep_integral)`;
13
+ - continuous `tf()` model and C99 header export with the same options.
@@ -0,0 +1,16 @@
1
+ cff-version: 1.2.0
2
+ message: "If you use this software, please cite it as below."
3
+ title: "pyfopid: fractional-order modelling and control in Python"
4
+ version: 0.1.0.dev0
5
+ date-released: 2026-11-22
6
+ license: BSD-3-Clause
7
+ repository-code: "https://github.com/ThermalProjects/pyfopid"
8
+ keywords:
9
+ - fractional calculus
10
+ - fractional-order control
11
+ - FOPID
12
+ authors:
13
+ - family-names: "Ferrer Pareja"
14
+ given-names: "Miguel F."
15
+ orcid: "https://orcid.org/0009-0000-7518-3583"
16
+ affiliation: "SIANI, Universidad de Las Palmas de Gran Canaria"
@@ -0,0 +1,24 @@
1
+ # Code of Conduct
2
+
3
+ pyfopid adopts the
4
+ [Contributor Covenant, version 2.1](https://www.contributor-covenant.org/version/2/1/code_of_conduct/)
5
+ as its code of conduct.
6
+
7
+ In short, we expect everyone who takes part in the project, in issues, pull
8
+ requests, discussions or any other channel, to:
9
+
10
+ - be respectful and considerate towards other people;
11
+ - give and accept constructive feedback on the work, not on the person;
12
+ - avoid harassment, personal attacks and discriminatory language;
13
+ - respect the privacy of others.
14
+
15
+ ## Reporting
16
+
17
+ Instances of unacceptable behaviour may be reported to the project maintainer,
18
+ Miguel F. Ferrer Pareja, at miguel.ferrer104@alu.ulpgc.es. All reports will be
19
+ reviewed and handled confidentially. The maintainer may remove, edit or reject
20
+ comments, commits, code, issues and other contributions that do not follow this
21
+ code of conduct.
22
+
23
+ The full text of the Contributor Covenant 2.1, including its enforcement
24
+ guidelines, applies and is available at the link above.
@@ -0,0 +1,63 @@
1
+ # Contributing to pyfopid
2
+
3
+ Thank you for your interest in pyfopid. Bug reports, questions, documentation
4
+ fixes and code contributions are welcome.
5
+
6
+ ## Reporting a bug or asking a question
7
+
8
+ Open an issue at <https://github.com/ThermalProjects/pyfopid/issues> and use
9
+ the matching template. For a bug, include:
10
+
11
+ - the pyfopid, Python, NumPy, SciPy and python-control versions;
12
+ - a minimal script that reproduces the problem;
13
+ - the output you expected and the output you obtained.
14
+
15
+ If the problem concerns generated C code, also state the target
16
+ microcontroller and compiler.
17
+
18
+ ## Development set-up
19
+
20
+ ```bash
21
+ git clone https://github.com/ThermalProjects/pyfopid.git
22
+ cd pyfopid
23
+ python -m pip install -e ".[test]"
24
+ python -m pytest
25
+ ```
26
+
27
+ All tests must pass before a pull request is merged. The continuous
28
+ integration runs the test suite on Linux, Windows and macOS with Python
29
+ 3.10–3.13.
30
+
31
+ ## Making a change
32
+
33
+ 1. Fork the repository and create a branch from `main`.
34
+ 2. Keep each pull request focused on one change.
35
+ 3. Add or update tests for any change in behaviour. Numerical code is tested
36
+ against closed-form results or reference data, with tolerances that reflect
37
+ the measured agreement.
38
+ 4. Update the docstrings, the README and `CHANGELOG.md` when the public API
39
+ or the behaviour changes.
40
+ 5. Open a pull request and fill in the template.
41
+
42
+ ## Numerical and embedded code
43
+
44
+ - Changes to the Grünwald–Letnikov operators, the Oustaloup approximation or
45
+ the `FOPID` class must keep the tests against the FOMCON reference data in
46
+ `validation/fomcon/` passing. To regenerate the reference data, run
47
+ `validation/fomcon/generate_reference.m` in MATLAB with FOMCON and commit the
48
+ resulting files in `validation/fomcon/data/`.
49
+ - Changes to the C export (`FOPID.to_c_header`) must keep the generated code
50
+ equivalent, sample by sample, to the Python implementation. The tests compile
51
+ the generated header with the host C compiler and compare both outputs.
52
+
53
+ ## Code style
54
+
55
+ - Follow PEP 8 and use type hints in public functions.
56
+ - Write docstrings in NumPy style, with units where they apply.
57
+ - Use plain, descriptive names for parameters (for example `memory`, `dt`,
58
+ `d_filter_tau`).
59
+
60
+ ## Code of conduct
61
+
62
+ This project follows the [Code of Conduct](CODE_OF_CONDUCT.md). By
63
+ participating, you agree to abide by its terms.
pyfopid-0.1.0/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Miguel F. Ferrer Pareja
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
pyfopid-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,151 @@
1
+ Metadata-Version: 2.5
2
+ Name: pyfopid
3
+ Version: 0.1.0
4
+ Summary: Fractional-order modelling and control in Python: Grünwald-Letnikov operators, Oustaloup approximations and FOPID controllers.
5
+ Project-URL: Homepage, https://github.com/ThermalProjects/pyfopid
6
+ Project-URL: Issues, https://github.com/ThermalProjects/pyfopid/issues
7
+ Author: Miguel F. Ferrer Pareja
8
+ License-Expression: BSD-3-Clause
9
+ License-File: LICENSE
10
+ Keywords: FOPID,Grünwald-Letnikov,Oustaloup,control systems,fractional calculus,fractional-order control
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Topic :: Scientific/Engineering
15
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
16
+ Requires-Python: >=3.10
17
+ Requires-Dist: control>=0.10
18
+ Requires-Dist: numpy>=1.24
19
+ Requires-Dist: scipy>=1.10
20
+ Provides-Extra: plot
21
+ Requires-Dist: matplotlib>=3.7; extra == 'plot'
22
+ Provides-Extra: test
23
+ Requires-Dist: pytest>=7; extra == 'test'
24
+ Description-Content-Type: text/markdown
25
+
26
+ # pyfopid
27
+
28
+ **Fractional-order modelling and control in Python.**
29
+
30
+ `pyfopid` provides tested, documented building blocks for fractional-order
31
+ control: Grünwald-Letnikov operators, Oustaloup rational approximations,
32
+ and a discrete-time FOPID (PI^λD^μ) controller that interoperates with
33
+ [python-control](https://python-control.readthedocs.io) and exports to C for
34
+ embedded targets (Arduino, ESP32, STM32).
35
+
36
+ > Status: alpha (0.1.0.dev0). API may change before 1.0.
37
+
38
+ ## Installation
39
+
40
+ ```bash
41
+ pip install pyfopid # once released on PyPI
42
+ pip install -e ".[test]" # development install from a clone
43
+ ```
44
+
45
+ Requires Python ≥ 3.10, NumPy, SciPy and python-control ≥ 0.10.
46
+
47
+ ## Quick start
48
+
49
+ ```python
50
+ import numpy as np
51
+ import pyfopid as pf
52
+
53
+ # Fractional derivative of order 0.5 of t^2 (Grünwald-Letnikov)
54
+ h = 1e-3
55
+ t = np.arange(0, 1 + h, h)
56
+ d = pf.gl_derivative(t**2, 0.5, h)
57
+
58
+ # Oustaloup approximation of s^0.5 in [0.01, 100] rad/s as a control.TransferFunction
59
+ G = pf.oustaloup(0.5, wb=1e-2, wh=1e2, N=5)
60
+
61
+ # Discrete FOPID controller (accumulated fractional integral, filtered
62
+ # derivative on the measurement, saturation with anti-windup)
63
+ c = pf.FOPID(kp=2.0, ki=1.0, kd=0.1, lam=0.9, mu=0.5, dt=0.01, memory=2000,
64
+ u_min=0.0, u_max=12.0,
65
+ derivative_on="measurement", d_filter_tau=0.05)
66
+ u = c.step(error=0.3, measurement=24.7)
67
+
68
+ # Export the same controller to a C header for a microcontroller
69
+ open("fopid.h", "w").write(c.to_c_header("fopid"))
70
+ ```
71
+
72
+ See [`examples/closed_loop.py`](examples/closed_loop.py) for a full
73
+ closed-loop simulation.
74
+
75
+ ## Features (v0.1)
76
+
77
+ | Module | Contents |
78
+ |---|---|
79
+ | `operators` | `gl_weights`, `gl_derivative` (with short-memory principle), `fractional_integral`, `caputo_derivative` |
80
+ | `approx` | `oustaloup` (any real order; integer part realised exactly), `ZPK`, `freqresp_zpk` |
81
+ | `controller` | `FOPID`: accumulated (default) or direct GL fractional integral, derivative on error or measurement, first-order derivative filter, conditional-integration anti-windup and integral clamp, bumpless `reset()`, `.tf()` (Oustaloup-based continuous model), `.to_c_header()` (C99, same options) |
82
+
83
+ ### Design notes
84
+
85
+ - **Accumulated integral (default).** `s^-λ = s^-1 · s^(1-λ)`: the fractional
86
+ operator of order `1-λ` is evaluated with a GL sum and followed by an
87
+ integer integrator. This keeps genuine integral action (zero steady-state
88
+ error) for any memory length. The direct GL integral (`integral="gl"`) has a
89
+ finite DC gain when the memory is truncated.
90
+ - **Memory length.** The fractional behaviour is effective for frequencies
91
+ above roughly `1/(L·dt)` rad/s. Choose `L·dt` comparable to the dominant
92
+ time constants of the plant.
93
+ - **Derivative.** For `μ > 1` noise amplification is severe; use
94
+ `d_filter_tau`. With non-integer `μ` the GL derivative of a constant is not
95
+ zero, so differentiating the error and differentiating the measurement are
96
+ not equivalent even for a constant set point; `derivative_on="measurement"`
97
+ also removes the set-point kick.
98
+
99
+ ## Verification
100
+
101
+ The test suite (`pytest`) checks:
102
+
103
+ - GL weights against the signed binomial coefficients `(-1)^j C(α, j)`,
104
+ including the sign pattern.
105
+ - GL derivatives and integrals against closed-form results
106
+ (`D^α t^k = Γ(k+1)/Γ(k+1−α) t^(k−α)`, `I^α 1 = t^α/Γ(1+α)`) and the
107
+ expected first-order convergence O(h).
108
+ - Caputo derivatives (constants map to zero; orders above one with initial
109
+ conditions).
110
+ - Oustaloup approximations against `(jω)^α`: with the band
111
+ `[wb, wh]` chosen at least two decades wider than the frequency range of
112
+ interest, errors are < 0.05 dB and < 0.5°. One decade from the band edges
113
+ the phase error rises to ≈ 3° for |α| = 0.5; this is dominated by band
114
+ truncation, not by `N`.
115
+ - Exact reduction of `FOPID` to the classical discrete PID for λ = μ = 1 in
116
+ both integral modes.
117
+ - Closed-loop tracking with and without saturation; zero steady-state error of
118
+ the accumulated integral with a short memory.
119
+ - Sample-by-sample agreement (rtol 1e-9) in closed loop with an independent
120
+ transcription of an FOPID firmware used on a Peltier test bench.
121
+ - Cross-validation against FOMCON 1.50.4 (MATLAB): Oustaloup approximations
122
+ identical to round-off for |α| ≤ 0.5 and more accurate for |α| > 0.5
123
+ (0.54° vs 5.09° phase error at α = −0.9); FOPID time response identical to
124
+ FOMCON `lsim` (relative difference 7.5e-12). See
125
+ [`validation/fomcon`](validation/fomcon/README.md).
126
+ - Compiled C output against Python for all integral/derivative options.
127
+
128
+ ## Citing
129
+
130
+ If you use `pyfopid` in research, please cite it using the metadata in
131
+ [`CITATION.cff`](CITATION.cff). A DOI will be issued through Zenodo with the
132
+ first release.
133
+
134
+ ## Contributing
135
+
136
+ Bug reports, questions and pull requests are welcome. See
137
+ [CONTRIBUTING.md](CONTRIBUTING.md) for the development set-up and the
138
+ [Code of Conduct](CODE_OF_CONDUCT.md).
139
+
140
+ ## Acknowledgements
141
+
142
+ pyfopid is developed at the University Institute of Intelligent Systems and
143
+ Numerical Applications in Engineering (SIANI), University of Las Palmas de
144
+ Gran Canaria. The author thanks Carlos Sánchez Morales and Federico León Zerpa
145
+ (Department of Process Engineering) and Alejandro Ramos Martín (SIANI) for
146
+ their supervision and for their review of the experimental work in which the
147
+ library was used.
148
+
149
+ ## License
150
+
151
+ BSD 3-Clause. See [LICENSE](LICENSE).
@@ -0,0 +1,126 @@
1
+ # pyfopid
2
+
3
+ **Fractional-order modelling and control in Python.**
4
+
5
+ `pyfopid` provides tested, documented building blocks for fractional-order
6
+ control: Grünwald-Letnikov operators, Oustaloup rational approximations,
7
+ and a discrete-time FOPID (PI^λD^μ) controller that interoperates with
8
+ [python-control](https://python-control.readthedocs.io) and exports to C for
9
+ embedded targets (Arduino, ESP32, STM32).
10
+
11
+ > Status: alpha (0.1.0.dev0). API may change before 1.0.
12
+
13
+ ## Installation
14
+
15
+ ```bash
16
+ pip install pyfopid # once released on PyPI
17
+ pip install -e ".[test]" # development install from a clone
18
+ ```
19
+
20
+ Requires Python ≥ 3.10, NumPy, SciPy and python-control ≥ 0.10.
21
+
22
+ ## Quick start
23
+
24
+ ```python
25
+ import numpy as np
26
+ import pyfopid as pf
27
+
28
+ # Fractional derivative of order 0.5 of t^2 (Grünwald-Letnikov)
29
+ h = 1e-3
30
+ t = np.arange(0, 1 + h, h)
31
+ d = pf.gl_derivative(t**2, 0.5, h)
32
+
33
+ # Oustaloup approximation of s^0.5 in [0.01, 100] rad/s as a control.TransferFunction
34
+ G = pf.oustaloup(0.5, wb=1e-2, wh=1e2, N=5)
35
+
36
+ # Discrete FOPID controller (accumulated fractional integral, filtered
37
+ # derivative on the measurement, saturation with anti-windup)
38
+ c = pf.FOPID(kp=2.0, ki=1.0, kd=0.1, lam=0.9, mu=0.5, dt=0.01, memory=2000,
39
+ u_min=0.0, u_max=12.0,
40
+ derivative_on="measurement", d_filter_tau=0.05)
41
+ u = c.step(error=0.3, measurement=24.7)
42
+
43
+ # Export the same controller to a C header for a microcontroller
44
+ open("fopid.h", "w").write(c.to_c_header("fopid"))
45
+ ```
46
+
47
+ See [`examples/closed_loop.py`](examples/closed_loop.py) for a full
48
+ closed-loop simulation.
49
+
50
+ ## Features (v0.1)
51
+
52
+ | Module | Contents |
53
+ |---|---|
54
+ | `operators` | `gl_weights`, `gl_derivative` (with short-memory principle), `fractional_integral`, `caputo_derivative` |
55
+ | `approx` | `oustaloup` (any real order; integer part realised exactly), `ZPK`, `freqresp_zpk` |
56
+ | `controller` | `FOPID`: accumulated (default) or direct GL fractional integral, derivative on error or measurement, first-order derivative filter, conditional-integration anti-windup and integral clamp, bumpless `reset()`, `.tf()` (Oustaloup-based continuous model), `.to_c_header()` (C99, same options) |
57
+
58
+ ### Design notes
59
+
60
+ - **Accumulated integral (default).** `s^-λ = s^-1 · s^(1-λ)`: the fractional
61
+ operator of order `1-λ` is evaluated with a GL sum and followed by an
62
+ integer integrator. This keeps genuine integral action (zero steady-state
63
+ error) for any memory length. The direct GL integral (`integral="gl"`) has a
64
+ finite DC gain when the memory is truncated.
65
+ - **Memory length.** The fractional behaviour is effective for frequencies
66
+ above roughly `1/(L·dt)` rad/s. Choose `L·dt` comparable to the dominant
67
+ time constants of the plant.
68
+ - **Derivative.** For `μ > 1` noise amplification is severe; use
69
+ `d_filter_tau`. With non-integer `μ` the GL derivative of a constant is not
70
+ zero, so differentiating the error and differentiating the measurement are
71
+ not equivalent even for a constant set point; `derivative_on="measurement"`
72
+ also removes the set-point kick.
73
+
74
+ ## Verification
75
+
76
+ The test suite (`pytest`) checks:
77
+
78
+ - GL weights against the signed binomial coefficients `(-1)^j C(α, j)`,
79
+ including the sign pattern.
80
+ - GL derivatives and integrals against closed-form results
81
+ (`D^α t^k = Γ(k+1)/Γ(k+1−α) t^(k−α)`, `I^α 1 = t^α/Γ(1+α)`) and the
82
+ expected first-order convergence O(h).
83
+ - Caputo derivatives (constants map to zero; orders above one with initial
84
+ conditions).
85
+ - Oustaloup approximations against `(jω)^α`: with the band
86
+ `[wb, wh]` chosen at least two decades wider than the frequency range of
87
+ interest, errors are < 0.05 dB and < 0.5°. One decade from the band edges
88
+ the phase error rises to ≈ 3° for |α| = 0.5; this is dominated by band
89
+ truncation, not by `N`.
90
+ - Exact reduction of `FOPID` to the classical discrete PID for λ = μ = 1 in
91
+ both integral modes.
92
+ - Closed-loop tracking with and without saturation; zero steady-state error of
93
+ the accumulated integral with a short memory.
94
+ - Sample-by-sample agreement (rtol 1e-9) in closed loop with an independent
95
+ transcription of an FOPID firmware used on a Peltier test bench.
96
+ - Cross-validation against FOMCON 1.50.4 (MATLAB): Oustaloup approximations
97
+ identical to round-off for |α| ≤ 0.5 and more accurate for |α| > 0.5
98
+ (0.54° vs 5.09° phase error at α = −0.9); FOPID time response identical to
99
+ FOMCON `lsim` (relative difference 7.5e-12). See
100
+ [`validation/fomcon`](validation/fomcon/README.md).
101
+ - Compiled C output against Python for all integral/derivative options.
102
+
103
+ ## Citing
104
+
105
+ If you use `pyfopid` in research, please cite it using the metadata in
106
+ [`CITATION.cff`](CITATION.cff). A DOI will be issued through Zenodo with the
107
+ first release.
108
+
109
+ ## Contributing
110
+
111
+ Bug reports, questions and pull requests are welcome. See
112
+ [CONTRIBUTING.md](CONTRIBUTING.md) for the development set-up and the
113
+ [Code of Conduct](CODE_OF_CONDUCT.md).
114
+
115
+ ## Acknowledgements
116
+
117
+ pyfopid is developed at the University Institute of Intelligent Systems and
118
+ Numerical Applications in Engineering (SIANI), University of Las Palmas de
119
+ Gran Canaria. The author thanks Carlos Sánchez Morales and Federico León Zerpa
120
+ (Department of Process Engineering) and Alejandro Ramos Martín (SIANI) for
121
+ their supervision and for their review of the experimental work in which the
122
+ library was used.
123
+
124
+ ## License
125
+
126
+ BSD 3-Clause. See [LICENSE](LICENSE).
@@ -0,0 +1,60 @@
1
+ """Closed-loop simulation: FOPID vs classical PID on a first-order-plus-dead-time plant.
2
+
3
+ Plant: G(s) = K e^{-theta s} / (tau s + 1), simulated with forward Euler.
4
+ Run: python examples/closed_loop.py
5
+ """
6
+
7
+ import numpy as np
8
+
9
+ import pyfopid as pf
10
+
11
+ K, TAU, THETA = 1.0, 2.0, 0.3
12
+ DT, T_END = 0.01, 30.0
13
+
14
+
15
+ def simulate(controller):
16
+ n = int(T_END / DT)
17
+ delay = int(THETA / DT)
18
+ y = np.zeros(n)
19
+ u_buf = np.zeros(n)
20
+ for k in range(n - 1):
21
+ u_buf[k] = controller.step(1.0 - y[k])
22
+ u_delayed = u_buf[k - delay] if k >= delay else 0.0
23
+ y[k + 1] = y[k] + DT * (-y[k] + K * u_delayed) / TAU
24
+ return np.arange(n) * DT, y, u_buf
25
+
26
+
27
+ def metrics(t, y):
28
+ overshoot = max(0.0, y.max() - 1.0) * 100
29
+ outside = np.where(np.abs(y - 1.0) > 0.02)[0]
30
+ settling = t[outside[-1] + 1] if outside.size and outside[-1] + 1 < t.size else 0.0
31
+ iae = np.sum(np.abs(1.0 - y)) * DT
32
+ return overshoot, settling, iae
33
+
34
+
35
+ if __name__ == "__main__":
36
+ pid = pf.FOPID(kp=2.0, ki=1.0, kd=0.3, lam=1.0, mu=1.0, dt=DT, memory=3000, u_min=0, u_max=5)
37
+ fopid = pf.FOPID(kp=2.0, ki=1.0, kd=0.3, lam=0.95, mu=0.7, dt=DT, memory=3000, u_min=0, u_max=5,
38
+ d_filter_tau=0.02)
39
+
40
+ print(f"{'controller':<8} {'overshoot %':>12} {'t_s (2%) s':>12} {'IAE':>8}")
41
+ results = {}
42
+ for name, c in (("PID", pid), ("FOPID", fopid)):
43
+ t, y, u = simulate(c)
44
+ results[name] = (t, y)
45
+ os_, ts, iae = metrics(t, y)
46
+ print(f"{name:<8} {os_:12.2f} {ts:12.2f} {iae:8.3f}")
47
+
48
+ try:
49
+ import matplotlib.pyplot as plt
50
+
51
+ for name, (t, y) in results.items():
52
+ plt.plot(t, y, label=name)
53
+ plt.axhline(1.0, color="k", lw=0.5)
54
+ plt.xlabel("Time (s)")
55
+ plt.ylabel("Output")
56
+ plt.legend()
57
+ plt.tight_layout()
58
+ plt.show()
59
+ except ImportError:
60
+ pass
@@ -0,0 +1,47 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "pyfopid"
7
+ dynamic = ["version"]
8
+ description = "Fractional-order modelling and control in Python: Grünwald-Letnikov operators, Oustaloup approximations and FOPID controllers."
9
+ readme = "README.md"
10
+ license = "BSD-3-Clause"
11
+ license-files = ["LICENSE"]
12
+ requires-python = ">=3.10"
13
+ authors = [{ name = "Miguel F. Ferrer Pareja" }]
14
+ keywords = [
15
+ "fractional calculus",
16
+ "fractional-order control",
17
+ "FOPID",
18
+ "Grünwald-Letnikov",
19
+ "Oustaloup",
20
+ "control systems",
21
+ ]
22
+ classifiers = [
23
+ "Development Status :: 3 - Alpha",
24
+ "Intended Audience :: Science/Research",
25
+ "Programming Language :: Python :: 3",
26
+ "Topic :: Scientific/Engineering",
27
+ "Topic :: Scientific/Engineering :: Mathematics",
28
+ ]
29
+ dependencies = ["numpy>=1.24", "scipy>=1.10", "control>=0.10"]
30
+
31
+ [project.optional-dependencies]
32
+ test = ["pytest>=7"]
33
+ plot = ["matplotlib>=3.7"]
34
+
35
+ [project.urls]
36
+ Homepage = "https://github.com/ThermalProjects/pyfopid"
37
+ Issues = "https://github.com/ThermalProjects/pyfopid/issues"
38
+
39
+ [tool.hatch.version]
40
+ path = "src/pyfopid/__init__.py"
41
+
42
+ [tool.hatch.build.targets.wheel]
43
+ packages = ["src/pyfopid"]
44
+
45
+ [tool.pytest.ini_options]
46
+ testpaths = ["tests", "src/pyfopid"]
47
+ addopts = "--doctest-modules -q"