1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
import gmsh
import math
import sys
# This example shows how to implement a simple interactive pre-processor for a
# finite element solver; in particular, it shows how boundary conditions,
# material properties, etc., can be specified on parts of the model.
gmsh.initialize(sys.argv)
if len(sys.argv) > 1:
try:
gmsh.open(sys.argv[1])
except:
pass
# For Gmsh to know which types of boundary conditions, materials, etc., are
# available, you should define "template" ONELAB parameters with names
# containing the following substrings:
#
# - "ONELAB Context/Point Template/"
# - "ONELAB Context/Curve Template/"
# - "ONELAB Context/Surface Template/"
# - "ONELAB Context/Volume Template/"
#
# Double-clicking on an entity in the GUI will then pop-up a context window
# where instances of these variables for the given entity can be edited. For
# example, if the "ONELAB Context/Curve Template/0Boundary condition" exists,
# double-clicking on curve 123 in the model (or in the visibility browser) will
# create "ONELAB Context/Curve 123/0Boundary condition" (or "ONELAB
# Context/Physical Curve 1000/0Boundary condition" depending on the choice in
# the context window, if curve 123 belongs to the physical curve 1000). The
# context window is also shown automatically when a new physical group is
# created in the GUI.
#
# As usual, every time the ONELAB database is changed the "ONELAB/Action"
# parameter is set to "check", which gives the Python code the opportunity to
# react in the event loop. This can e.g. be used to define new variables
# depending on the choices just made, or to show/hide existing variables. To
# make things simpler, several special attributes can also be specified
# ("ServerActionSet", "ServerActionShow", "ServerActionShowMatch",
# "ServerActionHide", ...) when creating parameters, which allow to set the
# visibility or the value of dependent parameters without explicitly processing
# the "check" action.
#
# Finally, explicit actions can be defined as well:
#
# * The "ONELAB/Button" parameter governs the behavior of the main "Run" button
# in the GUI; in this example the "run" action could trigger running the
# finite element solver with the current values of the parameters
#
# * Parameters with the "Macro" attribute set to "Action" will trigger an action
# named after their value; several such actions are defined in example below,
# in the main ONELAB tree or in the context window. Setting the "Aspect"
# attribute to "MiddleButton", "MiddleReturnButton", "RightButton",
# etc. allows to construct a context window with the usual button layout for
# action dialogs.
#
# See https://gitlab.onelab.info/doc/tutorials/-/wikis/ONELAB-JSON-interface for
# more information about the ONELAB JSON database interface.
parameters = """
[
{
"type":"number",
"name":"ONELAB Context/Curve Template/0Boundary condition",
"values":[0],
"choices":[0, 1],
"valueLabels":{"Temperature [℃]":0, "Flux [Wm⁻²]":1}
},
{
"type":"number",
"name":"ONELAB Context/Curve Template/1Value",
"values":[20],
"min":0,
"max":100,
"step":0.1
},
{
"type":"string",
"name":"ONELAB Context/Curve Template/2Action",
"values":["Some action on ONELAB Context/Curve Template"],
"attributes":{"Macro":"Action", "Aspect":"MiddleButton"}
},
{
"type":"string",
"name":"ONELAB Context/Curve Template/3Other action",
"values":["Some other action on ONELAB Context/Curve Template"],
"attributes":{"Macro":"Action", "Aspect":"RightReturnButton"}
},
{
"type":"number",
"name":"ONELAB Context/Surface Template/0Material",
"values":[1],
"choices":[0, 1, 2],
"valueLabels":{"User-defined":0, "Steel":1, "Concrete":2},
"attributes":
{
"ServerActionHideMatch":"ONELAB Context/Surface Template/1.*",
"ServerActionShowMatch 0":"ONELAB Context/Surface Template/1.*",
"ServerActionSet 1":"%10Cond, 205, %11Diff, 97",
"ServerActionSet 2":"%10Cond, 37, %11Diff, 12"
}
},
{
"type":"number",
"name":"ONELAB Context/Surface Template/10Cond",
"label":"Conductivity [Wm⁻¹K⁻¹]",
"values":[205],
"min":0.1,
"max":500,
"step":0.1,
"visible":false
},
{
"type":"number",
"name":"ONELAB Context/Surface Template/11Diff",
"label":"Diffusivity [mm²s⁻¹]",
"values":[97],
"min":10,
"max":1200,
"step":0.1,
"visible":false
},
{
"type":"string",
"name":"ONELAB/Button",
"values":["Run", "run"],
"visible":false
},
{
"type":"number",
"name":"0Modules/Solver/My solver/1Some flag",
"values":[0],
"choices":[0, 1]
},
{
"type":"number",
"name":"0Modules/Solver/My solver/2Some parameter",
"values":[1.234],
"min":0,
"max":10,
"step":0.1
},
{
"type":"number",
"name":"0Modules/Solver/My solver/3Some choice",
"values":[0],
"choices":[0, 1],
"valueLabels":{"Choice 1": 0, "Choice 2": 1}
},
{
"type":"string",
"name":"0Modules/Solver/My solver/3Some input",
"values":[""]
},
{
"type":"string",
"name":"0Modules/Solver/My solver/5Some action",
"values":["select entity"],
"attributes":{"Macro":"Action"}
},
{
"type":"string",
"name":"0Modules/Solver/My solver/6Some other action",
"values":["do something else"],
"attributes":{"Macro":"Action", "Aspect":"Button"}
}
]"""
gmsh.onelab.set(parameters)
def runSolver():
print("parameters =", gmsh.onelab.get())
print("running the solver with the given parameters...")
diffus = gmsh.onelab.getNames("ONELAB Context/.*([0-9]+)/11Diff")
for d in diffus:
print(d, "=", gmsh.onelab.getNumber(d))
def checkForEvent():
# check if an action is requested
action = gmsh.onelab.getString("ONELAB/Action")
if len(action) < 1:
# no action requested
pass
elif action[0] == "check":
# database was changed: update/define new parameters depending on new
# state
gmsh.onelab.setString("ONELAB/Action", [""])
#print("parameters = ", gmsh.onelab.get())
print("ONELAB check...")
gmsh.fltk.update()
elif action[0] == "reset":
# user clicked on "Reset database"
gmsh.onelab.setString("ONELAB/Action", [""])
gmsh.onelab.set(parameters)
gmsh.fltk.update()
elif action[0] == "run":
# user clicked on "Run"
gmsh.onelab.setString("ONELAB/Action", [""])
runSolver()
elif action[0] == "select entity":
# user clicked on "Some action"
gmsh.onelab.setString("ONELAB/Action", [""])
gmsh.fltk.setStatusMessage(
"Please select one or more curves (or press 'q' to quit)", True)
while 1:
r, ent = gmsh.fltk.selectEntities(dim=1)
print("select returned", r)
print("selected curves", ent)
if r == 0: break
if gmsh.fltk.isAvailable() == 0: return 0
if r and len(ent):
gmsh.fltk.showContextWindow(ent[0][0], ent[0][1])
gmsh.fltk.setStatusMessage("", True)
elif len(action[0]):
gmsh.onelab.setString("ONELAB/Action", [""])
print('Action to perform = ', action[0])
return 1
if "-nopopup" not in sys.argv:
gmsh.fltk.initialize()
# show the contents of the solver menu
gmsh.fltk.openTreeItem("0Modules/Solver")
while gmsh.fltk.isAvailable() and checkForEvent():
gmsh.fltk.wait()
else:
runSolver()
gmsh.finalize()
|