|
1 | 1 | Geometry Upload and Import
|
2 | 2 | ==========================
|
3 | 3 |
|
4 |
| -To Do: |
| 4 | +All simulations in the SimScale platform are built on top of a geometry model, and physics concepts |
| 5 | +such as material models, boundary conditions and interactions are assigned to geometrical features |
| 6 | +such as volumes and faces. |
5 | 7 |
|
6 |
| -* Explanation of the geometry import process |
7 |
| -* How to create an storage |
8 |
| -* How to upload a geometry |
9 |
| -* How to import a geometry to the project |
| 8 | +The geometrical model is defined via a CAD file. Some of the CAD formats supported in the platform |
| 9 | +include: |
| 10 | + |
| 11 | +* Parasolid |
| 12 | +* Solidworks |
| 13 | +* Inventor |
| 14 | +* Rinho |
| 15 | +* CATIA |
| 16 | +* ACIS |
| 17 | +* STEP |
| 18 | +* IGES |
| 19 | +* STL |
| 20 | + |
| 21 | +For a CAD geometry to be available for simulation use via the SDK, a two-step process |
| 22 | +is performed: |
| 23 | + |
| 24 | +1. Upload the CAD file to the platform storage |
| 25 | +2. Import the stored file into the project |
| 26 | + |
| 27 | +After the geometry is imported to the project, it can be used to build a simulation on top of it. |
| 28 | + |
| 29 | + |
| 30 | +Uploading the CAD File |
| 31 | +---------------------- |
| 32 | + |
| 33 | +In order to upload a CAD file to the platform, a ``storage`` is first created, by making use of |
| 34 | +the ``StorageApi`` module: |
| 35 | + |
| 36 | + |
| 37 | +.. code-block:: python |
| 38 | +
|
| 39 | + import simscale_sdk as sim |
| 40 | +
|
| 41 | + storage_api = sim.StorageApi(api_client) |
| 42 | +
|
| 43 | + storage = storage_api.create_storage() |
| 44 | +
|
| 45 | +
|
| 46 | +Then we can read the CAD file stored locally and upload it with a ``PUT`` request. The ``URL`` |
| 47 | +for the request is available in the ``storage`` object: |
| 48 | + |
| 49 | + |
| 50 | +.. code-block:: python |
| 51 | +
|
| 52 | + with open("cad_file.x_t", "rb") as file: |
| 53 | + |
| 54 | + api_client.rest_client.PUT( |
| 55 | + url=storage.url, |
| 56 | + headers={ |
| 57 | + "Content-Type": "application/octet-stream" |
| 58 | + }, |
| 59 | + body=file.read() |
| 60 | + ) |
| 61 | +
|
| 62 | +
|
| 63 | +After the request is completed, the file is stored in SimScale's cloud and is ready to |
| 64 | +be imported into a simulation project. For instance, the storage is identified by an ``UUID``: |
| 65 | + |
| 66 | + |
| 67 | +.. code-block:: python |
| 68 | +
|
| 69 | + storage_id = storage.storage_id |
| 70 | +
|
| 71 | + print(f"{storage_id=}") |
| 72 | +
|
| 73 | +
|
| 74 | +Importing the Geometry into the Project |
| 75 | +--------------------------------------- |
| 76 | + |
| 77 | +In order to deal with geometry data and the related operations, the SDK offers the |
| 78 | +``GeometryImportRequest`` object and the ``GeometryImportsApi`` module. We first define the |
| 79 | +relevant data for the CAD file, because the platform still doesn't know about the format, |
| 80 | +name, import options, etc., then use the ``GeometryImportsApi.import_geometry`` function: |
| 81 | + |
| 82 | + |
| 83 | +.. code-block:: python |
| 84 | +
|
| 85 | + import simscale_sdk as sim |
| 86 | +
|
| 87 | + geometry_import_api = sim.GeometryImportsApi(api_client) |
| 88 | +
|
| 89 | + geometry_import_req = sim.GeometryImportRequest( |
| 90 | + name="Geometry 1", |
| 91 | + location=sim.GeometryImportRequestLocation(storage_id), |
| 92 | + format="PARASOLID", |
| 93 | + input_unit="m", |
| 94 | + options=sim.GeometryImportRequestOptions( |
| 95 | + facet_split=False, |
| 96 | + sewing=False, |
| 97 | + improve=True, |
| 98 | + optimize_for_lbm_solver=False |
| 99 | + ), |
| 100 | + ) |
| 101 | +
|
| 102 | + geometry_import = geometry_import_api.import_geometry(project_id, geometry_import_req) |
| 103 | +
|
| 104 | + geometry_import_id = geometry_import.geometry_import_id |
| 105 | +
|
| 106 | +
|
| 107 | +The ``GeometryImportsApi.import_geometry`` method takes some time to complete its work, and is a |
| 108 | +non-blocking call, because the action happens in the platform. In order to sync our code |
| 109 | +with the execution of the task, we create a loop to check the status of the operation at a given |
| 110 | +frequency. |
| 111 | + |
| 112 | + |
| 113 | +.. code-block:: python |
| 114 | +
|
| 115 | + import time |
| 116 | +
|
| 117 | + while geometry_import.status not in ("FINISHED", "CANCELED", "FAILED"): |
| 118 | +
|
| 119 | + geometry_import = geometry_import_api.get_geometry_import(project_id, geometry_import_id) |
| 120 | +
|
| 121 | + time.sleep(10) |
| 122 | +
|
| 123 | +
|
| 124 | +An improved version of this snippet also adds a time-out check: |
| 125 | + |
| 126 | + |
| 127 | +.. code-block:: python |
| 128 | +
|
| 129 | + import time |
| 130 | +
|
| 131 | + GEOMETRY_IMPORT_TIMEOUT = 900 |
| 132 | +
|
| 133 | + import_start = time.time() |
| 134 | +
|
| 135 | + while geometry_import.status not in ("FINISHED", "CANCELED", "FAILED"): |
| 136 | +
|
| 137 | + if time.time() > import_start + GEOMETRY_IMPORT_TIMEOUT: |
| 138 | + raise TimeoutError() |
| 139 | +
|
| 140 | + geometry_import = geometry_import_api.get_geometry_import(project_id, geometry_import_id) |
| 141 | +
|
| 142 | + time.sleep(10) |
| 143 | +
|
| 144 | +
|
| 145 | +Then we can process the result of the operation, such as getting the id for the imported geometry: |
| 146 | + |
| 147 | + |
| 148 | +.. code-block:: python |
| 149 | +
|
| 150 | + if geometry_import.status != "FINISHED": |
| 151 | + raise Error("Geometry import operation was canceled or failed.") |
| 152 | +
|
| 153 | + geometry_id = geometry_import.geometry_id |
| 154 | +
|
| 155 | + print(f"{geometry_id=}") |
| 156 | +
|
| 157 | +
|
| 158 | +This is a common pattern that we will encounter on non-blocking operations that are launched |
| 159 | +with the API, but that we need to sync with because the results are to be used in suqsequent |
| 160 | +operations. Such cases would include mesh computation, simulation run execution, etc. |
| 161 | + |
| 162 | +Also, this loop is a great opportunity for an async execution break point. If you are running |
| 163 | +multiple such operations in a parallel asyncio loop, instead of waiting 10 seconds on a blocking |
| 164 | +call, you can mark the hypervisor to switch tasks at this point. For instance, take a look at the |
| 165 | +following snippet: |
| 166 | + |
| 167 | + |
| 168 | +.. code-block:: python |
| 169 | +
|
| 170 | + import asyncio |
| 171 | +
|
| 172 | + async def async_import_geometry(geoemetry_import_req, project_id): |
| 173 | +
|
| 174 | + # Do the preparation tasks and launch the import |
| 175 | +
|
| 176 | + while geometry_import.status not in ("FINISHED", "CANCELED", "FAILED"): |
| 177 | +
|
| 178 | + if time.time() > import_start + GEOMETRY_IMPORT_TIMEOUT: |
| 179 | + raise TimeoutError() |
| 180 | +
|
| 181 | + geometry_import = geometry_import_api.get_geometry_import(project_id, geometry_import_id) |
| 182 | +
|
| 183 | + await asyncio.sleep(10) |
| 184 | +
|
| 185 | + return geometry_import.geometry_id |
0 commit comments