|
|
|
|
@ -1,9 +1,13 @@
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from typing import List, Optional
|
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, BackgroundTasks, HTTPException, background, status, Query
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
|
|
|
|
from src.aeros_equipment.model import AerosEquipment
|
|
|
|
|
from src.aeros_simulation.model import EafContribution
|
|
|
|
|
from src.auth.service import CurrentUser
|
|
|
|
|
from src.database.core import DbSession
|
|
|
|
|
from src.database.service import CommonParameters
|
|
|
|
|
@ -33,7 +37,7 @@ from .service import (
|
|
|
|
|
get_plant_calc_result
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
from .simulation_save_service import execute_simulation
|
|
|
|
|
from .simulation_save_service import calculate_plant_eaf, execute_simulation
|
|
|
|
|
|
|
|
|
|
from src.aeros_equipment.schema import EquipmentWithCustomParameters
|
|
|
|
|
|
|
|
|
|
@ -77,6 +81,8 @@ async def run_simulations(
|
|
|
|
|
)
|
|
|
|
|
simulation_id = simulation.id
|
|
|
|
|
|
|
|
|
|
#simulation_id = "2e0755bf-8cce-4743-9659-8d9920d556e7"
|
|
|
|
|
|
|
|
|
|
project = await get_project(db_session=db_session)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
@ -85,7 +91,7 @@ async def run_simulations(
|
|
|
|
|
sim_data["projectName"] = project.project_name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##background_tasks.add_task(execute_simulation, db_session=db_session ,simulation_id=simulation_id, sim_data=sim_data)
|
|
|
|
|
# ##background_tasks.add_task(execute_simulation, db_session=db_session ,simulation_id=simulation_id, sim_data=sim_data)
|
|
|
|
|
|
|
|
|
|
results = await update_equipment_for_simulation(
|
|
|
|
|
db_session=db_session, project_name=project.project_name, schematic_name=simulation_in.SchematicName, custom_input=simulation_in.CustomInput
|
|
|
|
|
@ -99,6 +105,8 @@ async def run_simulations(
|
|
|
|
|
db_session=db_session, simulation_id=simulation_id, sim_data=sim_data, is_saved=True, eq_update=results
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
await calculate_plant_eaf(db_session=db_session, simulation_id=simulation_id)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"data": str(simulation_id),
|
|
|
|
|
"status": "success",
|
|
|
|
|
@ -209,6 +217,90 @@ async def get_custom_parameters_controller(db_session: DbSession):
|
|
|
|
|
"message": "Simulation result retrieved successfully",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@router.post("/calculate_eaf_contribution", response_model=StandardResponse[dict])
|
|
|
|
|
async def calculate_contribution(
|
|
|
|
|
db_session: DbSession,
|
|
|
|
|
simulation_in: SimulationInput,
|
|
|
|
|
batch_num: int = Query(0, ge=0)
|
|
|
|
|
):
|
|
|
|
|
"""RUN Simulation"""
|
|
|
|
|
|
|
|
|
|
#simulation_id = "2e0755bf-8cce-4743-9659-8d9920d556e7"
|
|
|
|
|
project = await get_project(db_session=db_session)
|
|
|
|
|
main_edh = 43.00797663527534
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
contribution_results = defaultdict()
|
|
|
|
|
simulations_eq = select(AerosEquipment)
|
|
|
|
|
|
|
|
|
|
eqs = (await db_session.execute(simulations_eq)).scalars().all()
|
|
|
|
|
|
|
|
|
|
batch_size = 100
|
|
|
|
|
start_index = batch_num * batch_size
|
|
|
|
|
end_index = start_index + batch_size
|
|
|
|
|
|
|
|
|
|
if end_index > len(eqs):
|
|
|
|
|
end_index = len(eqs)
|
|
|
|
|
|
|
|
|
|
eqs = eqs[start_index:end_index]
|
|
|
|
|
for eq in eqs:
|
|
|
|
|
simulation = await create_simulation(
|
|
|
|
|
db_session=db_session, simulation_in=simulation_in
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sim_data = simulation_in.model_dump(exclude={"SimulationName"})
|
|
|
|
|
sim_data["HubCnnId"] = str(simulation.id)
|
|
|
|
|
sim_data["projectName"] = project.project_name
|
|
|
|
|
|
|
|
|
|
custom_input = {
|
|
|
|
|
eq.node_name: {
|
|
|
|
|
"mttr": 8760,
|
|
|
|
|
"failure_rate": 0.1,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
results = await update_equipment_for_simulation(
|
|
|
|
|
db_session=db_session, project_name=project.project_name, schematic_name=simulation_in.SchematicName, custom_input=custom_input
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# await update_simulation(
|
|
|
|
|
# db_session=db_session, simulation_id=simulation_id, data={"reliability": results}
|
|
|
|
|
# )
|
|
|
|
|
|
|
|
|
|
await execute_simulation(
|
|
|
|
|
db_session=db_session, simulation_id=simulation.id, sim_data=sim_data, is_saved=True, eq_update=results
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
eaf, edh = await calculate_plant_eaf(db_session=db_session, simulation_id=simulation.id)
|
|
|
|
|
|
|
|
|
|
eaf_contribution = (main_edh - edh)/main_edh if main_edh else 0
|
|
|
|
|
|
|
|
|
|
contribution_results[eq.node_name] = {
|
|
|
|
|
"eaf": eaf,
|
|
|
|
|
"edh": edh,
|
|
|
|
|
"eaf_contribution": eaf_contribution
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eaf_conf = EafContribution(
|
|
|
|
|
location_tag=eq.node_name,
|
|
|
|
|
eaf_contribution=eaf_contribution
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db_session.add(eaf_conf)
|
|
|
|
|
await db_session.commit()
|
|
|
|
|
await db_session.delete(simulation)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"data": contribution_results,
|
|
|
|
|
"status": "success",
|
|
|
|
|
"message": "Simulation created successfully",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @router.get("/status/{simulation_id}", response_model=StandardResponse[None])
|
|
|
|
|
# async def get_simulation_status(simulation_id: str):
|
|
|
|
|
|