"""Contains the material class."""frompygaps.dataimportMATERIAL_LISTfrompygaps.utilities.exceptionsimportParameterError
[docs]classMaterial():""" An unified descriptor for an adsorbent material. Parameters ---------- name : str The name of the material. Other Parameters ---------------- density : float Material density. molar_mass : float Material molar mass. Notes ----- The members of the properties are left at the discretion of the user. There are, however, some unique properties which can be set. """# special reserved parameters_reserved_params=["name","density","molar_mass",]def__init__(self,name:str,store:bool=False,**properties,):"""Instantiate by passing all the parameters."""# Material nameself.name=name# Rest of material propertiesself.properties=properties# Store reference in internal listifstore:ifselfnotinMATERIAL_LIST:MATERIAL_LIST.append(self)def__repr__(self):"""Print material id."""returnf"<pygaps.Material '{self.name}'>"def__str__(self):"""Print material standard name."""returnself.namedef__hash__(self):"""Override hashing as a hash of name."""returnhash(self.name)def__eq__(self,other):"""Overload equality operator to use name."""ifisinstance(other,Material):returnself.name==other.namereturnself.name==otherdef__add__(self,other):"""Overload addition operator to use name."""returnself.name+otherdef__radd__(self,other):"""Overload rev addition operator to use name."""returnother+self.name
[docs]defprint_info(self):"""Print a short summary of all the material parameters."""string=f"pyGAPS Material: '{self.name}'\n"ifself.properties:string+="Other properties: \n"forprop,valinself.properties.items():string+=(f"\t{prop}: {str(val)}\n")print(string)
[docs]@classmethoddeffind(cls,name:str):"""Get the specified material from the master list. Parameters ---------- name : str The name of the material to search. Returns ------- Material Instance of class. Raises ------ ``ParameterError`` If it does not exist in list. """# Skip search if already materialifisinstance(name,Material):returnnameifnotisinstance(name,str):raiseParameterError("Pass a string as an material name.")# Checks to see if material exists in master listtry:returnnext(matformatinMATERIAL_LISTifname==mat)exceptStopIterationaserr:raiseParameterError(f"Material {name} does not exist in list of materials. ""First populate pygaps.MATERIAL_LIST with required material class")fromerr
[docs]defto_dict(self)->dict:""" Return a dictionary of the material class. Is the same dictionary that was used to create it. Returns ------- dict Dictionary of all parameters. """parameters_dict={'name':self.name}parameters_dict.update(self.properties)returnparameters_dict
@propertydefdensity(self)->float:"""Material bulk density, in g/cm3 (optional)."""returnself.properties.get("density")@density.setterdefdensity(self,val:float):ifval:self.properties["density"]=float(val)@propertydefmolar_mass(self)->float:"""Material molar mass, in g/mol (optional)."""returnself.properties.get("molar_mass")@molar_mass.setterdefmolar_mass(self,val:float):ifval:self.properties["molar_mass"]=float(val)
[docs]defget_prop(self,prop:str):""" Return a property from the internal dictionary. Parameters ---------- prop : str Property name desired. Returns ------- str/float Value of property in the properties dict. Raises ------ ``ParameterError`` If it does not exist. """req_prop=self.properties.get(prop)ifreq_propisNone:try:req_prop=getattr(self,prop)exceptAttributeErrorasexc:raiseParameterError(f"Material '{self.name}' does not have a property named '{prop}'.")fromexcreturnreq_prop