Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions geochemistrypi-chemical-modeling/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

# Ruff stuff:
.ruff_cache/

# PyPI configuration file
.pypirc
20 changes: 20 additions & 0 deletions geochemistrypi-chemical-modeling/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) [2024] [Chufan Zhou]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

...

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
59 changes: 59 additions & 0 deletions geochemistrypi-chemical-modeling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# GeochemistryPi Chemical Modeling

A Python package for solving geochemistry equations using different numerical methods (fsolve and root).

## Features

- Solve chemical equations using scipy.optimize.fsolve
- Solve chemical equations using scipy.optimize.root
- Compare results from both methods
- Command-line interface for easy use

## Installation

```bash
pip install geochemistrypi-chemical-modeling
```

## Usage

```bash
# Using fsolve method
geochemistrypi solve --initial-guess 0.5 0.5 2.0

# Using root method
geochemistrypi root-method --initial-guess 0.5 0.5 2.0

# Compare both methods
geochemistrypi compare --initial-guess 0.5 0.5 2.0
```

## Requirements

- Python >= 3.8
- numpy
- scipy
- typer
- rich

## Development

To contribute to this project:

1. Clone the repository
2. Install development dependencies:
```bash
pip install -e ".[dev]"
```
3. Run tests:
```bash
python -m pytest
```

## License

This project is licensed under the MIT License - see the LICENSE.txt file for details.

## Author

Chufan Zhou (1176733817@qq.com)
42 changes: 42 additions & 0 deletions geochemistrypi-chemical-modeling/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "geochemistrypi-chemical-modeling"
version = "0.1.0"
authors = [
{ name="Chufan Zhou", email="1176733817@qq.com" },
]
description = "A package for chemical modeling using fsolve and root methods."
readme = "README.md"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
"License :: OSI Approved :: MIT License",
"Topic :: Scientific/Engineering :: Chemistry",
"Intended Audience :: Science/Research",
]
license = "MIT"
license-files = ["LICENSE.txt"]
dependencies = [
"numpy",
"scipy",
"typer",
"rich",
]

[project.urls]
Homepage = "https://github.com/Yoko286/Geochemistrypi-chemical-modeling"
Issues = "https://github.com/Yoko286/Geochemistrypi-chemical-modeling/issues"
Documentation = "https://geochemistrypi.readthedocs.io/"

[tool.hatch.build]
directory = "dist"

[tool.hatch.build.targets.wheel]
packages = ["src"]

[project.scripts]
geochemistrypi = "geochemistrypi.cli:app"
Empty file.
74 changes: 74 additions & 0 deletions geochemistrypi-chemical-modeling/src/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from typing import Tuple
import typer
from .fsolve import solve_equations as fsolve_solve
from .root import equations, jacobi_matrix
from scipy.optimize import root

app = typer.Typer(help="A command line tool to compare fsolve and root methods for solving equations.")

@app.command()
def fsolve(
phi_ref: float = typer.Option(0.5, "--phi-ref", "-p", help="Initial guess for φ_ref"),
beta_sple: float = typer.Option(0.5, "--beta-sple", "-b", help="Initial guess for β_sple"),
beta_mix: float = typer.Option(2.0, "--beta-mix", "-m", help="Initial guess for β_mix")
) -> None:
"""
Solve the system of equations using scipy.optimize.fsolve method.

Args:
phi_ref (float): Initial guess for φ_ref parameter
beta_sple (float): Initial guess for β_sple parameter
beta_mix (float): Initial guess for β_mix parameter
"""
initial_guess = (phi_ref, beta_sple, beta_mix)
solution = fsolve_solve(initial_guess)
typer.echo(f"fsolve Solution: {solution}")


@app.command()
def root_method(
phi_ref: float = typer.Option(0.5, "--phi-ref", "-p", help="Initial guess for φ_ref"),
beta_sple: float = typer.Option(0.5, "--beta-sple", "-b", help="Initial guess for β_sple"),
beta_mix: float = typer.Option(2.0, "--beta-mix", "-m", help="Initial guess for β_mix")
) -> None:
"""
Solve the system of equations using scipy.optimize.root method.

Args:
phi_ref (float): Initial guess for φ_ref parameter
beta_sple (float): Initial guess for β_sple parameter
beta_mix (float): Initial guess for β_mix parameter
"""
initial_guess = (phi_ref, beta_sple, beta_mix)
solution = root(equations, initial_guess, jac=jacobi_matrix, method='hybr')
typer.echo(f"root Solution: {solution.x}")


@app.command()
def compare(
phi_ref: float = typer.Option(0.5, "--phi-ref", "-p", help="Initial guess for φ_ref"),
beta_sple: float = typer.Option(0.5, "--beta-sple", "-b", help="Initial guess for β_sple"),
beta_mix: float = typer.Option(2.0, "--beta-mix", "-m", help="Initial guess for β_mix")
) -> None:
"""
Compare solutions from both fsolve and root methods.

Args:
phi_ref (float): Initial guess for φ_ref parameter
beta_sple (float): Initial guess for β_sple parameter
beta_mix (float): Initial guess for β_mix parameter
"""
initial_guess = (phi_ref, beta_sple, beta_mix)

# Get solutions from both methods
fsolve_solution = fsolve_solve(initial_guess)
root_solution = root(equations, initial_guess, jac=jacobi_matrix, method='hybr')

# Print results
typer.echo("Comparison of solutions:")
typer.echo(f"fsolve Solution: {fsolve_solution}")
typer.echo(f"root Solution: {root_solution.x}")


if __name__ == "__main__":
app()
Loading
Loading