def write_bdf(self, f, size=8, is_double=False): #nids = ['%8i' % node_id for node_id in self.node_id] #temps = [print_float_8(tempi) % temp for temp in self.temperature] nleftover = self.n // 3 * 3 if size == 8: for i in range(0, self.n, 3): f.write( 'TEMP %8i%8i%s %8i%s%8i%s' % (self.temp_id, self.node_id[i], print_float_8(temps[i]), self.node_id[i + 1], print_float_8(temps[i + 1]), self.node_id[i + 2], print_float_8(temps[i + 2]))) for i in range(nleftover, self.n - nleftover): f.write('TEMP %8i%s' % (self.temp_id, print_float_8(temps[i]))) if self.is_default: f.write('TEMPD %8i%s\n' % (self.temp_id, print_float_8(self.default))) else: for i in range(0, self.n, 3): f.write( 'TEMP* %16i%16i%s\n' % (self.temp_id, self.node_id[i], print_float_16(temps[i]))) f.write('%8s%16i%s%16i%s' % ('* ', self.node_id[i + 1], print_float_16(temps[i + 1]), self.node_id[i + 2], print_float_16(temps[i + 2]))) for i in range(nleftover, self.n - nleftover): f.write( 'TEMP* %16i%16i%s\n*\n' % (self.temp_id, self.node_id[i], print_float_16(temps[i]))) if self.is_default: f.write('TEMPD* %16i%s\n' % (self.temp_id, print_float_16(self.default)))
def write_bdf(self, f, size=8, is_double=False): #nids = ['%8i' % node_id for node_id in self.node_id] #temps = [print_float_8(tempi) % temp for temp in self.temperature] nleftover = self.n // 3 * 3 if size == 8: for i in range(0, self.n, 3): f.write('TEMP %8i%8i%s %8i%s%8i%s' % (self.temp_id, self.node_id[i], print_float_8(temps[i]), self.node_id[i+1], print_float_8(temps[i+1]), self.node_id[i+2], print_float_8(temps[i+2]))) for i in range(nleftover, self.n - nleftover): f.write('TEMP %8i%s' % (self.temp_id, print_float_8(temps[i]))) if self.is_default: f.write('TEMPD %8i%s\n' % (self.temp_id, print_float_8(self.default))) else: for i in range(0, self.n, 3): f.write('TEMP* %16i%16i%s\n' % (self.temp_id, self.node_id[i], print_float_16(temps[i]))) f.write('%8s%16i%s%16i%s' % ('* ', self.node_id[i+1], print_float_16(temps[i+1]), self.node_id[i+2], print_float_16(temps[i+2]))) for i in range(nleftover, self.n - nleftover): f.write('TEMP* %16i%16i%s\n*\n' % (self.temp_id, self.node_id[i], print_float_16(temps[i]))) if self.is_default: f.write('TEMPD* %16i%s\n' % (self.temp_id, print_float_16(self.default)))
def write_bdf(self, f, size=8, is_double=False): if self.n: if size == 8: for load_id, element_id, tbar, tprime in zip( self.load_id, self.element_id, self.tbar, self.tprime): f.write('TEMPP1 %8i%8i%s%s\n' % (load_id, element_id, print_float_8(tbar), print_float_8(tprime))) else: for load_id, element_id, tbar, tprime in zip( self.load_id, self.element_id, self.tbar, self.tprime): f.write('TEMPP1* %16i%16i%s%s\n*\n' % (load_id, element_id, print_float_16(tbar), print_float_16(tprime)))
def write_bdf2(self, size=8, is_double=False): """ The writer method used by BDF.write_bdf :param self: the GRID object pointer :param size: the size of the card (8/16) :type size: int :param isdouble: should this card be written with double precision (default=False) :type is_double: bool """ xyz = self.xyz if size == 8: cp = set_string8_blank_if_default(self.Cp(), 0) cd = set_string8_blank_if_default(self.Cd(), 0) seid = set_string8_blank_if_default(self.SEid(), 0) msg = '%-8s%8i%8s%s%s%s%s%8s%s\n' % ('GRID', self.nid, cp, print_float_8(xyz[0]), print_float_8(xyz[1]), print_float_8(xyz[2]), cd, self.ps, seid) else: cp = set_string16_blank_if_default(self.Cp(), 0) cd = set_string16_blank_if_default(self.Cd(), 0) seid = set_string16_blank_if_default(self.SEid(), 0) if double: msg = ('%-8s%16i%16s%16s%16s\n' '%-8s%16s%16s%16s%16s\n' % ('GRID*', self.nid, cp, print_scientific_double(xyz[0]), print_scientific_double(xyz[1]), '*', print_scientific_double(xyz[2]), cd, self.ps, seid)) else: msg = ('%-8s%16i%16s%16s%16s\n' '%-8s%16s%16s%16s%16s\n' % ('GRID*', self.nid, cp, print_float_16(xyz[0]), print_float_16(xyz[1]), '*', print_float_16(xyz[2]), cd, self.ps, seid)) return self.comment() + msg.rstrip() + '\n'
def write_bdf2(self, size=8, double=False): if size == 8: msg = 'MPC %8s' % self.conid for (i, grid, component, enforced) in zip(count(), self.gids, self.constraints, self.enforced): msg += '%8i%8s%8s' % (grid, component, print_float_8(enforced)) if i % 2 == 1 and i > 0: msg += '\n%8s%8s' % ('', '') elif size == 16: # TODO: we're sure MPCs support double precision? msg = 'MPC* %16s' % self.conid if double: for (i, grid, component, enforced) in zip(count(), self.gids, self.constraints, self.enforced): msg += '%16i%16s%16s' % (grid, component, print_scientific_double(enforced)) if i % 2 == 1 and i > 0: msg += '\n%8s%16s' % ('*', '') else: for (i, grid, component, enforced) in zip(count(), self.gids, self.constraints, self.enforced): msg += '%16i%16s%16s' % (grid, component, print_float_16(enforced)) if i % 2 == 1 and i > 0: msg += '\n%8s%16s' % ('*', '') return self.comment() + msg.rstrip() + '\n'
def write_bdf(self, f, size=8, is_double=False): if self.n: if size == 8: for load_id, element_id, tbar, tprime in zip(self.load_id, self.element_id, self.tbar, self.tprime): f.write('TEMPP1 %8i%8i%s%s\n' % (load_id, element_id, print_float_8(tbar), print_float_8(tprime))) else: for load_id, element_id, tbar, tprime in zip(self.load_id, self.element_id, self.tbar, self.tprime): f.write('TEMPP1* %16i%16i%s%s\n*\n' % (load_id, element_id, print_float_16(tbar), print_float_16(tprime)))
def test_floats_positive_8(self): # ideal #self.assertEqual(print_float_8(-.003607), '-.003607', # print_float_8(-.003607)) # actual self.assertEqual(print_float_8(-.003607), '-3.607-3', print_float_8(-.003607)) # good self.assertEqual(print_float_8(1.2), ' 1.2', print_float_8(1.2)) self.assertEqual(print_float_8(0.5), ' .5', print_float_8(0.5)) self.assertEqual(print_float_8(-0.5), ' -.5', print_float_8(-0.5)) self.assertEqual(print_field(1.2), ' 1.2', 'a') self.assertEqual(print_field(1.23456789), '1.234568', 'b') self.assertEqual(print_field(12.234568), '12.23457', 'c') self.assertEqual(print_field(123.23457), '123.2346', 'd') self.assertEqual(print_field(1234.23468), '1234.235', 'e') self.assertEqual(print_field(12345.238), '12345.24', 'f') self.assertEqual(print_field(123456.28), '123456.3', 'g') self.assertEqual(print_field(1234567.25), '1234567.', print_field(1234567.25)) # 1.2346+6 self.assertEqual(print_field(12345678.), '1.2346+7', '%r' % print_field(12345678.)) self.assertEqual(print_field(123456789.), '1.2346+8', '%r' % print_field(12345678.)) self.assertEqual(print_field(0.1), ' .1', 'A|%s|' % print_field(0.1)) self.assertEqual(print_field(0.0001), ' .0001', 'B|%s|' % print_field(0.0001)) self.assertEqual(print_field(0.00001), ' .00001', 'C|%s|' % print_field(0.00001)) self.assertEqual(print_field(0.000001), ' .000001', 'D|%s|' % print_field(0.000001)) self.assertEqual(print_field(0.0000001), '.0000001', 'E|%s|' % print_field(0.0000001)) self.assertEqual(print_field(0.00000012), ' 1.2-7', 'F|%s|' % print_field(0.00000012)) self.assertEqual(print_field(0.000748519), '7.4852-4', 'G|%s|' % print_field(0.000748519)) self.assertEqual(print_field(0.12345678), '.1234568', 'H|%s|' % print_field(0.12345678)) self.assertEqual(print_field(0.00012349), '1.2349-4', 'I|%s|' % print_field(0.00012349)) self.assertEqual(print_field(0.000012349), '1.2349-5', 'J|%s|' % print_field(0.000012349)) self.assertEqual(print_field(5e-08), ' 5.-8', 'K|%s|' % print_field(5e-08)) self.assertEqual(print_field(1e-20), ' 1.-20', 'L|%s|' % print_field(1e-20)) self.assertEqual(print_field(0.0), ' 0.', print_field(0.0)) self.assertEqual(print_field(1.0), ' 1.', print_field(1.0))
def write_bdf2(self, size=8, is_double=False): """ The writer method used by BDF.write_bdf :param self: the GRID object pointer :param size: the size of the card (8/16) :type size: int :param isdouble: should this card be written with double precision (default=False) :type is_double: bool """ xyz = self.xyz if size == 8: cp = set_string8_blank_if_default(self.Cp(), 0) cd = set_string8_blank_if_default(self.Cd(), 0) seid = set_string8_blank_if_default(self.SEid(), 0) msg = '%-8s%8i%8s%s%s%s%s%8s%s\n' % ( 'GRID', self.nid, cp, print_float_8( xyz[0]), print_float_8(xyz[1]), print_float_8( xyz[2]), cd, self.ps, seid) else: cp = set_string16_blank_if_default(self.Cp(), 0) cd = set_string16_blank_if_default(self.Cd(), 0) seid = set_string16_blank_if_default(self.SEid(), 0) if double: msg = ('%-8s%16i%16s%16s%16s\n' '%-8s%16s%16s%16s%16s\n' % ('GRID*', self.nid, cp, print_scientific_double( xyz[0]), print_scientific_double(xyz[1]), '*', print_scientific_double(xyz[2]), cd, self.ps, seid)) else: msg = ('%-8s%16i%16s%16s%16s\n' '%-8s%16s%16s%16s%16s\n' % ('GRID*', self.nid, cp, print_float_16(xyz[0]), print_float_16(xyz[1]), '*', print_float_16( xyz[2]), cd, self.ps, seid)) return self.comment() + msg.rstrip() + '\n'
def test_floats_positive(self): tol = 1.0 self.assertEquals(print_float_8(1.2, tol), ' 1.2', print_float_8(1.2, tol)) self.assertEquals(print_float_8(0.5, tol), ' 0.', print_float_8(0.5, tol)) self.assertEquals(print_float_8(-0.5, tol), ' 0.', print_float_8(-0.5, tol)) self.assertEquals(print_field(1.2), ' 1.2', 'a') self.assertEquals(print_field(1.23456789), '1.234568', 'b') self.assertEquals(print_field(12.234568), '12.23457', 'c') self.assertEquals(print_field(123.23457), '123.2346', 'd') self.assertEquals(print_field(1234.23468), '1234.235', 'e') self.assertEquals(print_field(12345.238), '12345.24', 'f') self.assertEquals(print_field(123456.28), '123456.3', 'g') self.assertEquals(print_field(1234567.25), '1234567.', print_field(1234567.25)) # 1.2346+6 self.assertEquals(print_field( 12345678.), '1.2346+7', '|%s|' % print_field(12345678.)) self.assertEquals(print_field( 123456789.), '1.2346+8', '|%s|' % print_field(12345678.)) self.assertEquals(print_field(0.1), ' .1', 'A|%s|' % print_field(0.1)) self.assertEquals(print_field(0.0001), ' .0001', 'B|%s|' % print_field(0.0001)) self.assertEquals(print_field(0.00001), ' .00001', 'C|%s|' % print_field(0.00001)) self.assertEquals(print_field(0.000001), ' .000001', 'D|%s|' % print_field(0.000001)) self.assertEquals(print_field(0.0000001), '.0000001', 'E|%s|' % print_field(0.0000001)) self.assertEquals(print_field(0.00000012), ' 1.2-7', 'F|%s|' % print_field(0.00000012)) self.assertEquals(print_field(0.000748519), '7.4852-4', 'G|%s|' % print_field(0.000748519)) self.assertEquals(print_field(0.12345678), '.1234568', 'H|%s|' % print_field(0.12345678)) self.assertEquals(print_field(0.00012349), '1.2349-4', 'I|%s|' % print_field(0.00012349)) self.assertEquals(print_field(0.000012349), '1.2349-5', 'J|%s|' % print_field(0.000012349)) self.assertEquals(print_field(5e-08), ' 5.-8', 'K|%s|' % print_field(5e-08)) self.assertEquals(print_field(1e-20), ' 1.-20', 'L|%s|' % print_field(1e-20)) self.assertEquals(print_field(0.0), ' 0.', print_field(0.0)) self.assertEquals(print_field(1.0), ' 1.', print_field(1.0))
def write_bdf_by_index(self, f, i=None, size=8, is_double=False, write_header=True): """ Write the BDF cards :param f: a file object :param i: the indicies (default=None -> all) :param size: the field width (8/16) :param is_double: is this double precision (default=False) :param write_header: should the card marker be written """ if i is None: i = slice(None, None) if self.n: if write_header: f.write('$GRID\n') #self.model.log.debug('GRID i = %s' % i) # default to the GRDSET defaults #cp0 = self.model.grdset.cp #cd0 = self.model.grdset.cd #ps0 = self.model.grdset.ps #seid0 = self.model.grdset.seid # default to the GRID defaults cp0 = 0 cd0 = 0 ps0 = -1 seid0 = 0 blank = ' '*8 if size==8 else ' ' * 16 Cp = [cpi if cpi != cp0 else blank for cpi in self.cp[i]] Cd = [cdi if cdi != cd0 else blank for cdi in self.cd[i]] Ps = [psi if psi != ps0 else blank for psi in self.ps[i]] Seid = [seidi if seidi != seid0 else blank for seidi in self.seid[i]] if size == 8: for (nid, cp, xyz, cd, ps, seid) in zip(self.node_id, Cp, self.xyz[i, :], Cd, Ps, Seid): msg = ('%-8s%8i%8s%s%s%s%s%8s%s\n' % ('GRID', nid, cp, print_float_8(xyz[0]), print_float_8(xyz[1]), print_float_8(xyz[2]), cd, ps, seid)).rstrip() + '\n' f.write(msg) else: if is_double: for (nid, cp, xyz, cd, ps, seid) in zip(self.node_id, Cp, self.xyz[i, :], Cd, Ps, Seid): msg = (('%-8s%16i%16s%16s%16s\n' '%-8s%16s%16s%16s%16s\n' % ('GRID*', nid, cp, print_scientific_double(xyz[0]), print_scientific_double(xyz[1]), '*', print_scientific_double(xyz[2]), cd, ps, seid))).rstrip() + '\n' f.write(msg) else: for (nid, cp, xyz, cd, ps, seid) in zip(self.node_id, Cp, self.xyz[i, :], Cd, Ps, Seid): msg = (('%-8s%16i%16s%16s%16s\n' '%-8s%16s%16s%16s%16s\n' % ('GRID*', nid, cp, print_float_16(xyz[0]), print_float_16(xyz[1]), '*', print_float_16(xyz[2]), cd, ps, seid))).rstrip() + '\n' f.write(msg)
def write_bdf(self, f, size=8, is_double=False, element_id=None): assert self.n > 0, self.n if self.n: if element_id is None: i = arange(self.n) else: i = searchsorted(self.element_id, element_id) blank = ' ' * size #cid = [cid if cid != 0 else '' for cid in self.coord_id] #Mass = [x if x != 0.0 else '' for x in self.mass[i]] if size == 8: X0 = [print_float_8(x) if x != 0.0 else blank for x in self.x[i, 0]] X1 = [print_float_8(x) if x != 0.0 else blank for x in self.x[i, 1]] X2 = [print_float_8(x) if x != 0.0 else '' for x in self.x[i, 2]] I0 = [print_float_8(x) if x != 0.0 else blank for x in self.I[i, 0]] I1 = [print_float_8(x) if x != 0.0 else blank for x in self.I[i, 1]] I2 = [print_float_8(x) if x != 0.0 else blank for x in self.I[i, 2]] I3 = [print_float_8(x) if x != 0.0 else blank for x in self.I[i, 3]] I4 = [print_float_8(x) if x != 0.0 else blank for x in self.I[i, 4]] I5 = [print_float_8(x) if x != 0.0 else blank for x in self.I[i, 5]] for (eid, nid, cid, mass, x0, x1, x2, i0, i1, i2, i3, i4, i5) in zip( self.element_id[i], self.node_id[i], self.coord_id[i], self.mass[i], X0, X1, X2, I0, I1, I2, I3, I4, I5): carda = '%-8s%8i%8i%8i%8s%s%s%s\n' % ('CONM2', eid, nid, cid, print_float_8(mass), x0, x1, x2) cardb = '%8s%8s%8s%8s%8s%8s%8s' % ('', i0, i1, i2, i3, i4, i5) cardi = (carda + cardb).rstrip() + '\n' f.write(cardi) else: assert size == 16, size X0 = [print_float_16(x) if x != 0.0 else blank for x in self.x[i, 0]] X1 = [print_float_16(x) if x != 0.0 else blank for x in self.x[i, 1]] X2 = [print_float_16(x) if x != 0.0 else '' for x in self.x[i, 2]] I0 = [print_float_16(x) if x != 0.0 else blank for x in self.I[i, 0]] I1 = [print_float_16(x) if x != 0.0 else blank for x in self.I[i, 1]] I2 = [print_float_16(x) if x != 0.0 else blank for x in self.I[i, 2]] I3 = [print_float_16(x) if x != 0.0 else blank for x in self.I[i, 3]] I4 = [print_float_16(x) if x != 0.0 else blank for x in self.I[i, 4]] I5 = [print_float_16(x) if x != 0.0 else blank for x in self.I[i, 5]] for (eid, nid, cid, mass, x0, x1, x2, i0, i1, i2, i3, i4, i5) in zip( self.element_id[i], self.node_id[i], self.coord_id[i], self.mass[i], X0, X1, X2, I0, I1, I2, I3, I4, I5): carda = '%-8s%16i%16i%16i%16s\n' % ('CONM2*', eid, nid, cid, print_float_8(mass), ) cardb = '%-8s%16s%16s%16s%16s\n' % ('*', x0, x1, x2, i0) cardc = '%-8s%16s%16s%16s%16s\n' % ('*', i1, i2, i3, i4) cardd = '%-8s%16s' % ('*', i5) cardcd = (cardc + cardd).rstrip('* \n') if cardcd: cardi = carda + cardb + cardc + cardd else: cardi = carda + cardb f.write(cardi.rstrip() + '\n') #def get_stiffness(self, model, node_ids, index0s, fnorm=1.0): #return(K, dofs, nIJV)
def write_bdf(self, f, size=8, is_double=False, element_id=None): assert self.n > 0, self.n if self.n: if element_id is None: i = arange(self.n) else: i = searchsorted(self.element_id, element_id) blank = ' ' * size #cid = [cid if cid != 0 else '' for cid in self.coord_id] #Mass = [x if x != 0.0 else '' for x in self.mass[i]] if size == 8: X0 = [ print_float_8(x) if x != 0.0 else blank for x in self.x[i, 0] ] X1 = [ print_float_8(x) if x != 0.0 else blank for x in self.x[i, 1] ] X2 = [ print_float_8(x) if x != 0.0 else '' for x in self.x[i, 2] ] I0 = [ print_float_8(x) if x != 0.0 else blank for x in self.I[i, 0] ] I1 = [ print_float_8(x) if x != 0.0 else blank for x in self.I[i, 1] ] I2 = [ print_float_8(x) if x != 0.0 else blank for x in self.I[i, 2] ] I3 = [ print_float_8(x) if x != 0.0 else blank for x in self.I[i, 3] ] I4 = [ print_float_8(x) if x != 0.0 else blank for x in self.I[i, 4] ] I5 = [ print_float_8(x) if x != 0.0 else blank for x in self.I[i, 5] ] for (eid, nid, cid, mass, x0, x1, x2, i0, i1, i2, i3, i4, i5) in zip(self.element_id[i], self.node_id[i], self.coord_id[i], self.mass[i], X0, X1, X2, I0, I1, I2, I3, I4, I5): carda = '%-8s%8i%8i%8i%8s%s%s%s\n' % ( 'CONM2', eid, nid, cid, print_float_8(mass), x0, x1, x2) cardb = '%8s%8s%8s%8s%8s%8s%8s' % ('', i0, i1, i2, i3, i4, i5) cardi = (carda + cardb).rstrip() + '\n' f.write(cardi) else: assert size == 16, size X0 = [ print_float_16(x) if x != 0.0 else blank for x in self.x[i, 0] ] X1 = [ print_float_16(x) if x != 0.0 else blank for x in self.x[i, 1] ] X2 = [ print_float_16(x) if x != 0.0 else '' for x in self.x[i, 2] ] I0 = [ print_float_16(x) if x != 0.0 else blank for x in self.I[i, 0] ] I1 = [ print_float_16(x) if x != 0.0 else blank for x in self.I[i, 1] ] I2 = [ print_float_16(x) if x != 0.0 else blank for x in self.I[i, 2] ] I3 = [ print_float_16(x) if x != 0.0 else blank for x in self.I[i, 3] ] I4 = [ print_float_16(x) if x != 0.0 else blank for x in self.I[i, 4] ] I5 = [ print_float_16(x) if x != 0.0 else blank for x in self.I[i, 5] ] for (eid, nid, cid, mass, x0, x1, x2, i0, i1, i2, i3, i4, i5) in zip(self.element_id[i], self.node_id[i], self.coord_id[i], self.mass[i], X0, X1, X2, I0, I1, I2, I3, I4, I5): carda = '%-8s%16i%16i%16i%16s\n' % ( 'CONM2*', eid, nid, cid, print_float_8(mass), ) cardb = '%-8s%16s%16s%16s%16s\n' % ('*', x0, x1, x2, i0) cardc = '%-8s%16s%16s%16s%16s\n' % ('*', i1, i2, i3, i4) cardd = '%-8s%16s' % ('*', i5) cardcd = (cardc + cardd).rstrip('* \n') if cardcd: cardi = carda + cardb + cardc + cardd else: cardi = carda + cardb f.write(cardi.rstrip() + '\n')
def test_float_8(self): small_exponent = -17 large_exponent = 17 nums = ([0., 0.000034, -0.000034] + [ 9. / 11 * 10**x for x in range(small_exponent, large_exponent + 1) ] + [ -9. / 11 * 10**x for x in range(small_exponent, large_exponent + 1) ]) expected = [ ' 0.', ' .000034', ' -3.4-5', '8.182-18', '8.182-17', '8.182-16', '8.182-15', '8.182-14', '8.182-13', '8.182-12', '8.182-11', '8.182-10', '8.1818-9', '8.1818-8', '8.1818-7', '8.1818-6', '8.1818-5', '8.1818-4', '.0081818', '.0818182', '.8181818', '8.181818', '81.81818', '818.1818', '8181.818', '81818.18', '818181.8', '8181818.', '8.1818+7', '8.1818+8', '8.1818+9', '8.182+10', '8.182+11', '8.182+12', '8.182+13', '8.182+14', '8.182+15', '8.182+16', '-8.18-18', '-8.18-17', '-8.18-16', '-8.18-15', '-8.18-14', '-8.18-13', '-8.18-12', '-8.18-11', '-8.18-10', '-8.182-9', '-8.182-8', '-8.182-7', '-8.182-6', '-8.182-5', '-8.182-4', '-8.182-3', '-.081818', '-.818182', '-8.18182', '-81.8182', '-818.182', '-8181.82', '-81818.2', '-818182.', '-8.182+6', '-8.182+7', '-8.182+8', '-8.182+9', '-8.18+10', '-8.18+11', '-8.18+12', '-8.18+13', '-8.18+14', '-8.18+15', '-8.18+16', ] for x, expectedi in zip(nums, expected): output = print_float_8(x) self.assertEqual(len(output), 8, msg='output=%r len(output)=%i' % (output, len(output))) self.assertEqual(output, expectedi, msg='num=%s output=%r expected=%r' % (x, output, expectedi))