Skip to content

Commit 6a68d61

Browse files
committed
examples: Add tests for Border and document in examples
1 parent 3e7dd33 commit 6a68d61

2 files changed

Lines changed: 358 additions & 15 deletions

File tree

examples/userapi/03_subdomains.ipynb

Lines changed: 336 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,44 @@
935935
"Operator `Kernel` ran in 0.01 s\n"
936936
]
937937
},
938+
{
939+
"data": {
940+
"text/plain": [
941+
"PerformanceSummary([(PerfKey(name='section0', rank=None),\n",
942+
" PerfEntry(time=9.099999999999999e-05, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),\n",
943+
" (PerfKey(name='section1', rank=None),\n",
944+
" PerfEntry(time=7e-05, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[])),\n",
945+
" (PerfKey(name='section2', rank=None),\n",
946+
" PerfEntry(time=7.5e-05, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])"
947+
]
948+
},
949+
"execution_count": 33,
950+
"metadata": {},
951+
"output_type": "execute_result"
952+
}
953+
],
954+
"source": [
955+
"#NBVAL_IGNORE_OUTPUT\n",
956+
"left = Left()\n",
957+
"right = Right()\n",
958+
"mid = Middle()\n",
959+
"\n",
960+
"new_grid = Grid(shape=(10, 10), subdomains=(left, right, mid))\n",
961+
"\n",
962+
"g = Function(name='g', grid=new_grid)\n",
963+
"\n",
964+
"eq1 = Eq(g, g+1, subdomain=new_grid.subdomains['middle'])\n",
965+
"eq2 = Eq(g, g+2, subdomain=new_grid.subdomains['left'])\n",
966+
"eq3 = Eq(g, g+3, subdomain=new_grid.subdomains['right'])\n",
967+
"\n",
968+
"Operator([eq1, eq2, eq3])()"
969+
]
970+
},
971+
{
972+
"cell_type": "code",
973+
"execution_count": 34,
974+
"metadata": {},
975+
"outputs": [
938976
{
939977
"data": {
940978
"text/plain": [
@@ -950,42 +988,326 @@
950988
" [0., 0., 0., 0., 0., 0., 0., 0., 3., 3.]], dtype=float32)"
951989
]
952990
},
953-
"execution_count": 33,
991+
"execution_count": 34,
954992
"metadata": {},
955993
"output_type": "execute_result"
956994
}
957995
],
958996
"source": [
959-
"left = Left()\n",
960-
"right = Right()\n",
961-
"mid = Middle()\n",
997+
"g.data"
998+
]
999+
},
1000+
{
1001+
"cell_type": "markdown",
1002+
"metadata": {},
1003+
"source": [
1004+
"## `SubDomainSet` and `Border`:\n",
9621005
"\n",
963-
"new_grid = Grid(shape=(10, 10), subdomains=(left, right, mid))\n",
1006+
"To avoid creating many `SubDomain`s (which can become cumbersome), the `SubDomainSet` object is available, allowing users to easily define a large number of subdomains. A `SubDomainSet` is created as follows:"
1007+
]
1008+
},
1009+
{
1010+
"cell_type": "code",
1011+
"execution_count": 35,
1012+
"metadata": {},
1013+
"outputs": [
1014+
{
1015+
"name": "stderr",
1016+
"output_type": "stream",
1017+
"text": [
1018+
"Operator `Kernel` ran in 0.01 s\n"
1019+
]
1020+
},
1021+
{
1022+
"data": {
1023+
"text/plain": [
1024+
"PerformanceSummary([(PerfKey(name='section0', rank=None),\n",
1025+
" PerfEntry(time=0.000204, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])"
1026+
]
1027+
},
1028+
"execution_count": 35,
1029+
"metadata": {},
1030+
"output_type": "execute_result"
1031+
}
1032+
],
1033+
"source": [
1034+
"#NBVAL_IGNORE_OUTPUT\n",
1035+
"from devito import SubDomainSet\n",
9641036
"\n",
965-
"g = Function(name='g', grid=new_grid)\n",
1037+
"class MySubDomains(SubDomainSet):\n",
1038+
" name = 'mydomains'\n",
9661039
"\n",
967-
"eq1 = Eq(g, g+1, subdomain=new_grid.subdomains['middle'])\n",
968-
"eq2 = Eq(g, g+2, subdomain=new_grid.subdomains['left'])\n",
969-
"eq3 = Eq(g, g+3, subdomain=new_grid.subdomains['right'])\n",
1040+
"sds_grid = Grid(shape=(10, 10))\n",
9701041
"\n",
971-
"Operator([eq1, eq2, eq3])()\n",
1042+
"# Bounds for the various subdomains as (x_ltkn, x_rtkn, y_ltkn, y_rtkn)\n",
1043+
"# Note that this should be extended according the number of dimensions your grid has\n",
1044+
"bounds = (\n",
1045+
" np.array([1, 6], dtype=np.int32), # x left-side thickness\n",
1046+
" np.array([6, 1], dtype=np.int32), # x right-side thickness\n",
1047+
" np.array([1, 1], dtype=np.int32), # y left-side thickness\n",
1048+
" np.array([1, 1], dtype=np.int32) # y right-side thickness\n",
1049+
")\n",
9721050
"\n",
973-
"g.data"
1051+
"ndomains = 2 # Number of subdomains in the SubDomainSet\n",
1052+
"mysds = MySubDomains(N=ndomains, bounds=bounds, grid=grid)\n",
1053+
"\n",
1054+
"sds_f = Function(name='f', grid=sds_grid, dtype=np.int32)\n",
1055+
"Operator(Eq(sds_f, sds_f+1, subdomain=mysds))()"
1056+
]
1057+
},
1058+
{
1059+
"cell_type": "code",
1060+
"execution_count": 36,
1061+
"metadata": {},
1062+
"outputs": [
1063+
{
1064+
"data": {
1065+
"text/plain": [
1066+
"Data([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1067+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],\n",
1068+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],\n",
1069+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],\n",
1070+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1071+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1072+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],\n",
1073+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],\n",
1074+
" [0, 1, 1, 1, 1, 1, 1, 1, 1, 0],\n",
1075+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=int32)"
1076+
]
1077+
},
1078+
"execution_count": 36,
1079+
"metadata": {},
1080+
"output_type": "execute_result"
1081+
}
1082+
],
1083+
"source": [
1084+
"sds_f.data"
9741085
]
9751086
},
9761087
{
9771088
"cell_type": "markdown",
9781089
"metadata": {},
9791090
"source": [
980-
"## Note:\n",
1091+
"For the common case where a subdomain is needed for the edges of the grid, a `Border` convenience object is provided, which can be used as follows:"
1092+
]
1093+
},
1094+
{
1095+
"cell_type": "code",
1096+
"execution_count": 37,
1097+
"metadata": {},
1098+
"outputs": [
1099+
{
1100+
"name": "stderr",
1101+
"output_type": "stream",
1102+
"text": [
1103+
"Operator `Kernel` ran in 0.01 s\n"
1104+
]
1105+
},
1106+
{
1107+
"data": {
1108+
"text/plain": [
1109+
"PerformanceSummary([(PerfKey(name='section0', rank=None),\n",
1110+
" PerfEntry(time=0.000812, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])"
1111+
]
1112+
},
1113+
"execution_count": 37,
1114+
"metadata": {},
1115+
"output_type": "execute_result"
1116+
}
1117+
],
1118+
"source": [
1119+
"#NBVAL_IGNORE_OUTPUT\n",
1120+
"from devito import Border\n",
1121+
"\n",
1122+
"# Reset the data\n",
1123+
"sds_f.data[:] = 0\n",
9811124
"\n",
982-
"When creating a grid with many subdomains the process introduced in this notebook becomes cumbersome. For such cases the `SubDomainSet` object is available allowing users to easily define a large number of subdomains. A tutorial regarding the use of `SubDomainSet`'s will be released at some point in the future."
1125+
"# Construct a border of thickness 2\n",
1126+
"border = Border(sds_grid, 2)\n",
1127+
"\n",
1128+
"Operator(Eq(sds_f, sds_f+1, subdomain=border))()\n"
1129+
]
1130+
},
1131+
{
1132+
"cell_type": "code",
1133+
"execution_count": 38,
1134+
"metadata": {},
1135+
"outputs": [
1136+
{
1137+
"data": {
1138+
"text/plain": [
1139+
"Data([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1140+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1141+
" [1, 1, 0, 0, 0, 0, 0, 0, 1, 1],\n",
1142+
" [1, 1, 0, 0, 0, 0, 0, 0, 1, 1],\n",
1143+
" [1, 1, 0, 0, 0, 0, 0, 0, 1, 1],\n",
1144+
" [1, 1, 0, 0, 0, 0, 0, 0, 1, 1],\n",
1145+
" [1, 1, 0, 0, 0, 0, 0, 0, 1, 1],\n",
1146+
" [1, 1, 0, 0, 0, 0, 0, 0, 1, 1],\n",
1147+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1148+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32)"
1149+
]
1150+
},
1151+
"execution_count": 38,
1152+
"metadata": {},
1153+
"output_type": "execute_result"
1154+
}
1155+
],
1156+
"source": [
1157+
"sds_f.data"
1158+
]
1159+
},
1160+
{
1161+
"cell_type": "markdown",
1162+
"metadata": {},
1163+
"source": [
1164+
"Note that one can be more selective if a border is only wanted on a specific dimension:"
1165+
]
1166+
},
1167+
{
1168+
"cell_type": "code",
1169+
"execution_count": 39,
1170+
"metadata": {},
1171+
"outputs": [
1172+
{
1173+
"name": "stderr",
1174+
"output_type": "stream",
1175+
"text": [
1176+
"Operator `Kernel` ran in 0.01 s\n"
1177+
]
1178+
},
1179+
{
1180+
"data": {
1181+
"text/plain": [
1182+
"PerformanceSummary([(PerfKey(name='section0', rank=None),\n",
1183+
" PerfEntry(time=0.000173, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])"
1184+
]
1185+
},
1186+
"execution_count": 39,
1187+
"metadata": {},
1188+
"output_type": "execute_result"
1189+
}
1190+
],
1191+
"source": [
1192+
"#NBVAL_IGNORE_OUTPUT\n",
1193+
"\n",
1194+
"# Reset the data\n",
1195+
"sds_f.data[:] = 0\n",
1196+
"\n",
1197+
"x, y = sds_grid.dimensions\n",
1198+
"\n",
1199+
"# Construct a border of thickness 2 in only the x dimension\n",
1200+
"border2 = Border(sds_grid, 2, dims=x)\n",
1201+
"\n",
1202+
"Operator(Eq(sds_f, sds_f+1, subdomain=border2))()"
1203+
]
1204+
},
1205+
{
1206+
"cell_type": "code",
1207+
"execution_count": 40,
1208+
"metadata": {},
1209+
"outputs": [
1210+
{
1211+
"data": {
1212+
"text/plain": [
1213+
"Data([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1214+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1215+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1216+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1217+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1218+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1219+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1220+
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1221+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1222+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32)"
1223+
]
1224+
},
1225+
"execution_count": 40,
1226+
"metadata": {},
1227+
"output_type": "execute_result"
1228+
}
1229+
],
1230+
"source": [
1231+
"sds_f.data"
1232+
]
1233+
},
1234+
{
1235+
"cell_type": "markdown",
1236+
"metadata": {},
1237+
"source": [
1238+
"A border can even be introduced on specific sides:"
1239+
]
1240+
},
1241+
{
1242+
"cell_type": "code",
1243+
"execution_count": 41,
1244+
"metadata": {},
1245+
"outputs": [
1246+
{
1247+
"name": "stderr",
1248+
"output_type": "stream",
1249+
"text": [
1250+
"Operator `Kernel` ran in 0.01 s\n"
1251+
]
1252+
},
1253+
{
1254+
"data": {
1255+
"text/plain": [
1256+
"PerformanceSummary([(PerfKey(name='section0', rank=None),\n",
1257+
" PerfEntry(time=0.00030199999999999997, gflopss=0.0, gpointss=0.0, oi=0.0, ops=0, itershapes=[]))])"
1258+
]
1259+
},
1260+
"execution_count": 41,
1261+
"metadata": {},
1262+
"output_type": "execute_result"
1263+
}
1264+
],
1265+
"source": [
1266+
"#NBVAL_IGNORE_OUTPUT\n",
1267+
"\n",
1268+
"# Reset the data\n",
1269+
"sds_f.data[:] = 0\n",
1270+
"\n",
1271+
"# Construct a border of thickness 2\n",
1272+
"# Add a border on both sides in the x dimension, but only on the 'left' (from index 0) side in y\n",
1273+
"border3 = Border(sds_grid, 2, dims={x: x, y: 'left'})\n",
1274+
"\n",
1275+
"Operator(Eq(sds_f, sds_f+1, subdomain=border3))()"
1276+
]
1277+
},
1278+
{
1279+
"cell_type": "code",
1280+
"execution_count": 42,
1281+
"metadata": {},
1282+
"outputs": [
1283+
{
1284+
"data": {
1285+
"text/plain": [
1286+
"Data([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1287+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1288+
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1289+
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1290+
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1291+
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1292+
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1293+
" [1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
1294+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
1295+
" [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int32)"
1296+
]
1297+
},
1298+
"execution_count": 42,
1299+
"metadata": {},
1300+
"output_type": "execute_result"
1301+
}
1302+
],
1303+
"source": [
1304+
"sds_f.data"
9831305
]
9841306
}
9851307
],
9861308
"metadata": {
9871309
"kernelspec": {
988-
"display_name": "Python 3 (ipykernel)",
1310+
"display_name": "devito",
9891311
"language": "python",
9901312
"name": "python3"
9911313
},

0 commit comments

Comments
 (0)