Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
iwr
dune-vtk
Commits
d93834ef
Commit
d93834ef
authored
Dec 16, 2020
by
dedner
Browse files
extend 'addPointData' export to python using new methods on 'Function'
minor change to test
parent
e634dfad
Changes
5
Hide whitespace changes
Inline
Side-by-side
dune/python/test/testvtk.py
View file @
d93834ef
import
math
from
dune.grid
import
structuredGrid
,
gridFunction
from
dune.vtk
import
vtkUnstructuredGridWriter
from
dune.vtk
import
vtkUnstructuredGridWriter
,
RangeTypes
,
FieldInfo
grid
=
structuredGrid
([
0
,
0
],[
1
,
1
],[
10
,
10
])
def
test1
(
writer
):
@
gridFunction
(
grid
)
def
f
(
x
):
return
x
[
0
]
*
x
[
1
]
writer
.
addPointData
(
f
)
writer
.
addPointData
(
f
,
name
=
"f"
)
del
f
def
test2
(
writer
):
@
gridFunction
(
grid
)
def
g
(
x
):
return
[
math
.
sin
(
2
*
math
.
pi
*
x
[
0
]
*
x
[
1
]),
x
[
0
]
*
x
[
1
]]
writer
.
addPointData
(
g
)
del
g
@
gridFunction
(
grid
)
def
g
(
x
):
return
[
math
.
sin
(
2
*
math
.
pi
*
x
[
0
]
*
x
[
1
]),
x
[
0
]
*
x
[
1
]]
writer1
=
vtkUnstructuredGridWriter
(
grid
)
test1
(
writer1
)
writer1
.
write
(
"test1"
)
writer2
=
vtkUnstructuredGridWriter
(
grid
)
test2
(
writer2
)
writer2
.
addPointData
(
g
,
name
=
"g"
)
writer2
.
write
(
"test2"
)
# fails since 'name' is missing on grid function
writer
=
vtkUnstructuredGridWriter
(
grid
)
test1
(
writer
)
test2
(
writer
)
writer
.
write
(
"test"
)
@
gridFunction
(
grid
)
def
g
(
x
):
return
[
math
.
sin
(
2
*
math
.
pi
*
x
[
0
]
*
x
[
1
]),
x
[
0
]
*
x
[
1
]]
*
5
writer3
=
vtkUnstructuredGridWriter
(
grid
)
test1
(
writer3
)
writer3
.
addPointData
(
g
,
name
=
"g"
)
writer3
.
write
(
"test3"
)
writer4
=
vtkUnstructuredGridWriter
(
grid
)
writer4
.
addPointData
(
g
,
name
=
"g10"
,
components
=
(
0
,
1
))
# neither
writer4
.
addPointData
(
g
,
name
=
"g012"
,
components
=
[
0
,
1
,
2
])
# is vector
writer4
.
addPointData
(
g
,
name
=
"g23"
,
components
=
[
2
,
3
])
# neiter
writer4
.
addPointData
(
g
,
name
=
"g2"
,
components
=
[
2
])
# is scalar
writer4
.
addPointData
(
g
,
name
=
"g23V"
,
range
=
RangeTypes
.
vector
,
components
=
[
2
,
3
])
# is vector
writer4
.
addPointData
(
g
,
name
=
"g23S"
,
range
=
RangeTypes
.
scalar
,
components
=
[
2
,
3
])
# is scalar
writer4
.
addPointData
(
g
,
info
=
FieldInfo
(
name
=
"g2S"
,
range
=
RangeTypes
.
scalar
),
components
=
[
2
])
# is scalar
writer4
.
addPointData
(
g
,
info
=
FieldInfo
(
name
=
"g2V"
,
range
=
RangeTypes
.
vector
),
components
=
[
2
])
# is vector
writer4
.
write
(
"test4"
)
dune/python/vtk/writer.hh
View file @
d93834ef
...
...
@@ -7,6 +7,7 @@
#include <dune/vtk/vtkwriter.hh>
#include <dune/python/pybind11/pybind11.h>
#include <dune/python/pybind11/stl.h>
namespace
Dune
{
...
...
@@ -33,7 +34,6 @@ namespace Dune
writer
.
write
(
name
);
},
pybind11
::
arg
(
"name"
)
);
cls
.
def
(
"write"
,
[]
(
Writer
&
writer
,
const
std
::
string
&
name
,
int
number
)
{
std
::
stringstream
s
;
s
<<
name
<<
std
::
setw
(
5
)
<<
std
::
setfill
(
'0'
)
<<
number
;
...
...
@@ -41,11 +41,59 @@ namespace Dune
},
pybind11
::
arg
(
"name"
),
pybind11
::
arg
(
"number"
)
);
cls
.
def
(
"addPointData"
,
[]
(
Writer
&
writer
,
VirtualizedGF
&
f
,
RangeTypes
range
,
DataTypes
data
)
{
writer
.
addPointData
(
f
);
},
pybind11
::
arg
(
"f"
),
pybind11
::
arg
(
"range"
)
=
RangeTypes
::
AUTO
,
pybind11
::
arg
(
"data"
)
=
DataTypes
::
FLOAT32
);
cls
.
def
(
"addPointData"
,
[]
(
Writer
&
writer
,
VirtualizedGF
&
f
,
std
::
string
&
name
,
RangeTypes
range
,
DataTypes
data
)
{
f
.
setName
(
name
);
writer
.
addPointData
(
f
);
},
pybind11
::
arg
(
"f"
),
pybind11
::
arg
(
"name"
),
pybind11
::
arg
(
"range"
)
=
RangeTypes
::
AUTO
,
pybind11
::
arg
(
"data"
)
=
DataTypes
::
FLOAT32
);
cls
.
def
(
"addPointData"
,
[]
(
Writer
&
writer
,
VirtualizedGF
&
f
,
std
::
string
&
name
,
std
::
vector
<
int
>
&
components
,
RangeTypes
range
,
DataTypes
data
)
{
f
.
setName
(
name
);
f
.
setComponents
(
components
);
writer
.
addPointData
(
f
);
},
pybind11
::
arg
(
"f"
),
pybind11
::
arg
(
"name"
),
pybind11
::
arg
(
"components"
),
pybind11
::
arg
(
"range"
)
=
RangeTypes
::
AUTO
,
pybind11
::
arg
(
"data"
)
=
DataTypes
::
FLOAT32
);
cls
.
def
(
"addPointData"
,
[]
(
Writer
&
writer
,
VirtualizedGF
&
f
,
FieldInfo
&
info
)
{
f
.
setFieldInfo
(
info
);
writer
.
addPointData
(
f
);
},
pybind11
::
arg
(
"f"
),
pybind11
::
arg
(
"info"
)
);
cls
.
def
(
"addPointData"
,
[]
(
Writer
&
writer
,
VirtualizedGF
&
f
)
{
[]
(
Writer
&
writer
,
VirtualizedGF
&
f
,
std
::
vector
<
int
>
&
components
,
FieldInfo
&
info
)
{
f
.
setFieldInfo
(
info
);
f
.
setComponents
(
components
);
writer
.
addPointData
(
f
);
},
pybind11
::
arg
(
"f"
)
);
pybind11
::
arg
(
"f"
)
,
pybind11
::
arg
(
"components"
),
pybind11
::
arg
(
"info"
)
);
}
}
// namespace Python
...
...
python/dune/vtk/CMakeLists.txt
View file @
d93834ef
add_python_targets
(
vtk
__init__
)
dune_add_pybind11_module
(
NAME _vtk
)
set_property
(
TARGET _vtk PROPERTY LINK_LIBRARIES dunecommon dunegeometry dunegrid dunevtk APPEND
)
python/dune/vtk/__init__.py
View file @
d93834ef
import
hashlib
from
dune.generator.generator
import
SimpleGenerator
from
._vtk
import
*
generator
=
SimpleGenerator
(
"VtkWriter"
,
"Dune::Vtk"
)
def
load
(
includes
,
typeName
,
*
args
):
...
...
@@ -22,4 +24,5 @@ def vtkUnstructuredGridWriter(grid):
typeName
=
"Dune::VtkUnstructuredGridWriter<"
+
grid
.
_typeName
+
">"
includes
=
grid
.
_includes
+
[
"dune/vtk/writers/vtkunstructuredgridwriter.hh"
]
return
load
(
includes
,
typeName
).
VtkWriter
(
grid
)
Writer
=
load
(
includes
,
typeName
).
VtkWriter
return
Writer
(
grid
)
python/dune/vtk/_vtk.cc
0 → 100644
View file @
d93834ef
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#include <dune/vtk/types.hh>
#include <dune/python/pybind11/pybind11.h>
PYBIND11_MODULE
(
_vtk
,
module
)
{
{
using
namespace
Dune
::
Vtk
;
pybind11
::
enum_
<
FormatTypes
>
formatTypes
(
module
,
"FormatTypes"
);
for
(
const
auto
e
:
formatTypesList
)
formatTypes
.
value
(
to_string
(
e
).
c_str
(),
e
);
pybind11
::
enum_
<
RangeTypes
>
rangeTypes
(
module
,
"RangeTypes"
);
for
(
const
auto
e
:
rangeTypesList
)
rangeTypes
.
value
(
to_string
(
e
).
c_str
(),
e
);
pybind11
::
enum_
<
DataTypes
>
dataTypes
(
module
,
"DataTypes"
);
for
(
const
auto
e
:
dataTypesLists
)
dataTypes
.
value
(
to_string
(
e
).
c_str
(),
e
);
pybind11
::
class_
<
FieldInfo
>
fieldInfo
(
module
,
"FieldInfo"
);
fieldInfo
.
def
(
pybind11
::
init
(
[](
std
::
string
name
,
RangeTypes
range
,
DataTypes
data
)
{
return
new
FieldInfo
(
name
,
range
,
data
);
}
),
pybind11
::
arg
(
"name"
),
pybind11
::
arg
(
"range"
)
=
RangeTypes
::
AUTO
,
pybind11
::
arg
(
"data"
)
=
DataTypes
::
FLOAT32
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment