@@ -94,17 +94,8 @@ def __init__(self, composition_dictionary, unit_type="mass", normalize=False):
9494 input_dictionary [k ] = composition_dictionary [k ] / n_total
9595
9696 # Break component formulae into atomic dictionaries
97- self .component_formulae = {
98- c : dictionarize_formula (c ) for c in composition_dictionary .keys ()
99- }
100-
101- # Create lists of elemental compositions of components
102- self .element_list = OrderedCounter ()
103- for component in self .component_formulae .values ():
104- self .element_list += OrderedCounter (
105- {element : n_atoms for (element , n_atoms ) in component .items ()}
106- )
107- self .element_list = list (self .element_list .keys ())
97+ fl = self ._component_formulae_and_element_lists (composition_dictionary .keys ())
98+ self .component_formulae , self .element_list = fl
10899
109100 if unit_type == "mass" or unit_type == "weight" :
110101 self .mass_composition = input_dictionary
@@ -116,6 +107,30 @@ def __init__(self, composition_dictionary, unit_type="mass", normalize=False):
116107 "Should be either mass, weight or molar."
117108 )
118109
110+ def _component_formulae_and_element_lists (self , component_strings ):
111+ """
112+ Converts a list of components in string form into a list of components in dictionary form
113+ and a list of elements.
114+
115+ :param component_strings: A list of strings containing a formula for each component
116+ :type component_strings: list of strings
117+ :return: a list of components in dictionary form and a list of elements.
118+ :rtype: _type_
119+ """
120+
121+ # Break component formulae into atomic dictionaries
122+ component_formulae = {c : dictionarize_formula (c ) for c in component_strings }
123+
124+ # Create lists of elemental compositions of components
125+ element_list = OrderedCounter ()
126+ for component in component_formulae .values ():
127+ element_list += OrderedCounter (
128+ {element : n_atoms for (element , n_atoms ) in component .items ()}
129+ )
130+ element_list = list (element_list .keys ())
131+
132+ return component_formulae , element_list
133+
119134 def renormalize (self , unit_type , normalization_component , normalization_amount ):
120135 """
121136 Change the total amount of material in the composition
@@ -191,6 +206,14 @@ def change_component_set(self, new_component_list):
191206 :param new_component_list: New set of basis components.
192207 :type new_component_list: list of strings
193208 """
209+
210+ old_element_list = self .element_list
211+ _ , new_element_list = self ._component_formulae_and_element_lists (
212+ new_component_list
213+ )
214+
215+ self .element_list = list (set (old_element_list ).union (set (new_element_list )))
216+
194217 composition = np .array (
195218 [self .atomic_composition [element ] for element in self .element_list ]
196219 )
@@ -218,6 +241,24 @@ def change_component_set(self, new_component_list):
218241 # Reinitialize the object
219242 self .__init__ (composition , "molar" )
220243
244+ def remove_null_components (self , tol = 1.0e-12 ):
245+ """
246+ Removes components with absolute concentrations less than a given tolerance.
247+
248+ :param tol: zero tolerance, defaults to 1.e-12
249+ :type tol: float, optional
250+ """
251+ c = deepcopy (self .molar_composition )
252+ del_keys = []
253+ for key , value in c .items ():
254+ if np .abs (value ) < tol :
255+ del_keys .append (key )
256+ for key in del_keys :
257+ c .pop (key )
258+
259+ # Reinitialize the object
260+ self .__init__ (c , "molar" )
261+
221262 def _mole_to_mass_composition (self , molar_comp ):
222263 """
223264 Hidden function to returns the mass composition as a counter [kg]
0 commit comments