#!/usr/bin/env python
# 
# This example shows how to save quantities associated to cells and vertices
# using the low-level ViennaGrid wrapper for Python (viennagrid.wrapper).

from __future__ import print_function

from viennagrid.wrapper import TriangularCartesian2D_Domain as Domain
from viennagrid.wrapper import TriangularCartesian2D_Segmentation as Segmentation
from viennagrid.wrapper import PointCartesian2D as Point

from viennagrid.wrapper import TriangularCartesian2D_Cell_Accessor as CellAccessor

from viennagrid.wrapper import surface

# Create an empty domain
domain = Domain()

# Add vertices to the domain. These vertices will then be used to define
# cells.
domain.make_vertex(Point(0, 0)) # Vertex with ID #0
domain.make_vertex(Point(1, 0)) # Vertex with ID #1
domain.make_vertex(Point(2, 0)) # Vertex with ID #2
domain.make_vertex(Point(2, 1)) # Vertex with ID #3
domain.make_vertex(Point(1, 1)) # Vertex with ID #4
domain.make_vertex(Point(0, 1)) # Vertex with ID #5

# Create a segmentation of the domain
segmentation = Segmentation(domain)

# Create 2 segments in the segmentation of the domain
seg0 = segmentation.make_segment()
seg1 = segmentation.make_segment()

# Create 2 cells in the first segment. Since the domain is a triangular domain,
# cells are triangles and three vertices must be specified to create a cell.
seg0.make_cell(domain.get_vertex(0), domain.get_vertex(1), domain.get_vertex(5))
seg0.make_cell(domain.get_vertex(1), domain.get_vertex(4), domain.get_vertex(5))

# Create 2 cells in the second segment. This time, we will use variable to
# make the code shorter.
seg1.make_cell(domain.get_vertex(1), domain.get_vertex(2), domain.get_vertex(4))
seg1.make_cell(domain.get_vertex(3), domain.get_vertex(2), domain.get_vertex(4))

# Create a new accessor for the cells of the domain and save for each cell
# its surface.
cell_surface_accessor = CellAccessor()
# For each cell of the domain, we calculate its surface and store the surface
# of the cell using the accessor. To do that, we call the 'set_value' method
# of the accessor specifying the cell and the value that we want to assign to
# the cell.
for cell in domain.cells:
	cell_surface_accessor.set_value(cell, surface(cell))
# Now we want to read the value corresponding to each cell stored in the accessor.
# Since the cells that have a value stored in the accessor are all the cells of the
# domain, we simply iterate over the cells of the domain and get the value stored
# in the accessor for that cell using the 'get_value' method of the accessor.
for i, cell in enumerate(domain.cells):
	value = cell_surface_accessor.get_value(cell)
	print('Cell #%(i)d has surface %(value)f' % locals())

@@ -36,22 +36,22 @@
 mple
+s
  show
-s
  how to 
 save
@@ -50,169 +50,748 @@
  to 
-save quantities associated to cells and vertices%0A# using the low-level ViennaGrid wrapper for Python (viennagrid.wrapper).%0A%0Afrom __future__ import print_function
+use accessors to associate scalar data to mesh elements,%0A# and how to read and write those data from/to mesh files using ViennaGrid's readers%0A# and writers.%0A%0Afrom __future__ import print_function%0A%0A# In this example, we will illustrate how to read and write scalar data from and to%0A# mesh files using accessors and the VTK reader and writer, respectively.%0A# %0A# We will start defining a domain of triangles in the cartesian 2D space and will%0A# create some vertices and cells (triangles) in it. Then we will store the surface%0A# of the cells using accessors. Finally, we will write the mesh and the scalar data%0A# to a VTK file and we will read it again from the mesh file, just to illustrate%0A# how to read and write data stored using accessors.
 %0A%0Afr
@@ -1081,17 +1081,16 @@
 ccessor%0A
-%0A
 from vie
@@ -1121,16 +1121,68 @@
 urface%0A%0A
+from viennagrid.wrapper import read_vtk, write_vtk%0A%0A
 # Create
@@ -2524,16 +2524,17 @@
 essor()%0A
+%0A
 # For ea
@@ -2850,16 +2850,17 @@
 (cell))%0A
+%0A
 # Now we
@@ -3320,8 +3320,995 @@
 cals())%0A
+%0A# Next, we want to write the mesh and the scalar data to a VTK file. To do that,%0A# we must provide each accessor with a name for the data stored in it, preferrably%0A# a descriptive name of what the data mean.%0A# %0A# Since we have stored the surface of the cells, we will call the data 'surface'.%0A# Then, we create a dictionary associating the quantity name to the accessor that%0A# stores that given quantity. Since we only have one accessor to save, this dictionary%0A# will have only one key.%0Aaccessors = %7B'surface': cell_surface_accessor%7D%0A%0A# Now we call the VTK write as in the I/O tutorial, but we also specify the dictionary%0A# that contains all accessors that should be written to the file.%0Awrite_vtk('tri2d_mesh_with_surface_data.vtu', domain, accessors)%0A%0A# If you want to save the segmentation information, too, you must call the writer as%0A# explained in the I/O tutorial, also specifying the segmentation:%0Awrite_vtk('tri2d_mesh_with_surface_data.vtu', domain, segmentation, accessors)%0A

Improve low-level accessor example showing how to read and write to mesh files.