|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "558376fd-674a-424c-8885-65a872acfd95", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# PyFluent and PyMechanical out of PyWorkbench session" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "id": "014c5732-9f2b-423d-81ed-8e70928b2e47", |
| 15 | + "metadata": { |
| 16 | + "tags": [] |
| 17 | + }, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import os\n", |
| 21 | + "import json\n", |
| 22 | + "from ansys.workbench.core.launch_workbench import launch_workbench\n", |
| 23 | + "import ansys.fluent.core as pyfluent\n", |
| 24 | + "from ansys.mechanical.core import launch_mechanical" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "code", |
| 29 | + "execution_count": null, |
| 30 | + "id": "705a3b24-b51d-4724-8a17-f7307873197c", |
| 31 | + "metadata": { |
| 32 | + "tags": [] |
| 33 | + }, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "# launch Workbench on the local machine\n", |
| 37 | + "wb = launch_workbench(client_workdir='C:/Users/fli/demo')" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "id": "d02aea0f-e99b-434b-b3d1-c60a87b092ea", |
| 44 | + "metadata": {}, |
| 45 | + "outputs": [], |
| 46 | + "source": [ |
| 47 | + "# upload-download-file round trip\n", |
| 48 | + "wb.upload_file('sample.png')\n", |
| 49 | + "wb.run_script_string(\"\"\"import os\n", |
| 50 | + "wdir = GetServerWorkingDirectory()\n", |
| 51 | + "if os.path.exists(os.path.join(wdir, 'renamed.png')):\n", |
| 52 | + " os.remove(os.path.join(wdir, 'renamed.png'))\n", |
| 53 | + "os.rename(os.path.join(wdir, 'sample.png'), os.path.join(wdir, 'renamed.png'))\n", |
| 54 | + "\"\"\")\n", |
| 55 | + "wb.download_file('renamed.png')\n", |
| 56 | + "are_same = open('sample.png', 'rb').read() == open('renamed.png', 'rb').read()\n", |
| 57 | + "print('are two files the same? ' + str(are_same))" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "id": "b6af4888-7482-4c40-96da-c959b4147b3a", |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [], |
| 66 | + "source": [ |
| 67 | + "# create a Fluent system and return its name\n", |
| 68 | + "fluent_sys_name = wb.run_script_string('import json\\nwb_script_result=json.dumps(GetTemplate(TemplateName=\\\"FLUENT\\\").CreateSystem().Name)')" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "code", |
| 73 | + "execution_count": null, |
| 74 | + "id": "e88ab850-d840-4919-954c-eacd29a699fd", |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [], |
| 77 | + "source": [ |
| 78 | + "# start PyFluent server on the system, then create a PyFluent client session\n", |
| 79 | + "server_info_file=wb.start_pyfluent(system_name=fluent_sys_name)\n", |
| 80 | + "fluent=pyfluent.connect_to_fluent(server_info_filepath=server_info_file)" |
| 81 | + ] |
| 82 | + }, |
| 83 | + { |
| 84 | + "cell_type": "code", |
| 85 | + "execution_count": null, |
| 86 | + "id": "221ad401-82d1-480d-8910-da0dd34b5a2c", |
| 87 | + "metadata": { |
| 88 | + "tags": [] |
| 89 | + }, |
| 90 | + "outputs": [], |
| 91 | + "source": [ |
| 92 | + "# basic status queries on the PyFluent session\n", |
| 93 | + "print('server is serving? ' + str(fluent.health_check_service.is_serving))\n", |
| 94 | + "print('server version: ' + fluent.get_fluent_version())" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "code", |
| 99 | + "execution_count": null, |
| 100 | + "id": "6a3fd1a1-d261-45e0-9c9f-3f043d00aea6", |
| 101 | + "metadata": {}, |
| 102 | + "outputs": [], |
| 103 | + "source": [ |
| 104 | + "# upload an example Fluent case file, load it into the Fluent session\n", |
| 105 | + "wb.upload_file_from_example_repo(\"elbow.cas.h5\", \"elbow_case_file\")\n", |
| 106 | + "setup_state=wb.run_script_string(f\"\"\"import os\n", |
| 107 | + "sys=GetSystem(Name='{fluent_sys_name}')\n", |
| 108 | + "dir=GetServerWorkingDirectory()\n", |
| 109 | + "sys.GetContainer(ComponentName=\"Setup\").Import(FilePath=os.path.join(dir, 'elbow.cas.h5'), FileType='CffCase')\n", |
| 110 | + "setup=sys.GetComponent(Name=\"Setup\")\n", |
| 111 | + "setup.Refresh()\n", |
| 112 | + "wb_script_result = json.dumps(GetComponentState(Component=setup).State.ToString())\n", |
| 113 | + "\"\"\")\n", |
| 114 | + "print('Fluent Setup component is ' + setup_state)\n", |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "id": "a94f8a4e-4995-4d48-8966-8734c6c9d4bc", |
| 121 | + "metadata": {}, |
| 122 | + "outputs": [], |
| 123 | + "source": [ |
| 124 | + "# create a Mechanical system on Workbench, then load geometry\n", |
| 125 | + "wb.upload_file_from_example_repo(\"2pipes.agdb\", \"2pipes\")\n", |
| 126 | + "ss_system_name = wb.run_script_file('load_geometry.wbjn')" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "code", |
| 131 | + "execution_count": null, |
| 132 | + "id": "110c8edc-e890-422b-8323-3dddb2af346c", |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [], |
| 135 | + "source": [ |
| 136 | + "# start PyMechanical server on the system, then create a PyMechanical client session\n", |
| 137 | + "#server_port=wb.start_pymechanical(system_name=ss_system_name)\n", |
| 138 | + "mechanical = launch_mechanical(start_instance=False, ip='localhost', port=server_port)" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": null, |
| 144 | + "id": "48320724-37fb-41d4-959e-d9b0437a7bff", |
| 145 | + "metadata": {}, |
| 146 | + "outputs": [], |
| 147 | + "source": [ |
| 148 | + "# perform some queries on the PyMechanical session\n", |
| 149 | + "print(mechanical.version)\n", |
| 150 | + "mechanical.list_files()" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "code", |
| 155 | + "execution_count": null, |
| 156 | + "id": "6dfe8b49-c81b-4718-851e-e1f6f0f30a12", |
| 157 | + "metadata": { |
| 158 | + "tags": [] |
| 159 | + }, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "# shutdown the Workbench service\n", |
| 163 | + "wb.exit()" |
| 164 | + ] |
| 165 | + } |
| 166 | + ], |
| 167 | + "metadata": { |
| 168 | + "kernelspec": { |
| 169 | + "display_name": "Python 3 (ipykernel)", |
| 170 | + "language": "python", |
| 171 | + "name": "python3" |
| 172 | + }, |
| 173 | + "language_info": { |
| 174 | + "codemirror_mode": { |
| 175 | + "name": "ipython", |
| 176 | + "version": 3 |
| 177 | + }, |
| 178 | + "file_extension": ".py", |
| 179 | + "mimetype": "text/x-python", |
| 180 | + "name": "python", |
| 181 | + "nbconvert_exporter": "python", |
| 182 | + "pygments_lexer": "ipython3", |
| 183 | + "version": "3.9.4" |
| 184 | + } |
| 185 | + }, |
| 186 | + "nbformat": 4, |
| 187 | + "nbformat_minor": 5 |
| 188 | +} |
0 commit comments