"""NRV-:class:`.material` handling."""importfaulthandlerimportosimportnumpyasnpfromscipy.constantsimportpi,epsilon_0from..backend._log_interfaceimportrise_warningfrom..backend._NRV_ClassimportNRV_classfrom..backend._parametersimportparameters# enable faulthandler to ease "segmentation faults" debugfaulthandler.enable()# get the built-in material librairydir_path=parameters.nrv_path+"/_misc"material_library=os.listdir(dir_path+"/materials/")################# Functions #################
[docs]defis_mat(mat):""" check if an object is a material, return True if yes, else False Parameters ---------- mat : object object to test Returns ------- bool True it the type is a material object """returnisinstance(mat,material)
[docs]defget_mat_file_as_dict(fname):""" Open .mat material librairy file and return all lines as a dictionnary Returns ------- d : dictionnary physical properties of the material """d={}withopen(fname,"r")asf:forlineinf:(key,value)=line.split()d[key]=valuereturnd
[docs]defload_material(f_material):""" Load a material by its name. if the name is in the NRV2 material librairy, the extention is automatically added. Parameters ---------- f_material : str either material name if the material is in the NRV2 Librairy or path to the corresponding\ .mat material file """# load from librairy or from filef_in_librairy=str(f_material)+".mat"iff_in_librairyinmaterial_library:mat_file=get_mat_file_as_dict(dir_path+"/materials/"+f_in_librairy)else:mat_file=get_mat_file_as_dict(f_material)# creat material instancemat_obj=material()if"name"inmat_file:mat_obj.set_name(mat_file["name"])if"source"inmat_file:mat_obj.set_source(mat_file["source"])if"sigma_xx"inmat_file:mat_obj.set_anisotropic_conductivity(mat_file["sigma_xx"],mat_file["sigma_yy"],mat_file["sigma_zz"])elif"sigma"inmat_file:mat_obj.set_isotropic_conductivity(mat_file["sigma"])else:rise_warning("loading a material with 0 conductivity, \ this may induce further division by 0")if"epsilon_r"inmat_file:mat_obj.set_permitivity(mat_file["epsilon_r"])returnmat_obj
[docs]defcompute_effective_conductivity(sigma:float,epsilon:float,freq:float)->float:r""" return the effective conductivity of the material. Two cases are psooible: - purely conductive material (when both `epsilon` or `freq` are set) .. math:: \sigma_{eff} = \sigma - dielectric material at a fixe frequency: .. math:: \sigma_{eff} = |\sigma+2j\pi f\epsilon_0\epsilon_r| Parameters ---------- sigma : float conductivity """ifepsilonisNoneorfreqisNone:returnsigmareturnabs(sigma+2j*pi*freq*epsilon_0*epsilon)
###################### material class ######################
[docs]classmaterial(NRV_class):""" a class for material, where all the physical properties constants are stored. by default materials in NRV are considerated as purely conductive. Yet, """
[docs]def__init__(self):""" material instantiation """super().__init__()self.name=""self.source=""# Conduction propertiesself.isotrop_cond=Trueself._sigma=0self._sigma_xx=0self._sigma_yy=0self._sigma_zz=0self._sigma_func=None# permitivity propertyself.epsilon=Noneself.freq=None
@propertydefsigma(self):""" get the conductivity of the material """ifnotself.is_isotropic():returnnp.array([self.sigma_xx,self.sigma_yy,self.sigma_zz])returncompute_effective_conductivity(sigma=self._sigma,epsilon=self.epsilon,freq=self.freq)@propertydefsigma_xx(self)->float:""" get the conductivity of the material along ox """ifself.isotrop_cond:rise_warning("Isotropic conductor sigma_xx is sigma")returnself.sigmareturncompute_effective_conductivity(sigma=self._sigma_xx,epsilon=self.epsilon,freq=self.freq)@propertydefsigma_yy(self)->float:""" get the conductivity of the material along oy """ifself.isotrop_cond:rise_warning("Isotropic conductor sigma_yy is sigma")returnself.sigmareturncompute_effective_conductivity(sigma=self._sigma_yy,epsilon=self.epsilon,freq=self.freq)@propertydefsigma_zz(self)->float:""" get the conductivity of the material along oz """ifself.isotrop_cond:rise_warning("Isotropic conductor sigma_zz is sigma")returnself.sigmareturncompute_effective_conductivity(sigma=self._sigma_zz,epsilon=self.epsilon,freq=self.freq)
[docs]defsave_material(self,save=False,fname="material.json"):rise_warning("save_material is a deprecated method use save")self.save(save=save,fname=fname)
[docs]defload_material(self,data="material.json"):rise_warning("load_material is a deprecated method use load")self.load(data=data)
[docs]defset_name(self,name:str)->None:""" set a name to a material Parameters ---------- name : str name of the material """self.name=name
[docs]defset_source(self,source:str)->None:""" set a source to a material. Note that this source is a string without spaces Parameters ---------- source : str scientific reference to the value, for clarity only """self.source=source
[docs]defset_isotropic_conductivity(self,sigma:float|complex)->None:""" set the conductivity for an isotropic material Parameters ---------- sigma : float conductivity in S/m """self.isotrop_cond=Trueifisinstance(sigma,complex):self._sigma=sigmaelse:self._sigma=float(sigma)
[docs]defset_anisotropic_conductivity(self,sigma_xx:float,sigma_yy:float,sigma_zz:float)->None:""" set the conductivity tensor for an anisotropic material Parameters ---------- sigma_xx : float conductivity along the x axis, in S/m sigma_yy : float conductivity along the y axis, in S/m sigma_zz : float conductivity along the z axis, in S/m """self.isotrop_cond=Falseself._sigma_xx=float(sigma_xx)self._sigma_yy=float(sigma_yy)self._sigma_zz=float(sigma_zz)
[docs]defis_isotropic(self)->bool:""" check that the material is isotropic or not, return true if isotropic, else false Returns ------- bool : True if the per """returnself.isotrop_cond
## Permitivity relatied methods
[docs]defset_permitivity(self,epsilon:float)->None:""" set the relative permitivity of the material. Note ---- The relative permetivity is used to compute an apparent conductivity at a frequency when ``freq``-attribute is set. Parameters ---------- epsilon : float relative permetivity in F/m """self.epsilon=float(epsilon)
[docs]defset_frequency(self,freq:float)->None:""" set the frequency of the electric field in the material Parameters ---------- freq : float frequency of the fields in to set in kHz """self.freq=float(freq)
[docs]defclear_frequency(self)->None:""" set the woking frequency to None Parameters ---------- freq : float frequency of the fields in to set in kHz """self.freq=None
[docs]defis_permitive(self):""" check if the material permitivity and the electric field frequency are set """returnself.epsilonisnotNoneandself.freqisnotNone