Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
gmsh
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Larry Price
gmsh
Commits
1479aa58
Commit
1479aa58
authored
11 years ago
by
Jonathan Lambrechts
Browse files
Options
Downloads
Patches
Plain Diff
add python field
parent
1aec02c9
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Mesh/FieldPython.h
+59
-0
59 additions, 0 deletions
Mesh/FieldPython.h
wrappers/gmshpy/gmshMesh.i
+16
-0
16 additions, 0 deletions
wrappers/gmshpy/gmshMesh.i
with
75 additions
and
0 deletions
Mesh/FieldPython.h
0 → 100644
+
59
−
0
View file @
1479aa58
#ifndef _FIELD_PYTHON_H_
#define _FIELD_PYTHON_H_
#include
"Field.h"
#include
"Python.h"
class
FieldPython
:
public
Field
{
PyObject
*
_callback
;
public:
const
char
*
getName
()
{
return
"Python"
;
}
std
::
string
getDescription
()
{
return
"simple call to a python function"
;
}
FieldPython
(
PyObject
*
cb
,
PyObject
*
arg
=
NULL
)
{
_callback
=
cb
;
Py_INCREF
(
_callback
);
}
~
FieldPython
()
{
Py_DECREF
(
_callback
);
}
double
operator
()
(
double
x
,
double
y
,
double
z
,
GEntity
*
ge
=
0
)
{
PyObject
*
pyge
=
SWIG_NewPointerObj
((
void
*
)
ge
,
SWIGTYPE_p_GEntity
,
0
);
PyObject
*
args
=
Py_BuildValue
(
"(dddO)"
,
x
,
y
,
z
,
pyge
);
PyObject
*
result
=
PyEval_CallObject
(
_callback
,
args
);
Py_DECREF
(
args
);
if
(
result
)
{
double
r
=
PyFloat_AsDouble
(
result
);
if
(
PyErr_Occurred
())
{
PyErr_Print
();
PyErr_Clear
();
Msg
::
Error
(
"Result of python function of field %i cannot be interpreted as a float."
,
id
);
r
=
MAX_LC
;
}
Py_DECREF
(
result
);
return
r
;
}
else
{
if
(
PyErr_Occurred
())
{
PyErr_Print
();
PyErr_Clear
();
}
Msg
::
Error
(
"An error occurs while evaluating python function of field %i."
,
id
);
return
MAX_LC
;
}
}
};
#endif
This diff is collapsed.
Click to expand it.
wrappers/gmshpy/gmshMesh.i
+
16
−
0
View file @
1479aa58
...
...
@@ -21,6 +21,7 @@
#include "meshPartition.h"
#endif
#include "Field.h"
#include "FieldPython.h"
#include "meshMetric.h"
#if defined(HAVE_ANN)
#include "CenterlineField.h"
...
...
@@ -58,6 +59,21 @@ namespace std {
%
include
"
meshPartition.h
"
#
endif
%
include
"
Field.h
"
%
extend
FieldManager
{
int addPythonField(PyObject *callback, int id = -1) {
if (id == -1) {
id = $self->newId();
}
if($self->find(id) != $self->end()) {
Msg::Error("Field id %i is already defined", id);
return -1;
}
Field *f = new FieldPython(callback);
f->id = id;
$self->operator[](id) = f;
return id;
}
}
%
include
"
meshMetric.h
"
#
if
defined
(
HAVE_ANN
)
%
include
"
CenterlineField.h
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment