# Entropyk WebAssembly Bindings

WebAssembly bindings for the [Entropyk](https://github.com/entropyk/entropyk) thermodynamic simulation library.

## Features

- **Browser-native execution**: Run thermodynamic simulations directly in the browser
- **TabularBackend**: Pre-computed fluid tables for fast property lookups (100x faster than direct EOS calls)
- **Zero server dependency**: No backend required - runs entirely client-side
- **Type-safe**: Full TypeScript definitions included
- **JSON serialization**: All results are JSON-serializable for easy integration

## Installation

```bash
npm install @entropyk/wasm
```

## Quick Start

```javascript
import init, { 
    WasmSystem, 
    WasmCompressor,
    WasmCondenser,
    WasmEvaporator,
    WasmExpansionValve,
    WasmFallbackConfig 
} from '@entropyk/wasm';

// Initialize the WASM module
await init();

// Create components
const compressor = new WasmCompressor("R134a");
const condenser = new WasmCondenser("R134a", 1000.0);
const evaporator = new WasmEvaporator("R134a", 800.0);
const valve = new WasmExpansionValve("R134a");

// Create system
const system = new WasmSystem();

// Configure solver
const config = new WasmFallbackConfig();
config.timeout_ms(1000);

// Solve
const result = system.solve(config);

console.log(result.toJson());
// {
//   "converged": true,
//   "iterations": 12,
//   "final_residual": 1e-8,
//   "solve_time_ms": 45
// }
```

## API Reference

### Core Types

- `WasmPressure` - Pressure in Pascals or bar
- `WasmTemperature` - Temperature in Kelvin or Celsius
- `WasmEnthalpy` - Enthalpy in J/kg or kJ/kg
- `WasmMassFlow` - Mass flow rate in kg/s

### Components

- `WasmCompressor` - AHRI 540 compressor model
- `WasmCondenser` - Heat rejection heat exchanger
- `WasmEvaporator` - Heat absorption heat exchanger
- `WasmExpansionValve` - Isenthalpic expansion device
- `WasmEconomizer` - Internal heat exchanger

### Solver

- `WasmSystem` - Thermodynamic system container
- `WasmNewtonConfig` - Newton-Raphson solver configuration
- `WasmPicardConfig` - Sequential substitution solver configuration
- `WasmFallbackConfig` - Intelligent fallback solver configuration
- `WasmConvergedState` - Solver result

## Build Requirements

- Rust 1.70+
- wasm-pack: `cargo install wasm-pack`
- wasm32 target: `rustup target add wasm32-unknown-unknown`

## Building from Source

```bash
# Clone the repository
git clone https://github.com/entropyk/entropyk.git
cd entropyk/bindings/wasm

# Build for browsers
npm run build

# Build for Node.js
npm run build:node

# Run tests
npm test
```

## Performance

| Operation | Target | Typical |
|-----------|--------|---------|
| Simple cycle solve | < 100ms | 30-50ms |
| Property query | < 1μs | ~0.5μs |
| Cold start | < 500ms | 200-300ms |

## Limitations

- **CoolProp unavailable**: The WASM build uses TabularBackend with pre-computed tables. CoolProp C++ cannot compile to WebAssembly.
- **Limited fluid library**: By default, only R134a is embedded. Additional fluids can be loaded from JSON tables.

## Loading Custom Fluid Tables

```javascript
import { load_fluid_table } from '@entropyk/wasm';

// Load a custom fluid table (generated from the entropyk CLI)
const r410aTable = await fetch('/path/to/r410a.json').then(r => r.text());
await load_fluid_table(r410aTable);
```

## Browser Compatibility

- Chrome/Edge 80+
- Firefox 75+
- Safari 14+
- Node.js 14+

## License

MIT OR Apache-2.0

## Links

- [Documentation](https://docs.rs/entropyk)
- [Repository](https://github.com/entropyk/entropyk)
- [npm Package](https://www.npmjs.com/package/@entropyk/wasm)
