-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPython_pybind11_interface.html
203 lines (201 loc) · 14.7 KB
/
Python_pybind11_interface.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.10.0"/>
<title>G+Smo: Python via pybind11</title>
<link href="gismodoxy_tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript" src="cookie.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://people.ricam.oeaw.ac.at/gismo/mj/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="gismodoxy.css" rel="stylesheet" type="text/css">
<!-- -->
<!-- <script type="text/javascript"> -->
<!-- </script> -->
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<!-- <a name="top"></a> -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="gslogo-doxy.jpg"/></td>
<td style="padding-left: 0.5em;">
<div id="projectname"><a href="index.html"><font style="font-variant:small-caps;">G+S</font><font style="font-variant:small-caps;"
color="#000000">mo</font></a>
 <span id="projectnumber">25.01.0</span>
</div>
<div id="projectbrief">Geometry + Simulation Modules</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<span id="MSearchSelect" onmouseover="return searchBox.OnSearchSelectShow()" onmouseout="return searchBox.OnSearchSelectHide()"> </span>
<input type="text" id="MSearchField" value="" placeholder="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.10.0 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search/",'.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
$(function(){initNavTree('Python_pybind11_interface.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<div id="MSearchResults">
<div class="SRPage">
<div id="SRIndex">
<div id="SRResults"></div>
<div class="SRStatus" id="Loading">Loading...</div>
<div class="SRStatus" id="Searching">Searching...</div>
<div class="SRStatus" id="NoMatches">No Matches</div>
</div>
</div>
</div>
</div>
<div><div class="header">
<div class="headertitle"><div class="title">Python via pybind11</div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <a href="https://pybind11.readthedocs.io/en/latest/">pybind11</a> bindings are explicitly defined inside G+Smo, which are then compiled into a package called <code>pygismo</code>. The advantage of pybind11-based bindings compared to the <a class="el" href="Python_cppyy_interface.html">Python via cppyy</a>.</p>
<h3>Obtaining pygismo</h3>
<h4>From pip</h4>
<p>Since the G+Smo pybind11-bindings are compiled into a package, it is simply installed via <code>pip</code>. On any system, one can install pygismo as follows: </p><div class="fragment"><div class="line">pip install pygismo</div>
</div><!-- fragment --><p> pygismo is built upon every commit in G+Smo's stable branch. However, upon every <b>release</b>, it is uploaded to <a href="https://pypi.org/project/pygismo">PyPi</a>. To obtain the latest version of pygismo, one can download a <em>wheel</em> compiled by <a href="https://github.com/gismo/gismo/actions/workflows/build_wheels.yml">this</a> workflow on GitHub, and install it with pip as follows: </p><div class="fragment"><div class="line">pip install wheel_file.whl</div>
</div><!-- fragment --><h4>Compiled from source</h4>
<p>Alternative to installing pygismo from a pre-compiled wheel via pip, one can compile pygismo locally: </p><div class="fragment"><div class="line">cd /path/to/build</div>
<div class="line">cmake . -DGISMO_WITH_PYBIND11=ON -Dpybind11_DIR=path/to/pybind11 -DPYBIND11_PYTHON_VERSION=<your python version></div>
<div class="line">make pygismo</div>
</div><!-- fragment --><p> Then, in your Python file, you can import pygismo as follows: </p><div class="fragment"><div class="line"><span class="keyword">import</span> os, sys</div>
<div class="line"><span class="comment"># Obtain pygismo</span></div>
<div class="line">gismo_path=os.path.join(os.getcwd() , <span class="stringliteral">"/path/to/gismo"</span>)</div>
<div class="line">print(<span class="stringliteral">"G+Smo path:"</span>,gismo_path,<span class="stringliteral">"(change if needed)."</span>)</div>
<div class="line">sys.path.append(gismo_path+<span class="stringliteral">"build/lib"</span>)</div>
<div class="line"> </div>
<div class="line"><span class="comment"># Import pygismo</span></div>
<div class="line"><span class="keyword">import</span> pygismo <span class="keyword">as</span> gs </div>
</div><!-- fragment --><h3>Developing pybind11 bindings</h3>
<p>The Python bindings are generated in the file <code>gsPybind11.cpp</code>. Inside this file, a Python module is defined as follows: </p><div class="fragment"><div class="line">PYBIND11_MODULE(pygismo, m)</div>
<div class="line">{</div>
<div class="line"> <span class="comment">// Some module-related definitions</span></div>
<div class="line"> py::module core = m.def_submodule(<span class="stringliteral">"core"</span>);</div>
<div class="line"> <a class="code hl_class" href="classgismo_1_1gsTHBSplineBasis.html">gismo::pybind11_init_gsFunction</a>( core );</div>
<div class="line"> <span class="comment">// ... and other initializers</span></div>
<div class="line">}</div>
<div class="ttc" id="aclassgismo_1_1gsTHBSplineBasis_html"><div class="ttname"><a href="classgismo_1_1gsTHBSplineBasis.html">gismo::gsTHBSplineBasis</a></div><div class="ttdoc">Truncated hierarchical B-spline basis.</div><div class="ttdef"><b>Definition</b> gsTHBSplineBasis.h:36</div></div>
</div><!-- fragment --><p> As seen in the above snippet, the Python bindings of <a class="el" href="classgismo_1_1gsFunction.html">gsFunction</a> are included by the binding initializer-function <code>gismo::pybind11_init_gsFunction</code>. By convention, the initializer function is declared in <code><a class="el" href="gsFunction_8h.html" title="Provides declaration of Function abstract interface.">gsFunction.h</a></code>: </p><div class="fragment"><div class="line"><span class="preprocessor">#ifdef GISMO_WITH_PYBIND11</span></div>
<div class="line"> </div>
<div class="line"> <span class="keywordtype">void</span> pybind11_init_gsFunction(pybind11::module &m);</div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#endif </span><span class="comment">// GISMO_WITH_PYBIND11</span></div>
</div><!-- fragment --><p> The implementation of <code>pybind11_init_gsFunction</code> is provided in the file <code>gsFunction_.cpp</code>: </p><div class="fragment"><div class="line"><span class="preprocessor">#ifdef GISMO_WITH_PYBIND11</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">namespace </span>py = pybind11;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">void</span> pybind11_init_gsFunction(py::module &m)</div>
<div class="line">{</div>
<div class="line"> <span class="keyword">using </span>Base = gsFunctionSet<real_t>;</div>
<div class="line"> <span class="keyword">using </span>Class = gsFunction<real_t>;</div>
<div class="line"> py::class_<Class, Base>(m, <span class="stringliteral">"gsFunction"</span>)</div>
<div class="line"> .def(<span class="stringliteral">"jacobian"</span>, &Class::jacobian, <span class="stringliteral">"Returns the Jacobian"</span>)</div>
<div class="line"> .def(<span class="stringliteral">"hessian"</span>, &Class::hessian, <span class="stringliteral">"Returns the Hessian"</span>)</div>
<div class="line"> .def(<span class="stringliteral">"laplacian"</span>, &Class::laplacian, <span class="stringliteral">"Returns the Laplacian"</span>)</div>
<div class="line"> .def(<span class="stringliteral">"argMin"</span>, &Class::argMin, <span class="stringliteral">"Returns the position of the minimum"</span>,</div>
<div class="line"> py::arg(<span class="stringliteral">"accuracy"</span>) = 1e-6, py::arg(<span class="stringliteral">"max_loop"</span>) = 100,</div>
<div class="line"> py::arg(<span class="stringliteral">"init"</span>) = gsMatrix<real_t>(),</div>
<div class="line"> py::arg(<span class="stringliteral">"damping_factor"</span>) = 1 )</div>
<div class="line"> ;</div>
<div class="line">}</div>
<div class="line"><span class="preprocessor">#endif</span></div>
</div><!-- fragment --><p> In the snippet above, the following principles are followed:</p><ul>
<li><b>Class inheritance</b>: The class <code><a class="el" href="classgismo_1_1gsFunction.html" title="A function from a n-dimensional domain to an m-dimensional image.">gsFunction</a></code> is provided in <code>pybind11</code> in the line: <div class="fragment"><div class="line">py::class_<Class, Base>(m, <span class="stringliteral">"gsFunction"</span>)</div>
</div><!-- fragment --> Where the type <code>Class</code> is a <code><a class="el" href="classgismo_1_1gsFunction.html" title="A function from a n-dimensional domain to an m-dimensional image.">gsFunction</a><real_t></code> and the base type <code>Base</code> is a <code><a class="el" href="classgismo_1_1gsFunctionSet.html" title="Interface for the set of functions defined on a domain (the total number of functions in the set equa...">gsFunctionSet</a><real_t></code>. In this way, <code>pybind11</code> will be able to call all methods defined in <a class="el" href="classgismo_1_1gsFunctionSet.html">gsFunctionSet</a> on a <a class="el" href="classgismo_1_1gsFunction.html">gsFunction</a>.</li>
<li><b>Member function definition</b>: A member function is simply declared by calling, for example: <div class="fragment"><div class="line">.def(<span class="stringliteral">"argMin"</span>, &Class::argMin, <span class="stringliteral">"Returns the position of the minimum"</span>,</div>
<div class="line"> py::arg(<span class="stringliteral">"accuracy"</span>) = 1e-6, py::arg(<span class="stringliteral">"max_loop"</span>) = 100,</div>
<div class="line"> py::arg(<span class="stringliteral">"init"</span>) = gsMatrix<real_t>(),</div>
<div class="line"> py::arg(<span class="stringliteral">"damping_factor"</span>) = 1 )</div>
</div><!-- fragment --> Here, the first argument is the function name declared for <code>pybind11</code>, the second argument is the member function to be binded, and the third argument is a doc string. Optionally, more arguments can be given to declare default arguments.</li>
</ul>
<p>For more advanced bindings, e.g. binding a member function with multiple implementations for a single name, or returning a matrix by reference, we refer to existing bindings, e.g. the ones of <a class="el" href="classgismo_1_1gsGeometry.html">gsGeometry</a> or <a class="el" href="classgismo_1_1gsTensorBSplineBasis.html">gsTensorBSplineBasis</a>.</p>
<h3>Further information and examples</h3>
<p>For further information and examples regarding <code>pygismo</code>, we refer to the <a href="pygismo">pygismo documentation</a>, and the notebooks in the <a href="https://github.com/gismo/try_gismo"><code>try_gismo</code></a> repository. </p>
</div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Mon Feb 10 2025 09:21:44 for G+Smo by Doxygen v1.10.0 </li>
</ul>
</div>
<!-- Piwik -->
<!--
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://stats.sylphide-consulting.com/piwik/" : "http://stats.sylphide-consulting.com/piwik/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 20);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="http://stats.sylphide-consulting.com/piwik/piwik.php?idsite=20" style="border:0" alt="" /></p></noscript>
-->
<!-- End Piwik Tracking Code -->
</body>
</html>