This repository contains the backend API server for Electrisim - an open-source web-based application for comprehensive power system modeling, simulation, and analysis. The backend provides REST API endpoints for power system calculations and simulations using industry-standard libraries.
🌐 Online Application: app.electrisim.com
📁 Frontend Repository: Frontend Code
The Electrisim backend provides computational engines for:
- Power Flow Analysis
- Optimal Power Flow (OPF)
- Short-Circuit Analysis
- Contingency Analysis
- Controller Simulation
- Time Series Simulation
- Framework: Flask (Python web framework)
- Simulation Engines:
- pandapower - Primary power system analysis library
- OpenDSS - Alternative simulation engine via py-dss-interface
- Scientific Computing: NumPy, SciPy, pandas
- Web Server: Gunicorn (production WSGI server)
- Testing: pytest
- Deployment: Heroku/Render ready with Procfile
├── app.py # Main Flask application and API routes
├── pandapower_electrisim.py # pandapower simulation engine wrapper
├── opendss_electrisim.py # OpenDSS simulation engine wrapper
├── requirements.txt # Python dependencies
├── Procfile # Heroku/Render deployment configuration
├── runtime.txt # Python version specification
└── test_*.py # Test suites for various components
Before deploying the Electrisim backend, ensure you have:
- Python 3.9.18 (specified in runtime.txt)
- pip (Python package manager)
- Git (version control)
- Virtual Environment tools (venv or virtualenv)
git clone <repository-url>
cd appElectrisimBackend# Create virtual environment
python -m venv .venv
# Activate virtual environment
# On Windows:
.venv\Scripts\activate
# On macOS/Linux:
source .venv/bin/activatepip install -r requirements.txt# Set Flask environment variables
export FLASK_APP=app.py
export FLASK_ENV=development
# Run the application
flask run
# Or run directly with Python
python app.pyThe server will start on http://localhost:5000 by default.
# Test basic connectivity
curl http://localhost:5000/
# Test API endpoint (requires JSON data)
curl -X POST http://localhost:5000/ \
-H "Content-Type: application/json" \
-d '{"test": "data"}'- Install Heroku CLI and login:
heroku login- Create Heroku application:
heroku create your-electrisim-backend- Deploy to Heroku:
git add .
git commit -m "Deploy to Heroku"
git push heroku main- Configure environment variables:
heroku config:set FLASK_ENV=production
heroku config:set FLASK_APP=app.py-
Connect Repository: Link your GitHub repository to Render
-
Configure Build Settings:
- Build Command:
pip install -r requirements.txt - Start Command:
gunicorn app:app - Environment: Python 3.9.18
- Build Command:
-
Deploy: Render will automatically deploy from your repository
- Install system dependencies:
# Ubuntu/Debian
sudo apt update
sudo apt install python3.9 python3-pip python3-venv nginx
# CentOS/RHEL
sudo yum install python39 python3-pip nginx- Setup application:
# Clone and setup
git clone <repository-url> /opt/electrisim-backend
cd /opt/electrisim-backend
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt- Create systemd service (
/etc/systemd/system/electrisim-backend.service):
[Unit]
Description=Electrisim Backend API
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/opt/electrisim-backend
Environment=PATH=/opt/electrisim-backend/.venv/bin
ExecStart=/opt/electrisim-backend/.venv/bin/gunicorn --workers 3 --bind 127.0.0.1:5000 app:app
Restart=always
[Install]
WantedBy=multi-user.target- Configure Nginx reverse proxy:
server {
listen 80;
server_name your-backend-domain.com;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}- Start services:
sudo systemctl enable electrisim-backend
sudo systemctl start electrisim-backend
sudo systemctl reload nginxFor production deployments, configure SSL:
# Using Certbot (Let's Encrypt)
sudo certbot --nginx -d your-backend-domain.comThe backend is configured to accept requests from:
- Development:
localhost:5500,localhost:5501,127.0.0.1:5500,127.0.0.1:5501 - Production:
app.electrisim.com,www.electrisim.com,electrisim.com
Update CORS origins in app.py if deploying to different domains.
# Production settings
export FLASK_ENV=production
export FLASK_APP=app.py
# Optional: Database configuration (if using database)
export DATABASE_URL=your_database_url
# Optional: API keys for external services
export API_KEY=your_api_key- Development:
http://localhost:5000 - Production:
https://your-backend-domain.com
Description: Health check endpoint
Response: "Please send data to backend"
Description: Main simulation endpoint
Content-Type: application/json
Request Body Structure:
{
"simulationType": "powerflow|shortcircuit|contingency|timeseries",
"elements": {
"element_id": {
"typ": "element_type",
"name": "element_name",
"parameters": {...}
}
},
"settings": {
"solver": "pandapower|opendss",
"tolerance": 1e-6,
"max_iteration": 100
}
}Response Structure:
{
"success": true,
"results": {
"busbars": [...],
"lines": [...],
"transformers": [...],
"generators": [...],
"loads": [...]
},
"convergence": true,
"iterations": 5
}-
Power Flow Analysis
- Steady-state AC power flow
- Voltage and power calculations
- Loss analysis
-
Short Circuit Analysis
- Three-phase fault calculations
- Line-to-ground faults
- Protection coordination
-
Contingency Analysis
- N-1 security assessment
- Critical element identification
- System stability evaluation
-
Time Series Simulation
- Load profile analysis
- Renewable integration studies
- Dynamic system behavior
# Run all tests
pytest
# Run specific test files
pytest test_pandapower.py
pytest test_opendss.py
# Run with coverage
pytest --cov=. --cov-report=htmltest_numeric_conversion_fix.py- Data validation teststest_transformer_parallel_fix.py- Transformer modeling teststest_busbar_count.py- Bus system validationtest_voltage_and_generator_fixes.py- Voltage calculation teststest_comprehensive_fixes.py- Integration tests
- Flask 2.2.2 - Web framework
- Flask-CORS 3.0.10 - Cross-origin resource sharing
- pandapower 2.14.11 - Power system analysis
- py-dss-interface 2.0.4 - OpenDSS integration
- numpy 1.23 - Numerical computing
- pandas 2.1.4 - Data manipulation
- scipy 1.11.4 - Scientific computing
- gunicorn 20.1.0 - WSGI HTTP server
- pytest 7.4.3 - Testing framework
-
Import Errors:
# Ensure all dependencies are installed pip install -r requirements.txt # Check Python version python --version # Should be 3.9.18
-
CORS Issues:
- Verify frontend URL is in CORS origins list in
app.py - Check that requests include proper headers
- Verify frontend URL is in CORS origins list in
-
Simulation Errors:
- Validate input data format
- Check pandapower/OpenDSS installation
- Review error logs for specific issues
-
Performance Issues:
- Consider increasing Gunicorn workers
- Monitor memory usage for large networks
- Implement caching for repetitive calculations
-
Deployment Issues:
# Check logs heroku logs --tail # For Heroku # Verify environment variables heroku config # For Heroku # Test locally first gunicorn app:app --bind 0.0.0.0:5000
appElectrisimBackend/
├── app.py # Flask application entry point
├── pandapower_electrisim.py # pandapower simulation wrapper
├── opendss_electrisim.py # OpenDSS simulation wrapper
├── requirements.txt # Python dependencies
├── runtime.txt # Python version for deployment
├── Procfile # Process configuration for cloud deployment
├── test_*.py # Test suites
└── README.md # This file
- Create feature branch:
git checkout -b feature/new-analysis-type-
Implement changes:
- Add new simulation functions to appropriate engine file
- Update API routes in
app.py - Add corresponding tests
-
Test thoroughly:
pytest test_new_feature.py- Submit pull request with comprehensive description
- Memory Usage: Large power systems may require significant RAM
- CPU Usage: Complex simulations are computationally intensive
- Scaling: Consider horizontal scaling for high-traffic deployments
- Caching: Implement Redis/Memcached for repeated calculations
- CORS Configuration: Properly configured for known domains
- Input Validation: Implement comprehensive input sanitization
- Rate Limiting: Consider adding rate limiting for production
- HTTPS: Always use HTTPS in production environments
We welcome contributions! Please:
- Fork the repository
- Create a feature branch
- Make changes with tests
- Submit a pull request
- Follow Python PEP 8 style guidelines
- Add unit tests for new features
- Update documentation for API changes
- Ensure backward compatibility
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
- Documentation: GitHub Wiki
- Issues: GitHub Issues
- API Questions: Use the
electrisim-apitag on Stack Overflow
🚧 Upcoming Features:
- integrating OpenDSS for further functionality-
- Enhanced caching mechanisms
- Machine learning integration
- Distributed computing support
- Integrating AI
Electrisim Backend - Powering electrical engineering calculations with robust, scalable APIs.
For frontend repository and user interface, visit: Frontend Repository