#!/usr/bin/env python
# coding: utf-8

# # Three Quadratic Functions
# 
# Copyright (C) 2020 Andreas Kloeckner, 2021 Thomas Golecki
# 
# <details>
# <summary>MIT License</summary>
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# </details>

# Requirements to run this:
# 
# ```
# pip install ipywidgets pyvista[jupyter] trame
# ```
# (will need Jupyter server restart and browser hard-reload)
# 
# *Note:* This will not work in the web-based JupyterLab, because software.

# In[1]:


import numpy as np
import pyvista as pv
pv.set_jupyter_backend('trame')


# Consider the three equations:
# 
# $$y=x^2+\delta$$
# $$z=x^2-\delta$$
# $$y=z^2+\delta$$

# In[2]:


delta = 0.5


# In[3]:


pl = pv.Plotter()

res = 30

x, z = np.mgrid[-3:3:res*1j,-3:3:res*1j]
y = x**2 + delta

pl.add_mesh(pv.StructuredGrid(x, y, z))

if 1:
    y, x = np.mgrid[-3:3:res*1j,-3:3:res*1j]
    z = x**2 - delta

    pl.add_mesh(pv.StructuredGrid(x, y, z))

if 1:
    x, z = np.mgrid[-3:3:res*1j,-3:3:res*1j]
    y = z**2 + delta

    pl.add_mesh(pv.StructuredGrid(x, y, z))

pl.show()


# ### Fallback: Paraview

# In[10]:


get_ipython().system('rm -f 1.vts 2.vts 3.vts')


# In[11]:


from pyvisfile.vtk import write_structured_grid

res = 50

x, z = np.mgrid[-3:3:res*1j,-3:3:res*1j]
y = x**2 + delta
mesh = np.array([x, y, z])

write_structured_grid("1.vts", mesh.reshape(3, res, 1, res))

# ----------------------------------

y, x = np.mgrid[-3:3:res*1j,-3:3:res*1j]
z = x**2 - delta
mesh = np.array([x, y, z])

write_structured_grid("2.vts", mesh.reshape(3, res, res, 1))

# ----------------------------------

x, z = np.mgrid[-3:3:res*1j,-3:3:res*1j]
y = z**2 + delta
mesh = np.array([x, y, z])

write_structured_grid("3.vts", mesh.reshape(3, res, 1, res))


# In[ ]:




