cubiceos

package module
v1.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 5 Imported by: 0

README

cubiceos

CI Go Reference Go Report Card

Solvers and tools for cubic Equations of State (EOS) in Go.

Supported EOS:

  • Van der Waals (vdW)
  • Redlich–Kwong (RK)
  • Soave–Redlich–Kwong (SRK)
  • Peng–Robinson (PR)

This repository contains three parts:

  1. Library: github.com/rickykimani/cubiceos — reusable Go package for EOS calculations
  2. TUI: an interactive terminal app to explore EOS results quickly
  3. Web UI: a simple browser app for solving and visualizing results

Table of Contents


Part I — Library (github.com/rickykimani/cubiceos)

What is a cubic EOS?
  • Polynomial equations that are cubic in molar volume are the simplest equations capable of representing the 𝑃𝑉𝑇 behaviour of both liquids and vapours for a wide range of temperatures, pressures, and molar volumes.
\begin{align*}
P &= \frac{RT}{V-b} - \frac{a(T)}{(V + \varepsilon b)(V + \sigma b)}\\
b &= \Omega\frac{R T_c}{P_c}\\
a(T) &= \Psi\frac{\alpha(T_r, \omega) R^2 {T_c}^2}{P_c}
\end{align*}

and the corresponding cubic in V:

\begin{align*}
0 &= V^{3} 
+ \left[\;(\varepsilon + \sigma - 1)b - \frac{RT}{P}\;\right]V^{2} \\[6pt]
&\quad + \left\{\,b\left[(\varepsilon\sigma - \varepsilon - \sigma)b - (\varepsilon + \sigma)\frac{RT}{P}\right] + \frac{a(T)}{P}\,\right\}V \\[6pt]
&\quad -\,\varepsilon\sigma\, b^{2}\left(b + \frac{RT}{P}\right) - \frac{a(T)\,b}{P}.
\end{align*}

this describes cubic

Requirements

Install
go get github.com/rickykimani/cubiceos

Usage
package main

import (
	"log"

	"github.com/rickykimani/cubiceos"
)

func main() {
    //n-butane example
	eq := cubiceos.NewRKCfg(
		350,    // T (K)
		9.4573, // P (bar)
		425.1,  // Tc (K)
		37.96,  // Pc (bar)
		83.14,  // R (bar•cm^3/(mol•K))
	)
	res, err := cubiceos.CubicEOS(eq)
	if err != nil {
		log.Fatal(err)
	}
	cubiceos.ResultPrinter(res)
}

API overview
  • Builders:
    • NewvdWCfg(T, P, Tc, Pc, R)
    • NewRKCfg(T, P, Tc, Pc, R)
    • NewSRKCfg(T, P, Tc, Pc, W, R)
    • NewPRCfg(T, P, Tc, Pc, W, R)
  • Call CubicEOS(cfg) to solve and return three roots (possibly complex).
  • Print ResultPrinter(roots)

Interpreting results

CubicEOS returns three roots. Physical molar volumes are the real, positive roots:

  • One positive root → single phase.
  • Two positive roots (with one unstable in-between) → phase split (liquid/vapor).
  • At critical conditions, the three real roots can coalesce.

For examples, see example/main.go:

go run ./example

Part II — Terminal UI (TUI)

Install
go install github.com/rickykimani/cubiceos/cmd/eos-cli@latest

Run
eos-cli

Controls & input
  • Navigate: ↑/↓ or j/k
  • Select: Enter
  • Back: Esc
  • Quit: q or Ctrl+C Controls
  • Inputs: numeric values for T, P, Tc, Pc, R, and omega (for SRK/PR). Input

Results view
  • Displays which EOS was used and the computed roots. Result

Demo

Demo


Part III — Web UI

Features
  • Per-field unit selectors (T, P, Tc, Pc) with correct SI conversion internally
  • Volume display units (e.g., m³/mol, cm³/mol) applied to results
  • a(T) and b displayed with unit labels (a(T) in Punit·(Vunit)², b in Vunit)
  • Clean results view with phase classification badges and error messages
  • Dark mode with a top-right toggle; GitHub link in the header
  • Embedded static assets (works when installed via go install, no external files required)

Run

Quick start (opens browser on a local port):

eos-cli --http

Screenshots

Web Home Web Results


Project layout

  • cubiceos.go — core types and CubicEOS
  • solve.go — general cubic polynomial solver and helpers
  • vdw.go, rk.go, srk.go, pr.go — EOS implementations and config builders
  • cmd/ — interactive terminal UI and web UI
  • example/ — minimal library usage example

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CubicEOS

func CubicEOS(cfg EOSCfg) ([3]complex128, error)

CubicEOS solves the cubic equation and returns the volumes

func ResultPrinter

func ResultPrinter(c [3]complex128)

ResultPrinter prints physically meaningfull solutions

func SolveCubic

func SolveCubic(a, b, c, d float64) ([3]complex128, error)

SolveCubic solves ax^3 + bx^2 + cx + d = 0 Returns all 3 roots (possibly complex).

Types

type EOSCfg

type EOSCfg struct {
	Type EOSType
	T    float64 //Absolute temp
	P    float64 //Pressure
	Tc   float64 //Critical temp (Absolute)
	Pc   float64 //Critical pressure
	W    float64 //Acentric factor (SRK and PR only)
	R    float64 //Universal gas constant
}

EOSCfg is a configuration struct for an equation of state

func NewPRCfg

func NewPRCfg(T, P, Tc, Pc, W, R float64) EOSCfg

NewPRCfg creates a configuration for the Peng-Robinson cubic equation of state

func NewRKCfg

func NewRKCfg(T, P, Tc, Pc, R float64) EOSCfg

NewRKCfg creates a configuration for the Redlich-Kwong cubic equation of state

func NewSRKCfg

func NewSRKCfg(T, P, Tc, Pc, W, R float64) EOSCfg

NewSRKCfg creates a configuration for the Soave-Redlich-Kwong cubic equation of state

func NewvdWCfg

func NewvdWCfg(T, P, Tc, Pc, R float64) EOSCfg

NewvdW creates a configuration for the van der Waals cubic equation of state

type EOSType

type EOSType interface {
	Alpha(tr, w float64) float64
	Params() Params
	Name() string
}

EOSType defines what makes up an equation of state

type PR

type PR struct{}

func (PR) Alpha

func (PR) Alpha(tr, w float64) float64

func (PR) Name

func (PR) Name() string

func (PR) Params

func (PR) Params() Params

type Params

type Params struct {
	Sigma   float64
	Epsilon float64
	Omega   float64
	Psi     float64
}

Params represents the substance agnostic variables in any cubic equation of state

type RK

type RK struct{}

func (RK) Alpha

func (RK) Alpha(tr, w float64) float64

func (RK) Name

func (RK) Name() string

func (RK) Params

func (RK) Params() Params

type SRK

type SRK struct{}

func (SRK) Alpha

func (SRK) Alpha(tr, w float64) float64

func (SRK) Name

func (SRK) Name() string

func (SRK) Params

func (SRK) Params() Params

type VdW

type VdW struct{}

func (VdW) Alpha

func (VdW) Alpha(tr, w float64) float64

func (VdW) Name

func (VdW) Name() string

func (VdW) Params

func (VdW) Params() Params

Directories

Path Synopsis
cmd
eos-cli command
web-test command
internal
tui
web
web/components/icon
templui component icon - version: v0.97.0 installed by templui v0.97.0
templui component icon - version: v0.97.0 installed by templui v0.97.0
web/pages
templ: version: v0.3.943
templ: version: v0.3.943
web/utils
templui util templui.go - version: v0.97.0 installed by templui v0.97.0
templui util templui.go - version: v0.97.0 installed by templui v0.97.0

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL