Last active
December 19, 2024 09:18
-
-
Save jmarrec/784825772b351b7cf564b734daa9641a to your computer and use it in GitHub Desktop.
Creating geometry much faster In OpenStudio: it's a foot gun, use at your own risk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "0a31689d-ff0a-47e0-bf1a-fd990a101ca8", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import openstudio\n", | |
"import geomeffibem" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "efccb7e9-a4ae-4dce-b39b-9aa215e598fa", | |
"metadata": {}, | |
"source": [ | |
"# Create dummy surfaces" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 88, | |
"id": "9763eb6d-2d88-475e-929d-f1afa5cccf33", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"N_SURFACES = 10000\n", | |
"WIDTH = 10.0\n", | |
"surfaces = [\n", | |
" geomeffibem.Surface.Floor(\n", | |
" min_x=WIDTH * i,\n", | |
" max_x=WIDTH * (i+1),\n", | |
" min_y=0.0,\n", | |
" max_y=10.0,\n", | |
" z=0.0,\n", | |
" ) for i in range(N_SURFACES)\n", | |
"]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "bdf3c537-2158-433d-97d6-39a84468ac84", | |
"metadata": {}, | |
"source": [ | |
"# OpenStudio: create surfaces from Point3d" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 89, | |
"id": "0bb41bfc-163b-4b31-8215-da12d230f066", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 1min 17s, sys: 55.5 ms, total: 1min 17s\n", | |
"Wall time: 1min 17s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"m = openstudio.model.Model()\n", | |
"for gs in surfaces:\n", | |
" s = openstudio.model.Surface(gs.to_Point3dVector(), m)\n", | |
"assert len(m.getSurfaces()) == len(surfaces)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 90, | |
"id": "0f992b4d-7d78-4049-afe9-758467a3d179", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"OS:Surface,\n", | |
" {d6fa9c05-d232-49f8-914a-c31df02fa47f}, !- Handle\n", | |
" Surface 999, !- Name\n", | |
" Floor, !- Surface Type\n", | |
" , !- Construction Name\n", | |
" , !- Space Name\n", | |
" Ground, !- Outside Boundary Condition\n", | |
" , !- Outside Boundary Condition Object\n", | |
" NoSun, !- Sun Exposure\n", | |
" NoWind, !- Wind Exposure\n", | |
" , !- View Factor to Ground\n", | |
" , !- Number of Vertices\n", | |
" 9990, 10, 0, !- X,Y,Z Vertex 1 {m}\n", | |
" 9990, 0, 0, !- X,Y,Z Vertex 2 {m}\n", | |
" 9980, 0, 0, !- X,Y,Z Vertex 3 {m}\n", | |
" 9980, 10, 0; !- X,Y,Z Vertex 4 {m}\n", | |
"\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"print(m.getSurfaceByName(\"Surface 999\").get())" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "bf0f13b0-e3e2-41bb-b619-b22b8f3b9ad2", | |
"metadata": {}, | |
"source": [ | |
"# Circumvent PlanarSurface checks" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "1530e0bb-fa47-4bef-aaca-1286e93fb283", | |
"metadata": {}, | |
"source": [ | |
"## Create IdfObject, via extensible groups" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 122, | |
"id": "da2ae046-701b-4876-9b3a-1c1a49a3e6e4", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 5.88 s, sys: 18 ms, total: 5.9 s\n", | |
"Wall time: 5.9 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"m = openstudio.model.Model()\n", | |
"for i, gs in enumerate(surfaces):\n", | |
" idf_s = openstudio.IdfObject(openstudio.IddObjectType(\"OS:Surface\"))\n", | |
" idf_s.setString(1, f\"Surface {i + 1}\") # Name\n", | |
" idf_s.setString(2, \"Floor\") # Surface Type\n", | |
" # idf_s.setString(3, \"\") # Construction\n", | |
" # idf_s.setString(4, \"\") # Space Name\n", | |
" idf_s.setString(5, \"Ground\") # Outside Boundary Condition\n", | |
" # idf_s.setString(6, \"\") # Outside Boundary Condition Object\n", | |
" idf_s.setString(7, \"NoSun\") # Sun Exposure\n", | |
" idf_s.setString(8, \"NoWind\") # Wind Exposure\n", | |
" #idf_s.setString(9, \"\") # View Factor to Ground\n", | |
" #idf_s.setString(10, \"\") # Number of Vertices\n", | |
" for v in gs.to_numpy():\n", | |
" idf_s.pushExtensibleGroup([str(x) for x in v])\n", | |
"\n", | |
" m.addObject(idf_s)\n", | |
"assert len(m.getSurfaces()) == len(surfaces)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 123, | |
"id": "37fddfdd-4117-4089-a913-fafb235cbf82", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"OS:Surface,\n", | |
" {28c72982-84c8-4fda-a72e-398d641a9040}, !- Handle\n", | |
" Surface 999, !- Name\n", | |
" Floor, !- Surface Type\n", | |
" , !- Construction Name\n", | |
" , !- Space Name\n", | |
" Ground, !- Outside Boundary Condition\n", | |
" , !- Outside Boundary Condition Object\n", | |
" NoSun, !- Sun Exposure\n", | |
" NoWind, !- Wind Exposure\n", | |
" , !- View Factor to Ground\n", | |
" , !- Number of Vertices\n", | |
" 9990.0, 10.0, 0.0, !- X,Y,Z Vertex 1 {m}\n", | |
" 9990.0, 0.0, 0.0, !- X,Y,Z Vertex 2 {m}\n", | |
" 9980.0, 0.0, 0.0, !- X,Y,Z Vertex 3 {m}\n", | |
" 9980.0, 10.0, 0.0; !- X,Y,Z Vertex 4 {m}\n", | |
"\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"print(m.getSurfaceByName(\"Surface 999\").get())" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"id": "dba4e937-8a52-44b4-af3c-ae5327fc5d8f", | |
"metadata": {}, | |
"source": [ | |
"## Create text, then IdfObject load it" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 95, | |
"id": "357f265a-568e-4d84-8cd9-1bc22586f607", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'88bab602-f50e-4b5a-9221-5786d8f6fd93'" | |
] | |
}, | |
"execution_count": 95, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"import uuid\n", | |
"\n", | |
"str(uuid.uuid4())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 124, | |
"id": "cd2e144a-c643-4fb9-a7eb-78604d789ecd", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 6.29 s, sys: 48.2 ms, total: 6.34 s\n", | |
"Wall time: 6.28 s\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"m = openstudio.model.Model()\n", | |
"for i, gs in enumerate(surfaces):\n", | |
" surface_text = f\"\"\"OS:Surface,\n", | |
" {{{str(uuid.uuid4())}}}, !- Handle\n", | |
" Surface {i + 1}, !- Name\n", | |
" Floor, !- Surface Type\n", | |
" , !- Construction Name\n", | |
" , !- Space Name\n", | |
" Ground, !- Outside Boundary Condition\n", | |
" , !- Outside Boundary Condition Object\n", | |
" NoSun, !- Sun Exposure\n", | |
" NoWind, !- Wind Exposure\n", | |
" , !- View Factor to Ground\n", | |
" , !- Number of Vertices\n", | |
" \"\"\"\n", | |
" \n", | |
" numpy_vertices = gs.to_numpy()\n", | |
" n = numpy_vertices.shape[0]\n", | |
" for j, v in enumerate(numpy_vertices):\n", | |
" coords = \", \".join([str(x) for x in v])\n", | |
" sep = ';' if j == n - 1 else ','\n", | |
" surface_text += f\" {coords}{sep} !- X,Y,Z Vertex {j+1} {{m}}\\n\"\n", | |
" \n", | |
" idf_s = openstudio.IdfObject.load(surface_text).get()\n", | |
" m.addObject(idf_s)\n", | |
"assert len(m.getSurfaces()) == len(surfaces)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 125, | |
"id": "f324f4f6-fb48-4147-a066-4a8ee01b5b77", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"OS:Surface,\n", | |
" {dfd1b30a-cde5-43b0-a1f2-47b4f6c4ca21}, !- Handle\n", | |
" Surface 999, !- Name\n", | |
" Floor, !- Surface Type\n", | |
" , !- Construction Name\n", | |
" , !- Space Name\n", | |
" Ground, !- Outside Boundary Condition\n", | |
" , !- Outside Boundary Condition Object\n", | |
" NoSun, !- Sun Exposure\n", | |
" NoWind, !- Wind Exposure\n", | |
" , !- View Factor to Ground\n", | |
" , !- Number of Vertices\n", | |
" 9990.0, 10.0, 0.0, !- X,Y,Z Vertex 1 {m}\n", | |
" 9990.0, 0.0, 0.0, !- X,Y,Z Vertex 2 {m}\n", | |
" 9980.0, 0.0, 0.0, !- X,Y,Z Vertex 3 {m}\n", | |
" 9980.0, 10.0, 0.0; !- X,Y,Z Vertex 4 {m}\n", | |
"\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"print(m.getSurfaceByName(\"Surface 999\").get())" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.12.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment