!pip install -I "phoebe>=2.0,<2.1"

"""
Explanation: 'mesh' Datasets and Options
Setup
Let's first make sure we have the latest version of PHOEBE 2.0 installed. (You can comment out this line if you don't use pip for your installation or don't want to update to the latest release).
End of explanation
"""


%matplotlib inline

import phoebe
from phoebe import u # units
import numpy as np
import matplotlib.pyplot as plt

logger = phoebe.logger()

b = phoebe.default_binary()

"""
Explanation: As always, let's do imports and initialize a logger and a new Bundle.  See Building a System for more details.
End of explanation
"""


ps, constraints = phoebe.dataset.mesh()
print ps

"""
Explanation: Dataset Parameters
Let's create the ParameterSet which would be added to the Bundle when calling add_dataset. Later we'll call add_dataset, which will create and attach this ParameterSet for us.
End of explanation
"""


print ps['times']

"""
Explanation: times
End of explanation
"""


ps_compute = phoebe.compute.phoebe()
print ps_compute

"""
Explanation: Compute Options
Let's look at the compute options (for the default PHOEBE 2 backend) that relate to meshes
End of explanation
"""


print ps_compute['mesh_method']

"""
Explanation: mesh_method
End of explanation
"""


print ps_compute['ntriangles']

"""
Explanation: The 'mesh_method' parameter determines how each component in the system is discretized into its mesh, but currently only has one option:
 * marching (default): this is the new method introduced in PHOEBE 2.  The star is discretized into triangles, with the attempt to make them each of equal-area and nearly equilateral.  Although not as fast as 'wd', this method is more robust and will always form a closed surface (when possible).
ntriangles
The 'ntriangles' parameter is only relevenat if mesh_method=='marching' (so will not be available unless that is the case).
End of explanation
"""


print ps_compute['gridsize']

"""
Explanation: gridsize
The 'gridsize' parameter is only relevant if mesh_method=='wd' (so will not be available unless that is the case).
End of explanation
"""


b.add_dataset('mesh', times=[0], dataset='mesh01')

b.run_compute()

b['mesh@model'].twigs

"""
Explanation: Synthetics
End of explanation
"""


print b['times@primary@mesh01@model']

"""
Explanation: Per-Mesh Parameters
End of explanation
"""


print b['volume@primary@mesh01@model']

print b['rpole@primary@mesh01@model']

print b['pot@primary@mesh01@model']

"""
Explanation: Per-Time Parameters
End of explanation
"""


print b['vertices@primary@mesh01@model']

print b['xs@primary@mesh01@model']

print b['rs@primary@mesh01@model']

print b['r_projs@primary@mesh01@model']

print b['cosbetas@primary@mesh01@model']

print b['normals@primary@mesh01@model']

print b['nxs@primary@mesh01@model']

print b['mus@primary@mesh01@model']

print b['vxs@primary@mesh01@model']

print b['areas@primary@mesh01@model']

print b['loggs@primary@mesh01@model']

print b['teffs@primary@mesh01@model']

print b['visibilities@primary@mesh01@model']

"""
Explanation: Per-Element Parameters
End of explanation
"""


axs, artists = b['mesh@model'].plot()

"""
Explanation: Plotting
By default, MESH datasets plot as 'ys' vx 'xs' (plane of sky) of just the surface elements, taken from the vertices vectors.
End of explanation
"""


axs, artists = b['mesh@model'].plot(facecolor='teffs', edgecolor=None)

"""
Explanation: Any of the 1-D fields (ie not vertices or normals) or matplotlib-recognized colornames can be used to color either the faces or edges of the triangles.  Passing none for edgecolor or facecolor turns off the coloring (you may want to set edgecolor=None if setting facecolor to disable the black outline).
End of explanation
"""


axs, artists = b['mesh@model'].plot(time=0.0, x='mus', y='teffs')

"""
Explanation: Alternatively, if you provide simple 1-D fields to plot, a 2D x-y plot will be created using the values from each element (always for a single time - if meshes exist for multiple times in the model, you must provide a single time either in the twig or as an argument to plot).
End of explanation
"""


axs, artists = b['mesh@model'].plot(x='times', y='rpole', marker='s')

"""
Explanation: The exception to needing to provide a time is for the per-time parameters mentioned above.  For these, time can be the x-array (not very exciting in this case with only a single time).
For more examples see the following:
- Passband Luminosity Tutorial
End of explanation
"""


