FreeSurfaceSampler

These examples plot the evolution of the liquid-gas interface as a liquid column collapses and flows into the rest of the domain, and they show the velocity magnitude (in color) as a function of interface position. At each instant plotted, the shading of the line color represents the progression of time. Below, the scripts for processing the data in each available format (native, netcdf, and ascii) are shown, and these scripts are also available in the tools directory.

Note that the data accesses in the native and ascii examples can be applied to other sampler types because the format is identical. However, the NetCDF format can vary depending on the sampler type, and further detail is provided in that example.

The three examples will generate the same plot, shown here:

../_images/plot_sampling_freesurface.pdf

Native format: dam break example

To generate the data required to replicate this example, run the simulation contained in test/test_files/dam_break_godunov with the sampling format set to “native” and the sampling frequency (actually an interval) set to 30.

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt
import sys
AMR_WIND_PATH = '.'
sys.path.append(AMR_WIND_PATH+'/tools/')
import amrex_particle
from amrex_particle import AmrexParticleFile

loc_dir = "."
pp_dir = loc_dir + "/post_processing"

pfile = AmrexParticleFile(loc_dir)

nt = 5 + 1
out_int = 30

plt.figure()
base_color = 0.8

for n in range(nt):
    pt = pfile.load(n * out_int, root_dir = pp_dir)
    pt.parse_header()
    pt.load_binary_data()
    data = pt.df
    x_oo = data.xco
    z_oo = data.zco
    u_oo = data.velocityx
    v_oo = data.velocityy
    w_oo = data.velocityz
    # Reorder arrays
    ind_0 = np.argsort(x_oo) 
    x = np.zeros(np.shape(x_oo))
    z = np.zeros(np.shape(x_oo))
    Vmag = np.zeros(np.shape(x_oo))
    flag = np.zeros(np.shape(x_oo))
    for i in range(0, len(ind_0)): 
        x[i]= x_oo[ind_0[i]]
        z[i]= z_oo[ind_0[i]]
        Vmag[i] = np.sqrt(u_oo[ind_0[i]]**2+v_oo[ind_0[i]]**2+w_oo[ind_0[i]]**2)
        # When interface is not present, z location is set to zlo
        flag[i] = 1.0 if z[i] > 1e-8 else 0.
    # Shorten arrays (exclude points where interface not detected)
    nvalid = int(flag.sum())
    xshort = x[0:nvalid]
    zshort = z[0:nvalid]
    vshort = Vmag[0:nvalid]

    color = base_color - (base_color) * (n/nt)
    cstr = str(color)

    plt.plot(xshort,zshort,color=cstr)
    plt.scatter(xshort,zshort,c=vshort,cmap="jet",vmin=0.,vmax=2.)

plt.ylabel(r'$z$',fontsize=16)
plt.xlabel(r'$x$',fontsize=16)
plt.colorbar()
plt.savefig('plot_sampling_native.pdf',format='pdf',dpi=300,bbox_inches="tight")

NetCDF format: dam break example

To generate the data required to replicate this example, run the simulation contained in test/test_files/dam_break_godunov with the sampling format set to “netcdf” (AMR-Wind must also be compiled with NetCDF) and the sampling frequency (actually an interval) set to 30.

Because the FreeSurfaceSampler tracks the interface location, the sample points change position with time, and these changing positions are output as the “points” field. However, samplers that do not have moving sample locations do not have the “points” field in the NetCDF dataset. For static samplers, the locations are provided solely through the variable “coordinates”, which represents the initial positions of the sampler points. Otherwise, the data accesses in this example can be applied to other sampler types.

#!/usr/bin/env python3

import numpy as np
import netCDF4 as ncdf
import matplotlib.pyplot as plt

loc_dir = "."
pp_dir = loc_dir + "/post_processing"

nt = 5 + 1
out_int = 30

class SamplingFile(object):
    """Interface to Sampling NetCDF file"""

    def __init__(self, sampling_file = "sampling.nc", group_name = "s1"):
        """
        Args:
            stats_file (path): Absolute path to the NetCDF file
        """
        self.sampling_file = sampling_file
        self.sampling = ncdf.Dataset(self.sampling_file)
        self.fs = self.sampling["/"+group_name]
        self._points = self.fs.variables["points"][:,:,:]
        self._velocityx = self.fs.variables["velocityx"][:,:]
        self._velocityy = self.fs.variables["velocityy"][:,:]
        self._velocityz = self.fs.variables["velocityz"][:,:]

    @property
    def locations(self):
        return self._points

    @property
    def vmag(self):
        return np.sqrt(self._velocityx**2+self._velocityy**2+self._velocityz**2)

plt.figure()
base_color = 0.8

sampl = SamplingFile(sampling_file=pp_dir+"/sampling00000.nc", \
                    group_name = "fs")

for n in range(nt):
    x_oo=sampl.locations[n,:,:]
    Vmag_oo = sampl.vmag[n,:]

    # Reorder arrays
    ind_0 = np.argsort(x_oo[:,0]) 
    x = np.zeros(np.shape(x_oo)[0])
    z = np.zeros(np.shape(x_oo)[0])
    Vmag = np.zeros(np.shape(x_oo)[0])
    flag = np.zeros(np.shape(x_oo)[0])
    for i in range(0, len(ind_0)): 
        x[i]= x_oo[ind_0[i],0]
        z[i]= x_oo[ind_0[i],2]
        Vmag[i] = Vmag_oo[ind_0[i]]
        # When interface is not present, z location is set to 0
        flag[i] = 1.0 if z[i] > 1e-8 else 0.
    # Shorten arrays (exclude points where interface not detected)
    nvalid = int(flag.sum())
    xshort = x[0:nvalid]
    zshort = z[0:nvalid]
    vshort = Vmag[0:nvalid]

    color = base_color - (base_color) * (n/nt)
    cstr = str(color)

    plt.plot(xshort,zshort,color=cstr)
    plt.scatter(xshort,zshort,c=vshort,cmap="jet",vmin=0.,vmax=2.)

plt.ylabel('$z$',fontsize=16)
plt.xlabel('$x$',fontsize=16)
plt.colorbar()
plt.savefig('plot_sampling_netcdf.pdf',format='pdf',dpi=300,bbox_inches="tight")

ASCII format: dam break example

To generate the data required to replicate this example, run the simulation contained in test/test_files/dam_break_godunov with the sampling format set to “ascii” and the sampling frequency (actually an interval) set to 30.

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt

loc_dir = "."
pp_dir = loc_dir + "/post_processing"

nt = 5 + 1
out_int = 30

plt.figure()
base_color = 0.8

for n in range(nt):
    x_oo=np.genfromtxt(pp_dir+"/sampling"+str(n*out_int).zfill(5)+".txt",delimiter=' ',skip_header=5)
    # Reorder array
    ind_0 = np.argsort(x_oo[:,0]) 
    x = np.zeros(np.shape(x_oo))
    flag = np.zeros(np.shape(x_oo)[0])
    for i in range(0, len(ind_0)): 
        x[i,:]= x_oo[ind_0[i],:]
        # When interface is not present, z location is set to zlo
        flag[i] = 1.0 if x[i,2] > 1e-8 else 0.
  
    # Shorten arrays (exclude points where interface not detected)
    nvalid = int(flag.sum())
    # Columns are position (3), id, cpu, ints of struct (uid, sid, nid), field components
    xshort = x[0:nvalid,0]
    zshort = x[0:nvalid,2]
    vshort = np.sqrt(x[0:nvalid,8]**2+x[0:nvalid,9]**2+x[0:nvalid,10]**2)

    color = base_color - (base_color) * (n/nt)
    cstr = str(color)

    plt.plot(xshort,zshort,color=cstr)
    plt.scatter(xshort,zshort,c=vshort,cmap="jet",vmin=0.,vmax=2.)

plt.ylabel('$z$',fontsize=16)
plt.xlabel('$x$',fontsize=16)
plt.colorbar()
plt.savefig('plot_sampling_ascii.pdf',format='pdf',dpi=300,bbox_inches="tight")