Skip to content
Snippets Groups Projects
Commit 7dae3d0d authored by Koerber, Lukas (FWIN-C) - 108045's avatar Koerber, Lukas (FWIN-C) - 108045
Browse files

Optimize imports.

parent 8500bb7d
Branches
Tags
No related merge requests found
import numpy as np
import k3d
from ..experiments.relax import minimize_gibbs_free_energy
from ..experiments.eigen import calculate_normal_modes
from ..experiments.calculate_absorption import calculate_absorption
from ..helpers.math import h_U, h_hom, h_CPW, h_strip
import numpy as np
import pygmsh
from ..experiments.calculate_absorption import calculate_absorption
from ..experiments.relax import minimize_gibbs_free_energy
from ..helpers.math import h_hom, h_CPW, h_strip
B_ext_color = 0xED4B42
......@@ -45,8 +45,6 @@ class ExperimentalSetup:
else:
raise ValueError(f'Input {value} is not of type "MicrowaveAntenna"')
def show(self, comp="vec", scale=5, show_antenna=True):
plot = k3d.plot()
plt_mesh = k3d.mesh(np.float32(self.sample.mesh.points), np.uint32(self.sample.mesh.get_cells_type("triangle")))
......@@ -62,13 +60,23 @@ class ExperimentalSetup:
hz = self._Bext.T[2]
colors = np.repeat(np.array([(np.uint32(B_ext_color), np.uint32(B_ext_color))]), self.sample.nx)
if comp == "vec":
m_mesh = k3d.vectors(np.float32(self.sample.mesh.points), np.float32(scale*(np.vstack([hx, hy, hz] )).T), head_size=2.5*scale,line_width=0.05*scale,colors=colors)
m_mesh = k3d.vectors(np.float32(self.sample.mesh.points),
np.float32(scale * (np.vstack([hx, hy, hz])).T), head_size=2.5 * scale,
line_width=0.05 * scale, colors=colors)
else:
comp_dict = {"x": hx, "y": hy, "z" : hz, "phi" : np.arctan2(hy,hx), "abs" : np.sqrt(hx**2 + hy**2 + hz**2)}
color_dict = {"abs" : k3d.colormaps.matplotlib_color_maps.Inferno,"x": k3d.colormaps.matplotlib_color_maps.RdBu, "y": k3d.colormaps.matplotlib_color_maps.RdBu, "z" : k3d.colormaps.matplotlib_color_maps.RdBu, "phi" : k3d.colormaps.matplotlib_color_maps.Twilight_shifted}
comp_dict = {"x": hx, "y": hy, "z": hz, "phi": np.arctan2(hy, hx),
"abs": np.sqrt(hx ** 2 + hy ** 2 + hz ** 2)}
color_dict = {"abs": k3d.colormaps.matplotlib_color_maps.Inferno,
"x": k3d.colormaps.matplotlib_color_maps.RdBu,
"y": k3d.colormaps.matplotlib_color_maps.RdBu,
"z": k3d.colormaps.matplotlib_color_maps.RdBu,
"phi": k3d.colormaps.matplotlib_color_maps.Twilight_shifted}
# TODO check if comp is valid key
m_mesh = k3d.mesh(np.float32(self.sample.mesh.points), np.uint32(self.sample.mesh.get_cells_type("triangle")),attribute=comp_dict[comp],color_map=color_dict[comp],interpolation=False, side="double", flat_shading=True, name="$m_{{}}$".format(comp))
m_mesh = k3d.mesh(np.float32(self.sample.mesh.points),
np.uint32(self.sample.mesh.get_cells_type("triangle")), attribute=comp_dict[comp],
color_map=color_dict[comp], interpolation=False, side="double", flat_shading=True,
name="$m_{{}}$".format(comp))
plot += m_mesh
else:
......@@ -81,7 +89,8 @@ class ExperimentalSetup:
if self._antenna is not None and show_antenna:
span = np.max(np.abs(self.sample.xyz.T[0])) * 2
antenna_mesh = self._antenna.get_mesh(span)
plot += k3d.mesh(np.float32(antenna_mesh.points), np.uint32(antenna_mesh.get_cells_type("triangle")),color=0xFFD700, side="double")
plot += k3d.mesh(np.float32(antenna_mesh.points), np.uint32(antenna_mesh.get_cells_type("triangle")),
color=0xFFD700, side="double")
plot.display()
......@@ -90,13 +99,16 @@ class ExperimentalSetup:
if self.sample.mag is None:
print("Your sample does not have a magnetization field yet.")
else:
self.sample.mag = minimize_gibbs_free_energy(self.sample, self._Bext, tol_cg=tol,return_last=return_last, continue_least_with_squares=continue_least_with_squares)
self.sample.mag = minimize_gibbs_free_energy(self.sample, self._Bext, tol_cg=tol, return_last=return_last,
continue_least_with_squares=continue_least_with_squares)
def eigenmodes(self,kmin=-40e6, kmax=40e6, Nk=81, num_modes=20, k=None, no_dip=False,num_cpus=1, save_modes=True,save_local=False, save_magphase=False):
def eigenmodes(self, kmin=-40e6, kmax=40e6, Nk=81, num_modes=20, k=None, no_dip=False, num_cpus=1, save_modes=True,
save_local=False, save_magphase=False):
if self.sample.mag is None:
print("Your sample does not have a magnetization field yet.")
else:
dispersion = self.sample.eigensolver(self.sample,self.Bext, self.name, kmin, kmax, Nk, num_modes, k, no_dip,num_cpus,save_modes,save_local, save_magphase)
dispersion = self.sample.eigensolver(self.sample, self.Bext, self.name, kmin, kmax, Nk, num_modes, k,
no_dip, num_cpus, save_modes, save_local, save_magphase)
self._modes_available = save_modes
self.dispersion = dispersion
return dispersion
......@@ -114,7 +126,8 @@ class ExperimentalSetup:
print("Calculating eigenmodes first.")
self.dispersion = self.eigenmodes(save_modes=True, k=k)
print("Calculating absorption.")
power_map = calculate_absorption(self.sample, self.name, self.dispersion, self._antenna, fmin, fmax,Nf,k)
power_map = calculate_absorption(self.sample, self.name, self.dispersion, self._antenna, fmin, fmax, Nf,
k)
print("Done.")
return power_map
if self._modes_available is True and auto_eigenmodes is True:
......@@ -122,7 +135,8 @@ class ExperimentalSetup:
if self._modes_available is True and auto_eigenmodes is False:
print("Calculating absorption.")
power_map = calculate_absorption(self.sample, self.name, self.dispersion, self._antenna, fmin, fmax,Nf,k)
power_map = calculate_absorption(self.sample, self.name, self.dispersion, self._antenna, fmin, fmax, Nf,
k)
print("Done.")
return power_map
......@@ -173,6 +187,7 @@ class StriplineAntenna(MicrowaveAntenna):
antenna_field_func = lambda k: np.array([np.zeros_like(x_), antenna_func(k), antenna_func(k)]).flatten()
return antenna_field_func
class CPWAntenna(MicrowaveAntenna):
def __init__(self, width, gap, spacing):
......@@ -251,5 +266,6 @@ class CurrentLoopAntenna(MicrowaveAntenna):
# antenna_field_func = lambda k: np.array([np.zeros_like(x_), np.zeros_like(x_), antenna_func(k)]).flatten()
antenna_field_func = lambda k: np.array([antenna_func(k)*np.cos(phi_), antenna_func(k)*np.sin(phi_), antenna_func(k)]).flatten()
antenna_field_func = lambda k: np.array(
[antenna_func(k) * np.cos(phi_), antenna_func(k) * np.sin(phi_), antenna_func(k)]).flatten()
return antenna_field_func
from scipy.sparse.linalg import spsolve, LinearOperator, gmres, lgmres, cgs
from scipy.sparse import csr_matrix, dia_matrix, coo_matrix, identity, csc_matrix, diags, bmat
import numpy as np
from scipy.constants import mu_0
import logging
from scipy.constants import mu_0
from scipy.sparse import csr_matrix, diags
from scipy.sparse.linalg import spsolve, LinearOperator, lgmres
from .fem_core.cythoncore import computedensematrix_kdep_py
from ..helpers.math import *
......@@ -171,7 +171,8 @@ class UniAxialAnisotropyOperator(LinearOperator):
# TODO this could be faster by switching the order of things
e_u_normalized = normalize_single_3d_vec(np.array([e_u for i in range(self.nx)])).T.flatten()
sparse_mat += flattened_mesh_vec_tensor_product(-2 * Ku1_array / (mu_0 * self.Msat ** 2) * e_u_normalized, e_u_normalized)
sparse_mat += flattened_mesh_vec_tensor_product(-2 * Ku1_array / (mu_0 * self.Msat ** 2) * e_u_normalized,
e_u_normalized)
return sparse_mat
......
import numpy as np
import meshio
import k3d
from abc import ABC, abstractmethod
import k3d
import meshio
import numpy as np
from .fem_core.cythoncore import get_matrices
from .operators import ExchangeOperator, DipolarOperator, UniAxialAnisotropyOperator, BulkDMIOperator, \
InterfacialDMIOperator
from ..helpers.math import normalize_single_3d_vec
from ..helpers.io import get_mesh_dimension
from ..helpers.io import check_mesh_shape
from ..experiments.eigen import calculate_normal_modes, not_implemented_eigensolver
from ..helpers.io import check_mesh_shape
from ..helpers.io import get_mesh_dimension
from ..helpers.math import normalize_single_3d_vec
class AbstractSample(ABC):
......@@ -65,7 +67,6 @@ class AbstractSample(ABC):
print("Setting geometry and calculating discretized differential operators on mesh.")
self.mesh = mesh
self.xyz = self.mesh.points
self.nx = len(self.xyz)
......@@ -152,7 +153,6 @@ class FerromagnetMixin:
"mesh shape: {}\n".format(self.xyz.shape) + \
"value shape: {}\n".format(np.shape(value)))
def get_magnetic_tensors(self):
self.N_exc = ExchangeOperator(self)
self.N_dip = DipolarOperator(self)
......@@ -303,6 +303,7 @@ class AntiferromagnetMixin:
def show(self):
pass
class PropagatingWaveEigensolverMixin:
def get_eigensolver(self):
self.eigensolver = calculate_normal_modes
......@@ -312,6 +313,7 @@ class ConfinedWaveEigensolverMixin:
def get_eigensolver(self):
self.eigensolver = not_implemented_eigensolver
class ConfinedSampleFM(ConfinedWaveEigensolverMixin, FerromagnetMixin, AbstractSample):
def description(self):
print("I am a ferromagnetic confined sample.")
......
import multiprocessing as mp
import os
from functools import partial
import meshio
import numpy as np
from scipy.sparse import csr_matrix, dia_matrix, coo_matrix, identity, csc_matrix
from scipy.sparse.linalg import LinearOperator, eigsh, aslinearoperator, lgmres, spilu, eigs
from scipy.constants import mu_0
from scipy.optimize import minimize_scalar
import matplotlib
matplotlib.use('Agg')
from scipy.linalg import block_diag
import matplotlib.pyplot as plt
import argparse
import subprocess
import logging
from pathlib import Path
#from tetraeigen_cy import DemagOperator, call_eigensolver
import pandas as pd
import glob
import os
import time
import tqdm
from functools import partial, partialmethod
import multiprocessing as mp
import logging
import meshio
from scipy.constants import mu_0
from scipy.sparse import csc_matrix
from scipy.sparse.linalg import spilu, eigs
from ..core.operators import cross_operator, TotalDynMat, SuperLUInv, InvTotalDynMat
from ..core.fem_core.cythoncore import rotation_matrix_py
from ..core.operators import cross_operator, TotalDynMat, SuperLUInv, InvTotalDynMat
from ..helpers.math import flattened_mesh_vec_scalar_product, diag_matrix_from_vec
......
import time
import numpy as np
from scipy.optimize import minimize
from scipy.constants import mu_0
import time
from ..helpers.math import flattened_mesh_vec_scalar_product, spherical_angles_to_mesh_vector
from scipy.optimize import minimize
from scipy.sparse import csr_matrix
from ..helpers.math import flattened_mesh_vec_scalar_product, spherical_angles_to_mesh_vector
def not_implemented_minimizer(sample, Bext, tol_cg, return_last=False, continue_least_with_squares=False):
print("This energy minimizer will be implemented in a future release.")
def energy_per_length_spherical(mag_spherical, nx, dA, h_ext, N, N_cub):
# TODO include faster scalar product!
......@@ -70,8 +73,6 @@ def jac_spherical(mag_spherical, nx, dA, h_ext, N, N_cub):
def minimize_gibbs_free_energy(sample, Bext, tol_cg, return_last=False, continue_least_with_squares=False):
# read_tmv_from_project("area.bin", conf)
nx = sample.nx
# area associated with each node
......@@ -99,7 +100,6 @@ def minimize_gibbs_free_energy(sample, Bext,tol_cg, return_last=False, continue_
fac = (sample.Msat * sample.scale) ** 2 * mu_0
# tol_cg = 1e-12
print_current_spherical = lambda x: print(
"Current energy length density: {:.15e} J/m mx = {:.2f} my = {:.2f} mz = {:.2f}\r".format(
(energy_per_length_spherical(x, nx, dA, h_ext, N_tot, N_cub) * fac),
......@@ -111,7 +111,8 @@ def minimize_gibbs_free_energy(sample, Bext,tol_cg, return_last=False, continue_
print(f"Minimizing in using '{starting_method}' (tolerance {tol_cg}) ...")
t_start = time.time()
result = minimize(energy_per_length_spherical, m_ini_spherical, (nx, dA, h_ext, N_tot, N_cub), jac=jac_spherical,
callback=print_current_spherical, method=starting_method, options={"ftol": tol_cg, "gtol": tol_cg})
callback=print_current_spherical, method=starting_method,
options={"ftol": tol_cg, "gtol": tol_cg})
t_end = time.time()
if result.success:
theta = result.x[:nx]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment