Exemplo n.º 1
0
 def update_E_PM(self,particles,just_return_dont_update=False):
     global counter
     try:
         counter==None
     except:
         counter=0
     counter+=1
     self.V_point_sources=0
     for particle in particles:
         i = int(round(particle.pos[0]*10**6))
         j = int(round(particle.pos[1]*10**6))
         """to do: still a bug with particles being outside the period...
         if not self.contains([particle]):
             print(self.particles_left.nnz,
                   self.particles_right.nnz,
                   self.particles_top.nnz,
                   self.particles_bottom.nnz)"""
         """the dielectric constant and minus sign are already done in Fenics"""
         self.V_point_sources += particle.charge*e*self.point_sources[i,j]
     tci = LinearTriInterpolator(self.triang_V,self.V_point_sources) # faster interpolator, but not as accurate
     (Ex, Ey) = tci.gradient(self.triang_V.x,self.triang_V.y)
     #V = tci(self.triang_V.x,self.triang_V.y)
     if just_return_dont_update:
         return -np.array([Ex,Ey])#*3e5
         #return np.array([V,V*0]) #for debugging
     else:
         self.E_point_sources = -np.array([Ex,Ey])#*3e5
Exemplo n.º 2
0
    def plot_solution(self, phi, ax=None, **kwargs):
        """Given the solution vector phi of values at the vertices, plot the solution"""
        intp = LinearTriInterpolator(self.triang, phi)
        x, y = self.tri['vertices'].T
        u, v = intp.gradient(x, y)

        ax = ax or plt.gca()
        ax.tripcolor(self.triang, phi)
        ax.quiver(x, y, u, v, **kwargs)
Exemplo n.º 3
0
def gradient(
    solution,
    variable,
    *,
    gradient_norm=True,
    plot=True,
    color_style: COLOR_STYLE_TYPE = "equal",
    mask=None,
    **kwargs
) -> Union[np.ndarray, Tuple[np.ndarray, np.ndarray]]:
    """Compute and plot the gradient of a scalar field.

    :param solution: The solution structure
    :param variable: Name of the variable
    :param gradient_norm: Should the norm be computed
    :param plot: Should the data be plotted
    :param color_style: How to symmetrize the colormap (False/None, 'full', 'equal')
    :param mask: Valid matplotlib mask see generate_mask
    :param kwargs:
    :return: Computed field as a scalar or vector field
    """
    mpl_kwargs, fish2fem_kwargs = split_mpl_kwargs(kwargs)
    triangles, data = generate_triangles_for_2d(
        solution, variable, mask=mask, **fish2fem_kwargs
    )
    topology = triangles.triangles
    geometry = list(zip(triangles.x, triangles.y))
    interpolator = LinearTriInterpolator(triangles, -data)
    (e_x, e_y) = interpolator.gradient(triangles.x, triangles.y)
    if gradient_norm:
        norm = field_norm(e_x, e_y)
        if plot:
            mesh_plot_2d(
                None,
                None,
                color_style=color_style,
                mask=mask,
                topology=topology,
                geometry=geometry,
                data=data,
                **kwargs
            )
        return norm
    else:
        plt.quiver(triangles.x, triangles.y, e_x, e_y, **mpl_kwargs)
        return e_x, e_y
Exemplo n.º 4
0
    def velocity_from_phi(self, phi, x=None, y=None):
        """Interpolate velocity solution in vertex positions

        Velocity solution is: u = \grad(\phi) + \Gamma u_pf
        where \phi is the solution of phi at the vertices, and u_pf
        the standard point vortex, with magnitude Gamma stored in the
        (N+1)-th entry of the  solution vector."""
        assert (x is None) == (
            y is None), "Provide either both or neither x and y arguments"
        if x is None:
            x, y = self.tri['vertices'].T

        N = len(phi) - 1
        # first normal interpolation phi gradient
        intp = LinearTriInterpolator(self.triang, phi[:N])
        u, v = intp.gradient(x, y)

        # then add point vortex solution
        Gamma = phi[-1]
        r2 = (x - self.x_c)**2 + y**2
        u += -y / r2 * Gamma / 2 / np.pi
        v += (x - self.x_c) / r2 * Gamma / 2 / np.pi
        return u, v
Exemplo n.º 5
0
    point_sources = pickle.load(f)

with open('smartwindows/Variables/voltages_with_glass.pkl', 'rb') as f:
    V1, V2, V3, V4 = pickle.load(f)

trifinder = triang.get_trifinder()

y_b = 0.0e-5
y_t = 5.0e-5
t = 1.5e-5
x_1 = 0.0
x_2 = 20e-5
b = 5e-5

tci = LinearTriInterpolator(triang, V1)
(Ex, Ey) = tci.gradient(triang.x, triang.y)
E = -np.array([Ex, Ey])

x = np.linspace(0, 20e-5, 200 / 4)
y = np.linspace(0 - 1.5e-5, 5e-5 + 1.5e-5, (50 + 2 * 15) / 4)
X, Y = np.meshgrid(x, y)


def get_field_here(field, pos: np.ndarray) -> np.ndarray:
    tr = trifinder(*pos)
    i = triang.triangles[tr]
    v0 = np.array([triang.x[i[0]], triang.y[i[0]]])
    v1 = np.array([triang.x[i[1]], triang.y[i[1]]])
    v2 = np.array([triang.x[i[2]], triang.y[i[2]]])
    norm = np.array([
        np.linalg.norm(v0 - pos),
Exemplo n.º 6
0
 def update_E_electrodes(self, x=[0,0,0,0]):
     self.V_electrodes = x[0]*self.V1 + x[1]*self.V2 + x[2]*self.V3 + x[3]*self.V4
     tci = LinearTriInterpolator(self.triang_V,self.V_electrodes) # faster interpolator, but not as accurate                             
     (Ex, Ey) = tci.gradient(self.triang_V.x,self.triang_V.y)
     self.E_electrodes = -np.array([Ex,Ey])
#####################
nitr = 5

for itr in range(nitr):
    nfname = 'dep_smooth_itr_' + str(100 + itr) + '.nc'
    os.system('cp -f ' + fname + '  ' + nfname)

os.system('echo  Log smoothing &> log.txt  ')

for itr in range(nitr):
    print '   > Calculate gradient ..', itr, nitr
    os.system('echo  ' + str(itr) + ' from ' + str(nitr) + ' >> log.txt  ')
    os.system('echo  "************************************" >> log.txt  ')

    tci = LinearTriInterpolator(tri, dep)
    (ddep_dx, ddep_dy) = tci.gradient(UTMx, UTMy)
    grad_dep = np.sqrt(ddep_dx**2 + ddep_dy**2)

    grad_lim = 0.35
    [ind] = np.where(grad_dep > grad_dep.max() * grad_lim)

    #######
    if vv:
        fig, ax = adcp.make_map(res='m')
        vmin = 0
        vmax = 0.5
        dv = (vmax - vmin) / 50.0
        levels = np.arange(vmin, vmax + dv, dv)
        cf1 = ax.tricontourf(
            trip, grad_dep, levels=levels, cmap=cmaps.jetMinWi,
            extend='both')  #,alpha=0.8)#extend='max' )  #,extend='both'