def test_fgong_properties_reverse(self): m = fgong.load_fgong('data/modelS.fgong') m_rev = fgong.load_fgong('data/modelS.fgong') m_rev.var = m_rev.var[::-1] for attr in ['x', 'rho', 'P', 'tau', 'cs']: np.testing.assert_allclose(getattr(m, attr), getattr(m_rev, attr)[::-1])
def test_load_fgong(self): m = fgong.load_fgong('data/modelS.fgong') self.assertEqual(len(m), 2482) self.assertEqual(m.glob[0], m.M) self.assertEqual(m.glob[1], m.R) self.assertEqual(m.glob[2], m.L) np.testing.assert_equal(m.m, np.exp(m.var[:, 1]) * m.glob[0]) np.testing.assert_equal(m.x, m.var[:, 0] / m.glob[1]) np.testing.assert_equal(m.G1, m.Gamma_1) np.testing.assert_allclose(m.Gamma_1 / (m.Gamma_3 - 1), m.Gamma_2 / (m.Gamma_2 - 1), rtol=1e-14, atol=1e-14) s = '%s' % m s = '%r' % m r = fgong.load_fgong(remote_url + 'data/modelS.fgong') for line1, line2 in zip(m.description, r.description): self.assertEqual(line1, line2) np.testing.assert_equal(m.glob, r.glob) np.testing.assert_equal(m.var, r.var)
def test_save_fgong(self): m1 = fgong.load_fgong('data/modelS.fgong') m1.to_file(tmpfile, float_formatter='ivers') m2 = fgong.load_fgong(tmpfile) for line1, line2 in zip(m1.description, m2.description): self.assertEqual(line1, line2) np.testing.assert_allclose(m1.glob, m2.glob) np.testing.assert_allclose(m1.var, m2.var)
def test_save_fgong(self): glob1, var1, comment1 = fgong.load_fgong('data/modelS.fgong', return_comment=True) fgong.save_fgong(tmpfile, glob1, var1, comment=comment1, fmt='%16.9E') glob2, var2, comment2 = fgong.load_fgong(tmpfile, return_comment=True) for i in range(len(glob1)): self.assertAlmostEqual(glob1[i], glob2[i]) for line1, line2 in zip(comment1, comment2): self.assertEqual(line1, line2) for i in range(len(var1)): for j in range(len(var1[i])): self.assertAlmostEqual(var1[i, j], var2[i, j])
def test_fgong_to_amdl_mesa(self): a1 = adipls.load_amdl('data/mesa.amdl', G=6.67428e-8) f = fgong.load_fgong('data/mesa.fgong', G=6.67428e-8) a2 = f.to_amdl() np.testing.assert_allclose(a1.D, a2.D) np.testing.assert_allclose(a1.A, a2.A)
def test_fgong_to_amdl_modelS(self): a1 = adipls.load_amdl('data/modelS.amdl', G=6.67232e-8) m2 = fgong.load_fgong('data/modelS.fgong', G=6.67232e-8) a2 = m2.to_amdl() np.testing.assert_allclose(a1.D, a2.D) np.testing.assert_allclose(a1.A, a2.A)
def test_fgong_get(self): glob, var = fgong.load_fgong('data/modelS.fgong') x, r, R, M = fgong.fgong_get(['x', 'r', 'R', 'M'], glob, var) self.assertTrue(np.all(x == r / R)) self.assertAlmostEqual(M, 1.989e33) self.assertAlmostEqual(R, 6.959906258e10) cs, = fgong.fgong_get(['cs'], glob, var) cs2 = fgong.fgong_get('cs2', glob, var) self.assertTrue(np.allclose(cs**2, cs2))
def test_amdl_to_fgong_mesa(self): I = [0, 3, 4, 9, 14] # only these columns can be restored from AMDL format m1 = fgong.load_fgong('data/mesa.fgong', G=6.67428e-8) a2 = adipls.load_amdl('data/mesa.amdl', G=6.67428e-8) m2 = a2.to_fgong() np.testing.assert_allclose(m1.glob[:2], m2.glob[:2]) np.testing.assert_allclose(m1.var[:, I], m2.var[:, I])
def test_fgong_to_amdl_mesa(self): D1, A1 = adipls.load_amdl('data/mesa.amdl') glob, var = fgong.load_fgong('data/mesa.fgong') D2, A2 = adipls.fgong_to_amdl(glob, var, G=6.67232e-8) for (x, y) in zip(D1, D2): self.assertAlmostEqual(x, y) for i in range(len(A1)): for (x, y) in zip(A1[i], A2[i]): self.assertAlmostEqual(x, y)
def test_load_fgong(self): glob, var, comment = fgong.load_fgong('data/modelS.fgong', return_comment=True) self.assertEqual(comment[0][:6], 'L5BI.D') self.assertEqual(len(glob), 15) self.assertEqual(len(var), 2482) self.assertEqual(len(var[0]), 30) self.assertAlmostEqual(glob[0], 1.989e33) self.assertAlmostEqual(glob[1], 6.959906258e10) self.assertAlmostEqual(glob[2], 3.845999350e33)
def test_fgong_get_cross_check(self): glob, var = fgong.load_fgong('data/modelS.fgong') M, R, L, r, x, m, q, g, rho, P, Hp, G1, T, X, L_r, kappa, epsilon, cs2, cs, tau \ = fgong.fgong_get(['M','R', 'L', 'r', 'x', 'm', 'q', 'g', 'rho', 'P', 'Hp', 'G1', 'T', 'X', 'L_r', 'kappa', 'epsilon', 'cs2', 'cs', 'tau'], glob, var) self.assertTrue(np.allclose(q, m / M, rtol=4 * EPS)) self.assertTrue(np.allclose(x, r / R, rtol=4 * EPS)) self.assertTrue( np.allclose(Hp, P / (rho * g), rtol=4 * EPS, equal_nan=True)) self.assertTrue(np.allclose(cs2, G1 * P / rho, rtol=4 * EPS))
def myplot(): th = np.linspace(0.0, np.pi, 201) ph = np.linspace(0.5*np.pi, 2.0*np.pi, 301) Th, Ph = np.meshgrid(th, ph) x = np.outer(np.cos(ph), np.sin(th)) y = np.outer(np.sin(ph), np.sin(th)) z = np.outer(np.ones(np.size(ph)), np.cos(th)) s = sph_harm(emm, ell, Ph, Th).real mlab.mesh(x, y, z, scalars=s, colormap='seismic') S = fgong.load_fgong('data/modelS.fgong', return_object=True) css, eigs = adipls.load_amde('data/modelS.amde') I = np.where(css['ell']==ell)[0] if args.freq: i = I[np.argmin((css['nu_Ri'][I]-args.freq)**2)] elif args.enn: i = I[css['enn'][I]==args.enn][0] else: i = I[css['enn'][I]==14][0] print(' n = %i' % css['enn'][i]) print('freq = %.6f mHz' % css['nu_Ri'][i]) r = eigs[i][:,0] y1 = eigs[i][:,1] rho = np.interp(r, S.x[::-1], S.rho[::-1]) # r = np.linspace(0.,1.,51) # s = np.outer(np.sin(1.5*np.pi*r), np.ones(len(th))) s = np.outer(r*rho**0.5*y1, np.ones(len(th))) s = s*sph_harm(emm, ell, np.outer(np.ones(len(r)), np.ones(len(th))), np.outer(np.ones(len(r)), th)).real # s = 0.5+0.5*s/np.max(np.abs(s)) smax = np.max(np.abs(s))/2. # divide by two gives richer colour smin = -smax # guarantees symmetry in colourmap # first inner semicircle x = np.outer(r, np.sin(th)) y = 0.*x z = np.outer(r, np.cos(th)) mlab.mesh(x, y, z, scalars=-s, colormap='seismic', vmin=smin, vmax=smax) # second inner semicircle y = np.outer(r, np.sin(th)) x = 0.*y z = np.outer(r, np.cos(th)) mlab.mesh(x, y, z, scalars=-s, colormap='seismic', vmin=smin, vmax=smax) mlab.view(azimuth=args.view[0], elevation=args.view[1], distance=4.2)
def test_fgong_properties(self): m = fgong.load_fgong('data/modelS.fgong') np.testing.assert_equal(m.x, m.r / m.R) self.assertAlmostEqual(m.M, 1.989e33) self.assertAlmostEqual(m.R, 6.959894677e10) np.testing.assert_allclose(m.cs**2, m.cs2, rtol=4 * EPS) np.testing.assert_allclose(m.q, m.m / m.M, rtol=4 * EPS) np.testing.assert_allclose(m.x, m.r / m.R, rtol=4 * EPS) np.testing.assert_allclose(m.Hp[:-1], (m.P / m.rho / m.g)[:-1], rtol=4 * EPS) np.testing.assert_allclose(m.cs2, m.Gamma_1 * m.P / m.rho, rtol=4 * EPS)
def convert(args): """Convert function for `tomso` command-line script.""" from_format = (guess_format(args.input_file) if args.from_format == 'guess' else args.from_format) to_format = (guess_format(args.output_file) if args.to_format == 'guess' else args.to_format) if from_format == to_format: raise ValueError("input format and output format are both %s\n" "did you mean to copy the file?" % from_format) kwargs = {} if args.G is not None: kwargs['G'] = args.G if from_format == 'fgong': from tomso.fgong import load_fgong m = load_fgong(args.input_file, **kwargs) elif from_format == 'amdl': from tomso.adipls import load_amdl m = load_amdl(args.input_file, **kwargs) elif from_format == 'gyre': from tomso.gyre import load_gyre m = load_gyre(args.input_file, **kwargs) else: raise ValueError("%s is not a valid input format" % from_format) if to_format == 'fgong': m.to_fgong(ivers=args.ivers).to_file(args.output_file) elif to_format == 'amdl': m.to_amdl().to_file(args.output_file) elif to_format == 'gyre': m.to_gyre().to_file(args.output_file) else: raise ValueError("%s is not a valid output format" % to_format)
def test_fgong_to_fgong(self): f = load_fgong('data/modelS.fgong') self.compare_models(f, f.to_gyre().to_fgong(), thermo=True)
raise ValueError("""you can only use one of the radial order (-n, --enn) or the cyclic frequency (-f, --freq)""") amax = 0.55 # np.sqrt((2.*ell+1.)/(4.*np.pi)*factorial(l-m)/factorial(l+m)) ? Ntheta = 201 def get_colour(theta, phi): a = sph_harm(args.emm, args.ell, np.outer(phi, np.ones(len(theta))), np.outer(np.ones(len(phi)), theta)).real a = 0.5+0.5*a/amax return pl.cm.seismic(a) S = fgong.load_fgong('data/modelS.fgong', return_object=True) css, eigs = adipls.load_amde('data/modelS.amde') I = np.where(css['ell']==args.ell)[0] if args.freq: i = I[np.argmin((css['nu_Ri'][I]-args.freq)**2)] elif args.enn: i = I[css['enn'][I]==args.enn][0] else: i = I[css['enn'][I]==14][0] r = eigs[i][:,0] y1 = eigs[i][:,1] rho = np.interp(r, S.x[::-1], S.rho[::-1]) theta = np.linspace(0.0, 2.*np.pi, Ntheta) a = np.outer(r*rho**0.5*y1, np.ones(len(theta))) # use pi/2-theta instead of theta so that 0 degrees is at the top
}).to_csv('psi_lambda.dat', sep='\t', index=False) #exit() ## amde file holds the eigenfunctions # ref: ibid., Section 8.4 # important note: TOMSO uses nfmode=1 ! amde = adipls.load_amde(amde_fname, nfmode=1, return_object=True) ## read equation of state derivatives from file, either FGONG or external if os.path.exists(gamder_fname): gamder = pd.read_table(gamder_fname, sep='\\s+', names=['x', 'dgam_P', 'dgam_rho', 'dgam_Y'])[::-1] else: gong = fgong.load_fgong(fgong_fname, return_object=True) if gong.var.shape <= 25: print("Error: FGONG does not contain EOS derivatives") gamder = pd.DataFrame({ 'x': gong.var[:, 0] / R, 'dgam_rho': gong.var[:, 25], # (dGamma_1/drho)_P,Y 'dgam_P': gong.var[:, 26], # (dGamma_1/dP)_rho,Y 'dgam_Y': gong.var[:, 27] }) # (dGamma_1/dY)_P,rho # interpolate onto remeshed grid dG1rho = InterpolatedUnivariateSpline(gamder['x'], gamder['dgam_rho'])(x) dG1P = InterpolatedUnivariateSpline(gamder['x'], gamder['dgam_P'])(x) dG1Y = InterpolatedUnivariateSpline(gamder['x'], gamder['dgam_Y'])(x)
#!/usr/bin/env python import numpy as np from tomso import fgong from matplotlib import pyplot as pl # Model S can be downloaded from # http://astro.phys.au.dk/~jcd/solar_models/fgong.l5bi.d.15c try: s = fgong.load_fgong('data/modelS.fgong', G=6.67232e-8, return_object=True) except IOError: try: from urllib2 import urlopen except ImportError: from urllib.request import urlopen response = urlopen('http://astro.phys.au.dk/~jcd/solar_models/fgong.l5bi.d.15c') with open('data/modelS.fgong','wb') as f: f.write(response.read()) response.close() s = fgong.load_fgong('data/modelS.fgong', G=6.67232e-8, return_object=True) N = np.ones_like(s.N2)*1e-50 N[s.N2>0] = np.sqrt(s.N2[s.N2>0])/2./np.pi S = [np.sqrt(1.*l*(l+1)*s.cs2/s.r**2)/2./np.pi for l in [1,15]] pl.plot([-1,2], [5.2e3, 5.2e3], 'k--') line, = pl.semilogy(s.x, N*1e6) pl.fill_between(s.x, 1e-99, N*1e6, facecolor=line.get_color(), alpha=0.5)