def test_nested_processing_instructions(self): element = Element("<Element><?Target1?><?Target2 data?></Element>") element.parse_to_end({}) self.assertEqual("Target1", element.content[0].target) self.assertEqual("Target1", element.processing_instructions[0].target) self.assertEqual("Target2", element.content[1].target) self.assertEqual("Target2", element.processing_instructions[1].target)
def test_nested_elements(self): element = Element("<Element><SubElement><SubSubElement></SubSubElement></SubElement></Element>") element.parse_to_end({}) self.assertEqual("SubElement", element.content[0].name) self.assertEqual("SubElement", element.children[0].name) self.assertEqual("SubSubElement", element.content[0].content[0].name) self.assertEqual("SubSubElement", element.children[0].children[0].name)
def __calculate_property_for_integral_points(elem: Element, univ_el: UniversalElement, layer_info: dict, property_name: str) -> list: property_for_ip = [] # table to collect property value for integral point pos_x_for_ip = [] # table to store real x position of integral point x_pos, _ = elem.points_coordinates_vector( ) # getting x coordinates of element nodes # calculating global pos of integral points(here only x coord) for shape_fun_vector in univ_el.N: pos_x_for_ip.append( np.dot(shape_fun_vector, np.asarray([x_pos]).reshape(4, 1))[0]) # now we go thought each layer and checking if ip is in specified layer for integral_id in range(len(univ_el.N)): act_layer_border = 0. for i, [layer_thickness, layer_properties] in enumerate(layer_info): if layer_thickness + act_layer_border >= pos_x_for_ip[ integral_id]: # pos of first ip (most to left) property_for_ip.append(layer_properties[property_name]) break else: # element is in other layer, go next act_layer_border += layer_thickness return property_for_ip
def test_name_parsing(self): with self.subTest("Self-closing"): element = Element("<Element/> some text") element.parse_to_end({}) self.assertEqual("Element", element.name) with self.subTest("Open"): element = Element("<Element>some text</Element> some more text") element.parse_to_end({}) self.assertEqual("Element", element.name)
def test_disallowed_xml_name(self): with self.subTest("xml"): element = Element("<xmlElement/>") with self.assertRaises(XMLError): element.parse_to_end({}) with self.subTest("XmL"): element = Element("<XmLElement/>") with self.assertRaises(XMLError): element.parse_to_end({})
def __set_element(self, gd: GlobalData, nr: int): j = int(nr / (gd.nH - 1)) nodes_array = [ self.nodes[nr + j], self.nodes[nr + j + gd.nH], self.nodes[nr + j + gd.nH + 1], self.nodes[nr + j + 1] ] elem = Element(nr + 1, nodes_array) return elem
def test_disallowed_attribute_entity_replacement_character(self): with self.subTest("Disallow <"): entity = MockEntity("entity", expansion_text="<") element = Element("<Element attr='blah&entity;blah'/>") with self.assertRaises(XMLError): element.parse_to_end({"entity": entity}) with self.subTest("Allow <"): lt = MockEntity("lt", "<") element = Element("<Element attr='blah<blah'/>") element.parse_to_end({"lt": lt})
def test_nested_mixed(self): element = Element( "<Element>Some text<SubElement>More text</SubElement> <!-- comment --><?Target data?></Element>") element.parse_to_end({}) self.assertEqual("Some text", element.content[0].text) self.assertEqual("Some text", element.text[0].text) self.assertEqual("SubElement", element.content[1].name) self.assertEqual("SubElement", element.children[0].name) self.assertEqual("More text", element.content[1].text[0].text) self.assertEqual(" ", element.content[2].text) self.assertEqual(" ", element.text[1].text) self.assertEqual("Target", element.content[3].target) self.assertEqual("Target", element.processing_instructions[0].target) self.assertEqual(4, len(element.content))
def test_ends_parse_on_close(self): with self.subTest("Self-closing"): element = Element("<Element/> some text") unparsed_xml = element.parse_to_end({}) self.assertEqual(" some text", unparsed_xml) with self.subTest("Open"): element = Element("<Element>some text</Element> some more text") unparsed_xml = element.parse_to_end({}) self.assertEqual(" some more text", unparsed_xml)
def __init__(self, n_horizontal_nodes: int, n_vertical_nodes: int, gridWidth: float, gridHeight: float): # checking input if n_horizontal_nodes <= 1 or n_vertical_nodes <= 1: raise ValueError( "Grid must have positive numbers or vertical and horizontal nodes" ) #initializing class fields nElements = (n_horizontal_nodes - 1) * (n_vertical_nodes - 1) nNodes = n_horizontal_nodes * n_vertical_nodes self.__data = self.__grid_data(gridHeight, gridWidth, n_vertical_nodes, n_horizontal_nodes, \ nNodes, nElements) self.__nodes = [] self.__elements = [] element_width = gridWidth / (n_horizontal_nodes - 1) element_height = gridHeight / (n_vertical_nodes - 1) #creating list of nodes id = 1 for x in range(n_horizontal_nodes): for y in range(n_vertical_nodes): #border control if (x == 0 or x == n_horizontal_nodes - 1) or (y == 0 or y == n_vertical_nodes - 1): self.__nodes.append( Node(id, x * element_width, y * element_height, BC=True)) else: self.__nodes.append( Node(id, x * element_width, y * element_height, BC=False)) id += 1 #creating elements of 4 nodes elem_id = 1 for x in range(n_horizontal_nodes - 1): for y in range(n_vertical_nodes - 1): id = y + x * n_vertical_nodes node_list = [ self.__nodes[id], self.__nodes[n_vertical_nodes + id], self.__nodes[n_vertical_nodes + id + 1], self.__nodes[id + 1] ] self.__elements.append(Element(elem_id, 4, node_list)) elem_id += 1
def jacobian_matrix(self, element: Element, integral_point_id): jacobian = np.zeros(shape=(2, 2)) # creating matrix 2x2 from NumPy x_arr, y_arr = element.points_coordinates_vector( ) # creating list of nodes coordinates jacobian.itemset((0, 0), np.dot(self.__dN_dksi_matrix[integral_point_id], np.array([x_arr]).transpose())[0]) # dx_dksi jacobian.itemset((1, 0), np.dot(self.__dN_deta_matrix[integral_point_id], np.array([x_arr]).transpose())[0]) # dx_deta jacobian.itemset((1, 1), np.dot(self.__dN_deta_matrix[integral_point_id], np.array([y_arr]).transpose())[0]) # dy_deta jacobian.itemset((0, 1), np.dot(self.__dN_dksi_matrix[integral_point_id], np.array([y_arr]).transpose())[0]) # dy_dksi return jacobian
def test_disallowed_name_start_characters(self): for char in ["-", ".", "0", "7", "\u0305"]: with self.subTest(char): element = Element(f"<{char}xmlElement/>") with self.assertRaises(XMLError): element.parse_to_end({})
from classes.Element import Element from classes.Pool import Pool from classes.Species import Species from optimizer import Optimizer m0_H2O2 = Species([Element("H", 2), Element("O", 2)], mol=0) m0_H2O = Species([Element("H", 2), Element("O", 1)], mol=0) m0_CO2 = Species([Element("C"), Element("O", 2)], mol=0) m0_OH = Species([Element("O"), Element("H")], mol=0) m0_CO = Species([Element("C"), Element("O")], mol=0) CH4 = Species([Element("C"), Element("H", 4)]) O2 = Species([Element("O", 2)]) H2 = Species([Element("H", 2)]) pool_1 = Pool([O2, H2, m0_OH, m0_H2O, m0_H2O2, CH4, m0_CO, m0_CO2]) pool_2 = Pool([O2, H2, m0_H2O]) pool_3 = Pool([O2, m0_H2O, CH4, m0_CO2]) pools = [pool_1, pool_2, pool_3] pres = 100_000 t = 500 # Kelvin if __name__ == '__main__': for p in pools: print("Processing:", p) proj = Optimizer(p, t, pres) result = proj.minimize() print("\n" + "=" * 50 + "\n")
def test_mismatched_tags(self): element = Element("<Element1>some text</Element2> some more text") with self.assertRaises(XMLError): element.parse_to_end({})
def test_attribute_decimal_character_reference_replacement(self): for num, char in [(118, "v"), (279, "ė"), (632, "ɸ"), (986, "Ϛ"), (1948, "ޜ")]: with self.subTest(f"&#{num}; -> {char}"): element = Element(f"<Element attr='Entity &#{num}; text'/>") element.parse_to_end({}) self.assertEqual(f"Entity {char} text", element.attributes["attr"])
def test_attribute_hexadecimal_character_reference_replacement(self): for num, char in [("76", "v"), ("117", "ė"), ("278", "ɸ"), ("3da", "Ϛ"), ("79c", "ޜ")]: with self.subTest(f"&#{num}; -> {char}"): element = Element(f"<Element attr='Entity &#x{num}; text'/>") element.parse_to_end({}) self.assertEqual(f"Entity {char} text", element.attributes["attr"])
def test_attribute_missing_general_entity(self): element = Element("<Element attr='Entity &entity; text'/>") with self.assertRaises(XMLError): element.parse_to_end({})
def test_attribute_ignores_parameter_entity(self): element = Element("<Element attr='Entity %entity; text'/>") element.parse_to_end({}) self.assertEqual("Entity %entity; text", element.attributes["attr"])
def test_attribute_general_entity_replacement(self): entity = MockEntity("entity", expansion_text="ENTITY TEXT") element = Element("<Element attr='Entity &entity; text'/>") element.parse_to_end({"entity": entity}) self.assertEqual("Entity ENTITY TEXT text", element.attributes["attr"])
def test_multiple_attribute_parsing(self): element = Element("<Element attr1='Value1' attr2='Value2'/> some text") element.parse_to_end({}) self.assertEqual({"attr1": "Value1", "attr2": "Value2"}, element.attributes)
def test_attribute_parsing(self): with self.subTest("Self-closing - \' delimiters"): element = Element("<Element attr='Value'/> some text") element.parse_to_end({}) self.assertEqual({"attr": "Value"}, element.attributes) with self.subTest("Self-closing - \" delimiters"): element = Element("<Element attr=\"Value\"/> some text") element.parse_to_end({}) self.assertEqual({"attr": "Value"}, element.attributes) with self.subTest("Self-closing - with whitespace"): element = Element("<Element attr = \"Value\"/> some text") element.parse_to_end({}) self.assertEqual({"attr": "Value"}, element.attributes) with self.subTest("Open"): element = Element("<Element attr='Value'>some text</Element> some more text") element.parse_to_end({}) self.assertEqual({"attr": "Value"}, element.attributes)
def test_disallowed_attribute_characters(self): for char in ["<", "&", "\u0001", "\u0003", "\u0010", "\ufffe", "\uffff"]: with self.subTest(char): element = Element(f"<Element attr='Blah{char}'/>") with self.assertRaises(XMLError): element.parse_to_end({})
def test_nested_text(self): element = Element("<Element>Some text here</Element>") element.parse_to_end({}) self.assertEqual("Some text here", element.content[0].text) self.assertEqual("Some text here", element.text[0].text)
def test_disallowed_name_characters(self): for char in ["\u0001", "\u0003", "\u0010", "\ufffe", "\uffff"]: with self.subTest(char): element = Element(f"<{char}xmlElement/>") with self.assertRaises(XMLError): element.parse_to_end({})
def test_repeated_attribute(self): element = Element("<Element attr='value' attr='value'/>") with self.assertRaises(XMLError): element.parse_to_end({})