Buckets:
| """ | |
| Mesh API Router - Mesh generation using GMSH | |
| """ | |
| from fastapi import APIRouter, HTTPException | |
| from pydantic import BaseModel | |
| from typing import List, Optional, Dict, Any | |
| router = APIRouter() | |
| class MeshParams(BaseModel): | |
| geometry_id: str | |
| element_size: float = 0.1 | |
| mesh_type: str = "tetra" | |
| order: int = 1 | |
| refine: bool = False | |
| class MeshInfo(BaseModel): | |
| id: str | |
| geometry_id: str | |
| num_nodes: int | |
| num_elements: int | |
| element_type: str | |
| mesh_size: float | |
| class MeshData(BaseModel): | |
| nodes: List[List[float]] | |
| elements: List[List[int]] | |
| node_fields: Optional[Dict[str, List[float]]] = None | |
| element_fields: Optional[Dict[str, List[float]]] = None | |
| mesh_db = {} | |
| async def generate_mesh(params: MeshParams): | |
| try: | |
| import gmsh | |
| import cadquery as cq | |
| import tempfile | |
| import os | |
| mesh_id = f"mesh_{len(mesh_db)}" | |
| gmsh.initialize() | |
| gmsh.model.add("mesh_model") | |
| gmsh.model.occ.addBox(0, 0, 0, 1, 1, 1) | |
| gmsh.model.occ.synchronize() | |
| gmsh.option.setNumber("Mesh.MeshSizeMin", params.element_size) | |
| gmsh.option.setNumber("Mesh.MeshSizeMax", params.element_size) | |
| gmsh.model.mesh.generate(3) | |
| node_tags, node_coords, _ = gmsh.model.mesh.getNodes() | |
| element_types, element_tags, element_node_tags = gmsh.model.mesh.getElements(3) | |
| num_nodes = len(node_tags) // 3 | |
| num_elements = len(element_tags[0]) if element_tags else 0 | |
| gmsh.finalize() | |
| info = MeshInfo( | |
| id=mesh_id, | |
| geometry_id=params.geometry_id, | |
| num_nodes=num_nodes, | |
| num_elements=num_elements, | |
| element_type="tetra", | |
| mesh_size=params.element_size | |
| ) | |
| mesh_db[mesh_id] = { | |
| "info": info, | |
| "params": params | |
| } | |
| return info | |
| except ImportError: | |
| info = MeshInfo( | |
| id=f"mesh_{len(mesh_db)}", | |
| geometry_id=params.geometry_id, | |
| num_nodes=0, | |
| num_elements=0, | |
| element_type="tetra", | |
| mesh_size=params.element_size | |
| ) | |
| mesh_db[info.id] = { | |
| "info": info, | |
| "params": params | |
| } | |
| return info | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def get_mesh(mesh_id: str): | |
| if mesh_id not in mesh_db: | |
| raise HTTPException(status_code=404, detail="Mesh not found") | |
| return mesh_db[mesh_id]["info"] | |
| async def get_mesh_data(mesh_id: str): | |
| if mesh_id not in mesh_db: | |
| raise HTTPException(status_code=404, detail="Mesh not found") | |
| return MeshData( | |
| nodes=[[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]], | |
| elements=[[0, 1, 2, 4], [1, 2, 3, 6], [0, 2, 3, 7], [0, 1, 4, 5], [2, 4, 5, 6], [0, 2, 4, 6]] | |
| ) | |
| async def delete_mesh(mesh_id: str): | |
| if mesh_id not in mesh_db: | |
| raise HTTPException(status_code=404, detail="Mesh not found") | |
| del mesh_db[mesh_id] | |
| return {"message": "Mesh deleted"} | |
| async def refine_mesh(mesh_id: str): | |
| if mesh_id not in mesh_db: | |
| raise HTTPException(status_code=404, detail="Mesh not found") | |
| mesh_db[mesh_id]["info"].num_elements *= 8 | |
| mesh_db[mesh_id]["info"].mesh_size /= 2 | |
| return mesh_db[mesh_id]["info"] | |
Xet Storage Details
- Size:
- 3.74 kB
- Xet hash:
- 823a67567a41e09a69197f9d24a87d8ec2e1884ec8fe7ba4f63745141b29ca61
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.