Skip to content

Instantly share code, notes, and snippets.

@suvarchal
Created February 20, 2021 22:15
Show Gist options
  • Save suvarchal/caff707b6b17c28a6c4ae75528cc6e07 to your computer and use it in GitHub Desktop.
Save suvarchal/caff707b6b17c28a6c4ae75528cc6e07 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:11:56.975222Z",
"start_time": "2021-02-20T22:11:54.210562Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pyfesom2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:00.877724Z",
"start_time": "2021-02-20T22:11:56.976843Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/home/suvarchal/AWI/fesom2/AO1_MESH/pickle_mesh_py3_fesom2\n",
"The usepickle == True)\n",
"The pickle file for FESOM2 exists.\n",
"The mesh will be loaded from /home/suvarchal/AWI/fesom2/AO1_MESH/pickle_mesh_py3_fesom2\n"
]
}
],
"source": [
"mesh = pyfesom2.load_mesh(\"../../../fesom2/AO1_MESH/\") # load AO1 mesh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# check perf of opt get_no_cyclic "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:00.881742Z",
"start_time": "2021-02-20T22:12:00.879451Z"
}
},
"outputs": [],
"source": [
"from pyfesom2.ut import get_no_cyclic"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:01.062848Z",
"start_time": "2021-02-20T22:12:00.883528Z"
}
},
"outputs": [],
"source": [
"def opt_get_no_cyclic(mesh, elem_no_nan):\n",
" \"\"\"Compute non cyclic elements of the mesh.\"\"\"\n",
" d = mesh.x2[elem_no_nan].max(axis=1) - mesh.x2[elem_no_nan].min(axis=1)\n",
" no_cyclic_elem = np.argwhere(d < 100)\n",
" return no_cyclic_elem.ravel()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:01.075033Z",
"start_time": "2021-02-20T22:12:01.064541Z"
}
},
"outputs": [],
"source": [
"## dont use time it becuse get_no_cyclic in current mster is too slow"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:11.433804Z",
"start_time": "2021-02-20T22:12:01.076350Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 9.41 s, sys: 925 ms, total: 10.3 s\n",
"Wall time: 10.3 s\n"
]
}
],
"source": [
"%%time\n",
"non_cyclic_elem = get_no_cyclic(mesh, mesh.elem)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:13.583381Z",
"start_time": "2021-02-20T22:12:11.435161Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 1.07 s, sys: 1.06 s, total: 2.13 s\n",
"Wall time: 2.15 s\n"
]
}
],
"source": [
"%%time\n",
"opt_non_cyclic_elem = opt_get_no_cyclic(mesh, mesh.elem)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:15.368827Z",
"start_time": "2021-02-20T22:12:13.585587Z"
}
},
"outputs": [],
"source": [
"assert np.all(np.asarray(non_cyclic_elem) == opt_non_cyclic_elem)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## check perf of opt cut_region "
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:15.372929Z",
"start_time": "2021-02-20T22:12:15.370345Z"
}
},
"outputs": [],
"source": [
"from pyfesom2.ut import cut_region"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:15.379036Z",
"start_time": "2021-02-20T22:12:15.374620Z"
}
},
"outputs": [],
"source": [
"def opt_cut_region(mesh, box):\n",
" left, right, down, up = box\n",
" \n",
" selection = (\n",
" (mesh.x2 >= left)\n",
" & (mesh.x2 <= right)\n",
" & (mesh.y2 >= down)\n",
" & (mesh.y2 <= up)\n",
" )\n",
"\n",
" elem_selection = selection[mesh.elem] \n",
" \n",
" no_nan_triangles = np.all(elem_selection, axis=1) \n",
" \n",
" elem_no_nan = mesh.elem[no_nan_triangles]\n",
" \n",
" return elem_no_nan, no_nan_triangles"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:15.385796Z",
"start_time": "2021-02-20T22:12:15.380565Z"
}
},
"outputs": [],
"source": [
"# a box for selection, bigger domains \n",
"bbox = [10,40,0,30] # [-180,180, 0, 90]"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:15.391523Z",
"start_time": "2021-02-20T22:12:15.387226Z"
}
},
"outputs": [],
"source": [
"### timeit can be used here because it is fast"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:29.488649Z",
"start_time": "2021-02-20T22:12:15.393010Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.74 s ± 77.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"tri,ind_tri =cut_region(mesh, bbox)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:45.576114Z",
"start_time": "2021-02-20T22:12:29.490557Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"198 ms ± 6.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"opt_tri, opt_ind_tri = opt_cut_region(mesh, bbox)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:47.677333Z",
"start_time": "2021-02-20T22:12:45.577761Z"
}
},
"outputs": [],
"source": [
"tri,ind_tri =cut_region(mesh, bbox)\n",
"opt_tri, opt_ind_tri = opt_cut_region(mesh, bbox)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:47.714040Z",
"start_time": "2021-02-20T22:12:47.678716Z"
}
},
"outputs": [],
"source": [
"# a quick test both old and opt version give same result\n",
"assert np.all(ind_tri == opt_ind_tri)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"ExecuteTime": {
"end_time": "2021-02-20T22:12:48.067073Z",
"start_time": "2021-02-20T22:12:47.715364Z"
}
},
"outputs": [],
"source": [
"assert np.all(tri.ravel() == opt_tri.ravel())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment