Skip to content

Commit

Permalink
Add "pyplot" backend to hex_plotter
Browse files Browse the repository at this point in the history
The "gnuplot" backend remains available, but "pyplot" is the new default.

The output from the two backends is tuned to look fairly similar.

There are options to pass a script filename and a picture filename, if
the script name is not passed a default one will be produced, as we want
to /always/ have a saved script, for reproducibility. If the figure name
is not passed there will be no saved picture, but just a GUI popup for
both backends (or something similar for peculiar cases).

The "gnuplot" backend aimsˆ† to support also a custom <terminal> such as
<qt>, <wxt>, <dumb> (especially useful on HPC servers), <png>, <svg>,…
which instead are necessary to allow correct I/O if figure_name is given.

†) Issue kookma/ogpf#41 needs to be solved...
   > for now I just redirect the fpm.toml pointer to a relevant "devel"
     branch on my personal fork ==> for now just changes the default
     terminal, so to allow proper testing here, but a new public API
     should be defined to allow the user choose.

—————————————————————————————————————————————————————————————————————————

Let me brag the <dumb> terminal "gnuplot" output :)

—————————————————————————————————————————————————————————————————————————

                 7 +-------------------------------------------+
                   |                                           |
                   |               F          F                |
                 6 |-+        F          F          F          |
                   |                                           |
                   |                                           |
                 5 |-+        F          F          F          |
                   |                                           |
                   |    F          F          F          F     |
                 4 |-+                                         |
              y    |                                           |
                   |    F          F          F          F     |
                   |                                           |
                 3 |-+        F          F          F          |
                   |                                           |
                   |                                           |
                 2 |-+        F          F          F          |
                   |               F          F                |
                   |     +      +     +     +     +      +     |
                 1 +-------------------------------------------+
                  -1     0      1     2     3     4      5     6
                                         x

—————————————————————————————————————————————————————————————————————————
  • Loading branch information
beddalumia committed Sep 16, 2022
1 parent 4b4729a commit e548172
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 10 deletions.
21 changes: 20 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
# Build files
**/*.mod
# FORD files
doc/
# RAW debugging
fort.*
**/*.gp
# OGPF output script files
**/*.gp
# PyPlot allowed output files
**/*.eps
**/*.jpeg
**/*.jpg
**/*.pdf
**/*.pgf
**/*.png
**/*.ps
**/*.raw
**/*.rgba
**/*.svg
**/*.svgz
**/*.tif
**/*.tiff
**/*.py
6 changes: 4 additions & 2 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ library = true
[dependencies]
assert.git = "https://github.com/sourceryinstitute/assert.git"
assert.tag = "1.4.0"
ogpf.git = "https://github.com/kookma/ogpf.git"
ogpf.branch = "master"
ogpf.git = "https://github.com/bellomia/OGPF.git"
ogpf.branch = "expose-term-options-to-user"
pyplot-fortran.git = "https://github.com/jacobwilliams/pyplot-fortran.git"
pyplot-fortran.tag = "3.2.0"
90 changes: 84 additions & 6 deletions src/hex_plotter.f90
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module hex_plotter
!! Providing plotting facilities for hex tessellations

use pyplot_module
use ogpf
use hex_coordinates
use hex_layout
Expand All @@ -16,18 +17,27 @@ module hex_plotter

contains

subroutine hex_plot(layout,hexagons)
subroutine hex_plot(layout,hexagons,backend,figure_name,script_name,set_terminal)
type(unit_cell),intent(in) :: layout
type(hex),intent(in) :: hexagons(:)
character(*),intent(in),optional :: backend !! default: "pyplot" (or "gnuplot")
character(*),intent(in),optional :: figure_name
character(*),intent(in),optional :: script_name
character(*),intent(in),optional :: set_terminal !! relevant to gnuplot backend
character(8) :: engine
character(32) :: source_name
integer :: M
type(gpf) :: plotter
type(pyplot) :: plt
type(gpf) :: gnu
type(xy_tile),allocatable :: corner(:)
real(8),dimension(N) :: xtmp,ytmp
real(8),allocatable :: x(:),y(:)
integer :: i,j

M = size(hexagons)
allocate(x(N*M),y(N*M),corner(M))
corner = hex2corner(layout,hexagons)

do i = 1,M
do j = 1,N
xtmp(j) = corner(i)%vertex(j)%x
Expand All @@ -36,11 +46,79 @@ subroutine hex_plot(layout,hexagons)
x((1+N*(i-1)):N*i) = xtmp
y((1+N*(i-1)):N*i) = ytmp
enddo
call plotter%xlabel('x')
call plotter%ylabel('y')
call plotter%filename("hex_temp_script.gp")
call plotter%plot(x,y,'with points pt 6 ps 1.2 lc rgb "#000000"')

if(present(backend))then
engine = trim(backend)
else
engine = "pyplot"
end if

select case(trim(engine))

case default
print*, "unknown backend: no plot generated"

case ("pyplot")

call plt%initialize(xlabel='x',ylabel='y',axis_equal=.true.)
call plt%add_plot(x,y,label='',linestyle='o',markersize=5)

if(present(script_name))then
source_name = script_name
else
source_name = "hex_plot.py"
endif

if(present(figure_name))then
call plt%savefig(trim(figure_name), pyfile=trim(source_name))
print*
print*, "> PyPlot figure saved to: "//trim(figure_name)
print*
else
print*
print*, "> PyPlot GUI popping up..."
print*
call plt%showfig(pyfile=trim(source_name))
endif

case ("gnuplot")

if(present(script_name))then
source_name = script_name
else
source_name = "hex_plot.gp"
endif

if(present(script_name))then
source_name = script_name
else
source_name = "hex_plot.gp"
endif

if(present(set_terminal))then
call gnu%options("set term "//set_terminal//";")
else
call gnu%options("set term qt;")
endif

if(present(figure_name))then
call gnu%options('set output "'//figure_name//'"')
endif

call gnu%options("set size ratio -1;")
call gnu%options("unset grid")
call gnu%xlabel('x')
call gnu%ylabel('y')
call gnu%filename(source_name)
print*
print*, "> Gnuplot GUI popping up..."
print*
call gnu%plot(x,y,'with points pt 6 ps 1.2 lc rgb "#000000"')

end select

deallocate(x,y)

end subroutine


Expand Down
6 changes: 5 additions & 1 deletion test/unit.f90
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ program unit_test

print*, ""
print*, "Plotting neighborhood of hex a..."
call hex_plot(v,neighborhood)
call hex_plot(v,neighborhood,backend="pyplot",figure_name='pyflower.svg')
call hex_plot(v,neighborhood,backend="gnuplot",figure_name='gnuflower.svg')
call hex_plot(v,neighborhood,backend="gnuplot",set_terminal='dumb')
call hex_plot(v,neighborhood,backend="pyplot")
! THIS HAS TO BE TESTED MUCH MORE CAREFULLY TO ASSURE GOOD COVERAGE

end program unit_test

0 comments on commit e548172

Please sign in to comment.