Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pgfplot #2939

Merged
merged 1 commit into from
May 31, 2023
Merged

add pgfplot #2939

merged 1 commit into from
May 31, 2023

Conversation

ksagiyam
Copy link
Contributor

Description

Enable outputting Function data for LaTeX tikz plots. Not for large Functions.

Example:

from firedrake import *
  

mesh = TorusMesh(31, 13, 2.0, 0.5, quadrilateral=False, print_latex_example=True)
x, y, z = SpatialCoordinate(mesh)
V = FunctionSpace(mesh, "CG", 2)
f = Function(V, name="torus_tri").interpolate(x)
pgfplot(f, "./pgfplot_example_torus_tri.txt", degree=2)

generates an output file "./pgfplot_example_torus_tri.txt" and prints an example usage in LaTeX:

===========================================================================================
% pgfplot_example.tex

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{figure}[ht]
\begin{tikzpicture}
\begin{axis}[title=torus tri,
             xmin=-2.49, xmax= 2.50,
             ymin=-2.50, ymax= 2.50,
             zmin=-0.50, zmax= 0.50,
             xlabel={$x$},
             ylabel={$y$},
             zlabel={$z$},
             % xtick={0, 1, 2},
             % xticklabels={0, 1, 2},
             % ytick={0, 1},
             % yticklabels={0, 1},
             % ztick={0, 1},
             % zticklabels={0, 1},
             % axis equal,
             axis equal image,
             colorbar,
             colormap/hot, % hot, cool, bluered, greenyellow, redyellow, violet, blackwhite
             colorbar/width=10pt,
             view={30}{45},
             width=300pt,
             height=300pt,
             % axis line style={draw=none},
             % tick style={draw=none},
            ]
\addplot3[patch,
          patch type=triangle quadr,
          point meta=explicit,
          shader=faceted interp, % interp, faceted, faceted interp
          opacity=1.,
         ] table[x=x, y=y, z=z, meta=torus_tri] {./pgfplot_example_torus_tri.txt};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
===========================================================================================

Run:

lualatex -shell-escape pgfplot_example.tex

For more details, see:

https://pgfplots.sourceforge.net/gallery.html
http://www.bakoma-tex.com/doc/latex/pgfplots/pgfplots.pdf

Associated Pull Requests:

Fixes Issues:

Checklist for author:

  • I have linted the codebase (make lint in the firedrake source directory).
  • My changes generate no new warnings.
  • All of my functions and classes have appropriate docstrings.
  • I have commented my code where its purpose may be unclear.
  • I have included and updated any relevant documentation.
  • Documentation builds locally (make linkcheck; make html; make latexpdf in firedrake/docs directory)
  • I have added tests specific to the issues fixed in this PR.
  • I have added tests that exercise the new functionality I have introduced
  • Tests pass locally (pytest tests in the firedrake source directory) (useful, but not essential if you don't have suitable hardware).
  • I have performed a self-review of my own code using the below guidelines.

Checklist for reviewer:

  • Docstrings present
  • New tests present
  • Code correctly commented
  • No bad "code smells"
  • No issues in parallel
  • No CI issues (excessive parallelism/memory usage/time/warnings generated)
  • Upstream/dependent branches and PRs are ready

Feel free to add reviewers if you know there is someone who is already aware of this work.

Please open this PR initially as a draft and mark as ready for review once CI tests are passing.

@ksagiyam
Copy link
Contributor Author

mesh = TorusMesh(31, 13, 2.0, 0.5, quadrilateral=False)
x, y, z = SpatialCoordinate(mesh)
V = FunctionSpace(mesh, "CG", 2)
f = Function(V, name="torus_tri").interpolate(x)
pgfplot(f, "./pgfplot_example_torus_tri.txt", degree=2)

torus_tri.pdf

@ksagiyam
Copy link
Contributor Author

mesh = TorusMesh(31, 13, 2.0, 0.5, quadrilateral=True)
x, y, z = SpatialCoordinate(mesh)
V = FunctionSpace(mesh, "CG", 2)
f = Function(V, name="torus_quad").interpolate(x)
pgfplot(f, "./pgfplot_example_torus_quad.txt", degree=2)

torus_quad.pdf

@ksagiyam
Copy link
Contributor Author

mesh = UnitBallMesh(refinement_level=3)
x, y, z = SpatialCoordinate(mesh)
V = FunctionSpace(mesh, "CG", 2)
f = Function(V, name="ball").interpolate(x)
pgfplot(f, "./pgfplot_example_ball.txt", degree=2)

ball.pdf

@ksagiyam
Copy link
Contributor Author

ksagiyam commented May 19, 2023

mesh = BoxMesh(32, 16, 16, 2.0, 1.0, 1.0, hexahedral=True)
x, y, z = SpatialCoordinate(mesh)
V = FunctionSpace(mesh, "CG", 2)
r = sqrt(x**2 + y**2 + z**2)
f = Function(V, name="box").interpolate(sin(16 * r) / (r + 1.e-6))
pgfplot(f, "./pgfplot_example_box.txt", degree=2)

box.pdf

Copy link
Contributor

@connorjward connorjward left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Surely this should be part of firedrake.plot? Perhaps we should make firedrake.plot a subpackage. If we are careful with namespacing this shouldn't affect an end user at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Yes, we can.

return patches, patch_type


def pgfplot(f, filename, degree=1, complex_component='real', print_latex_example=True):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work in parallel? The docstring should probably make it clear either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Updated docstring.

@ksagiyam ksagiyam force-pushed the ksagiyam/pgfplot branch 2 times, most recently from bd25be3 to 1e84261 Compare May 19, 2023 18:00
@ksagiyam ksagiyam marked this pull request as ready for review May 19, 2023 18:04
Copy link
Member

@dham dham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. Just wow.

firedrake/plot.py Show resolved Hide resolved
@ksagiyam ksagiyam force-pushed the ksagiyam/pgfplot branch 2 times, most recently from baa6e75 to f8faef5 Compare May 19, 2023 22:03
@ksagiyam ksagiyam force-pushed the ksagiyam/pgfplot branch from f8faef5 to a7b97a8 Compare May 20, 2023 14:05
@dham dham merged commit ad6c0e4 into master May 31, 2023
@dham dham deleted the ksagiyam/pgfplot branch May 31, 2023 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants