z-dist.celx

All about writing scripts for Celestia in Lua and the .cel system
Topic author
ajtribick
Developer
Posts: 1855
Joined: 11.08.2003
With us: 20 years 8 months

z-dist.celx

Post #1by ajtribick » 17.01.2008, 19:14

A couple of issues I've identified in z-dist.celx.

Firstly, selecting a galaxy and then deselecting it removes the marker indicating the redshift.

Secondly, the selection highlighting does not discriminate between galaxies and other objects.

I've rewritten some of the code in the "main" section of the script to correct these issues, hopefully this all works now.

Code: Select all

-- Title: Show Redshifts of Galaxies

function get_distance(obj_pos, ref_pos)
   -- Returns distance Earth-object in Mpc.
   distance = ref_pos:distanceto(obj_pos) * km2Mpc
   return distance
end

function get_z(distance)
   -- Returns redshift z.
   -- Hubble constant = 73.2 (km/s)/Mpc, WMAP 3years, best
   -- http://map.gsfc.nasa.gov/m_mm/tp_links.html
   local H0 = 73.2
   local c = 299792.458
   z0 = (H0 * distance)/c
   z = z0*(1 - z0/4)/(1 - z0/2)^2
   -- d <= 2 * c/H0
   return z
end

function rgb2hex(r,g,b)
   -- Converts color code from RGB to Hex
   local hex = string.format("#%.2x%.2x%.2x", r,g,b)
   return hex
end

function get_color(z_rel)
   -- Returns Hex color code from z_rel
   if z_rel > 1 then
      z_rel = 1
   end
   --local green = 255 * (1 - z_rel)/(1 + math.sqrt(z_rel))
   local green = 255 * (1 - z_rel)^2
   local red   = 255
   local blue  = 255* math.sqrt(green/255)
   hex_color   = rgb2hex(red, green, blue)
   return hex_color
end
   
function mark_galaxies(sel_pos)
   zz = z_max(sel_pos)
   for dso in celestia:dsos() do
      if dso:type() == "galaxy" then
         dso_pos = dso:getposition()
         local d = get_distance(dso_pos,sel_pos)
         local z_rel = get_z(d)/zz
         local hex_color = get_color(z_rel)
         dso:mark( hex_color, "disk", 1, 1 )
      end
   end
end

function z_max(ref_pos)
   -- determine maximal redshift in catalog wrto ref_pos
   z_old = 0
   for dso in celestia:dsos() do
      if dso:type() == "galaxy" then
         dso_pos = dso:getposition()
         local d = get_distance(dso_pos, ref_pos)
         local z = get_z(d)
         if z > z_old then
            z_max = z
            z_old = z_max
            dsomax = dso
         end   
      end
   end
   return z_max
end

----------
-- main --
----------
celestia:unmarkall()

celestia:show("markers")
km2Mpc = 1/3.08568025e19
MW = celestia:find("Milky Way")
MW_pos = MW:getposition()
--
-- select and specially mark Milky Way
--
celestia:select(MW)
celestia:mark(MW)
--
-- color encode all other galaxies according to their redshift
-- relative to Milky Way
--
mark_galaxies(MW_pos)
--
-- move observer to a distance of 1000 Mpc from Milky Way
--
observer = celestia:getobserver()
observer:gotodistance(MW, 1000/km2Mpc,5)

sel_old = MW
   
while true do   
   sel = celestia:getselection()
   --
   -- specially mark possible new selection, unmark the old one
   --
   if sel_old ~= nil then
      if sel ~= sel_old then
         if sel_old:type() == "galaxy" then
            sel_old:unmark()
            local d = get_distance(MW_pos,sel_old:getposition())
            local z_rel = get_z(d) / zz
            local hex_color = get_color(z_rel)
            sel_old:mark(hex_color, "disk", 1, 1)
            sel_old = nil
         end
      end
   end
   
   sel_pos = sel:getposition()
   
   -- obs_pos = observer:getposition()
   
   if sel:type() == "galaxy" then
      sel:unmark()
      sel:mark("#00FF00","disk",10,1)
      sel_old = sel
      local d = get_distance(MW_pos,sel_pos)
      local z = get_z(d)
      celestia:print(sel:name()..": "..string.format("redshift z = %5.3f,  distance = %5.2f  Mpc", z,d).."\nmax. redshift: "..dsomax:name(),5,-1,-1,0,6)
   end
   wait(0)
end

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 1 month
Location: Hamburg, Germany

Post #2by t00fri » 17.01.2008, 19:59

hey hey, becoming a .celx expert now ! ;-) I like that...
(me being not quite innocent I suppose? apart from being the z-dist.celx author...)

Cheers,
Fridger
Image

captain_picard
Posts: 6
Joined: 11.08.2010
With us: 13 years 8 months

Re: z-dist.celx

Post #3by captain_picard » 16.02.2013, 18:34

Hi,

Sorry for the thread "necromancy", but I was playing with the script and I soon realized that there seem to be a couple of problems with the estimation of the redshift.

First, inverting the formula

Code: Select all

z0 = (H0 * distance)/c
z = z0*(1 - z0/4)/(1 - z0/2)^2

and solving for the distance one gets

Code: Select all

distance(z)=2 c/H_0 (1 - 1/Sqrt[1 + z])

clearly this is valid only for a matter dominated model, ie \Omega_m=1. Such a model has a hubble parameter

Code: Select all

H(z)/H_0=(1+z)^{3/2}

and is clearly ruled out by current (WMAP9) observations, see http://lambda.gsfc.nasa.gov/product/map ... meters.cfm and http://arxiv.org/pdf/1212.5226.pdf

Second, since what is actually measured in a survey, especially for far away galaxies, is the redshift and not the distance itself, then a question is how were the redshifts converted to distances. Furthermore, I think that the luminosity distance

Code: Select all

d_L(z)=(1+z)*distance(z)

would be appropriate instead of the physical distance (though both are similar for small z).

Finally, a very good approximation to the luminosity distance for a Universe with a cosmological constant and matter density \Omega _m is
if z0=\frac{d_L(z)}{c/H_0}

Code: Select all

z=z0+\frac{1}{4} \left(-4+3 \Omega_m\right) z0^2+\frac{1}{4} \left(8-7 \Omega_m\right) z0^3-\frac{5}{16} \left(16-18 \Omeg _m+3 \Omega_m^2\right) z0^4+\frac{1}{16} \left(224-310 \Omega_m+93 \Omega_m^2\right) z0^5+\frac{1}{32} \left(-1344+2212 \Omega_m-958 \Omega_m^2+69 \Omega_m^3\right) z0^6+O[z0]^7

(sorry for the long expression, the tex doesn't seem to be working).

Also, H_0 is found by WMAP9 to be around to 70.5 for this model, see http://lambda.gsfc.nasa.gov/product/map ... pt_act.cfm

Cheers
Image

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 1 month
Location: Hamburg, Germany

Re: z-dist.celx

Post #4by t00fri » 16.02.2013, 19:44

All these distance(z) formulae are approximations only, with accuracy depending on the application under consideration. First of all you should realize that we are facing here a framework embedded in General Relativity. That implies that one has to carefully contemplate what distance means within GR!

Most naive notions of a distance are simply incompatible with GR.
The only relativistically invariant distance is the so-called co-moving distance eta, depending of course on all Omegas. Here is some respective commented Perl code that I wrote and used in the context of my implementation of 10000+ galaxies in Celestia:

Code: Select all

sub dcmr {
# calculate the comoving distance appropriate for Hubble's law,
# given $z, $Omega_matter=$Wm, $Omega_lambda=$Wlam.
    my($Wm, $Wlam, $z) = @_;
    my $Wr = 0.4165/($H0 * $H0); # 3 massless neutrinos to Omega (radiation)
    my $Wk = 1 - $Wm -$Wr -$Wlam; # flat universe, Omega (total) = 1
    my $az = 1/($z + 1);
    my $numpoints = 50; # support points for numerical integration
    # do integral over $a=1/(1+$z) from $az to 1 in $numpoints steps,
    # midpoint rule
        $dcmr = 0;
        #print "$Wm $Wlam $Wk  $Wr $az\n";
        for ($i = 0; $i < $numpoints; $i++) {
    my $a = $az + (1 - $az) * ($i + 0.5)/$numpoints;
    my $adot = sqrt($Wk + ($Wm/$a) + ($Wr/($a * $a)) + ($Wlam * $a * $a));
    $dcmr = $dcmr + 1/($a * $adot);
        }
        return $dcmr = $pc2ly * 1e6 * $c/$H0 * (1 - $az) * $dcmr/$numpoints;
}


Fridger
Image

captain_picard
Posts: 6
Joined: 11.08.2010
With us: 13 years 8 months

Re: z-dist.celx

Post #5by captain_picard » 16.02.2013, 20:59

First of all thanks for the quick response!
t00fri wrote:All these distance(z) formulae are approximations only, with accuracy depending on the application under consideration.
The equation you used in the "z-dist.celx" script is not an approximation, but it is an exact result for a matter dominated universe, called actually the "Mattigs formula", see Eq. (3.78) for Omega=1 in http://ned.ipac.caltech.edu/level5/Peac ... ck3_4.html . Thus, as I argued earlier, it is wrong or at least not compatible with the observations and in clear contradiction to the perl code you used in the first place.

t00fri wrote:First of all you should realize that we are facing here a framework embedded in General Relativity. That implies that one has to carefully contemplate what distance means within GR! Most naive notions of a distance are simply incompatible with GR.

Yes, I actually know GR quite well. BTW, I have a Phd in theoretical physics/cosmology and right now I work as a postdoc in a major university in the EU. I've done most of my work on testing Dark energy/modified gravity models with supernovae Ia, CMB and BAO data.

t00fri wrote:The only relativistically invariant distance is the so-called co-moving distance eta, depending of course on all Omegas.
When you use light signals, as it is supposed to be done in Celestia, you don't measure the comoving distance d(z) but the luminosity distance dL(z), where dL=(1+z)*d(z). Of course, since you only use the distance as an intermediate step for bookkeeping reasons, my point is moot :)

t00fri wrote: Here is some respective commented Perl code that I wrote and used in the context of my implementation of 10000+ galaxies in Celestia
Thanks for sharing this. I'm not an expert in Perl, but from a quick look that I just had, the conversion from z to comoving distance d(z) seems to have been done properly. The only possible problem is the part:

Code: Select all

 my $Wk = 1 - $Wm -$Wr -$Wlam; # flat universe, Omega (total) = 1
...
$adot = sqrt($Wk + ($Wm/$a) + ($Wr/($a * $a)) + ($Wlam * $a * $a));

In a flat universe Omega_K (the curvature) would be 0 by default, ie $Wk=0, and you would use the extra freedom, as Omega (total) = 1 is true by construction, to fix the cosmological constant term $Wlam=1 - $Wm -$Wr. Of course, if you imposed "by hand" the right values for $Wlam, $Wm, $Wr this would not be an issue.

Therefore, the error/discrepancy is only in "z-dist.celx" where the comoving distance is converted back to redshift. So, if you want to be accurate you could update the script with the formula for the cosmological constant model to convert the comoving distances back to redshift or at least put a warning message.

Thanks!
Image

Avatar
t00fri
Developer
Posts: 8772
Joined: 29.03.2002
Age: 22
With us: 22 years 1 month
Location: Hamburg, Germany

Re: z-dist.celx

Post #6by t00fri » 16.02.2013, 22:17

captain_picard wrote:First of all thanks for the quick response!
t00fri wrote:All these distance(z) formulae are approximations only, with accuracy depending on the application under consideration.
The equation you used in the "z-dist.celx" script is not an approximation, but it is an exact result for a matter dominated universe, called actually the "Mattigs formula", see Eq. (3.78) for Omega=1 in http://ned.ipac.caltech.edu/level5/Peac ... ck3_4.html . Thus, as I argued earlier, it is wrong or at least not compatible with the observations and in clear contradiction to the perl code you used in the first place.
Of course, I am well aware of the Mattigs formula.
Yet, for me a matter-dominated Universe is also an approximation (as the term 'dominated' already expresses), e.g. since meanwhile we have long entered the dark-energy dominated era...
t00fri wrote:First of all you should realize that we are facing here a framework embedded in General Relativity. That implies that one has to carefully contemplate what distance means within GR! Most naive notions of a distance are simply incompatible with GR.

Yes, I actually know GR quite well. BTW, I have a Phd in theoretical physics/cosmology and right now I work as a postdoc in a major university in the EU. I've done most of my work on testing Dark energy/modified gravity models with supernovae Ia, CMB and BAO data.
Good, so we are colleagues as to the field of research (theory), except that I am far more senior ;-) My daughter has also done her PhD on Dark Energy (and Neutrinos) in theoretical Particle Cosmology beginning of 2008.
t00fri wrote:The only relativistically invariant distance is the so-called co-moving distance eta, depending of course on all Omegas.
When you use light signals, as it is supposed to be done in Celestia, you don't measure the comoving distance d(z) but the luminosity distance dL(z), where dL=(1+z)*d(z). Of course, since you only use the distance as an intermediate step for bookkeeping reasons, my point is moot :)

t00fri wrote: Here is some respective commented Perl code that I wrote and used in the context of my implementation of 10000+ galaxies in Celestia
Thanks for sharing this. I'm not an expert in Perl, but from a quick look that I just had, the conversion from z to comoving distance d(z) seems to have been done properly. The only possible problem is the part:

Code: Select all

 my $Wk = 1 - $Wm -$Wr -$Wlam; # flat universe, Omega (total) = 1
...
$adot = sqrt($Wk + ($Wm/$a) + ($Wr/($a * $a)) + ($Wlam * $a * $a));

In a flat universe Omega_K (the curvature) would be 0 by default, ie $Wk=0, and you would use the extra freedom, as Omega (total) = 1 is true by construction, to fix the cosmological constant term $Wlam=1 - $Wm -$Wr. Of course, if you imposed "by hand" the right values for $Wlam, $Wm, $Wr this would not be an issue.
That was what I did in this quick coding exercise.
Therefore, the error/discrepancy is only in "z-dist.celx" where the comoving distance is converted back to redshift. So, if you want to be accurate you could update the script with the formula for the cosmological constant model to convert the comoving distances back to redshift or at least put a warning message.
Anyway, from my part the script 'z-dist.celx' was only a quick 15 min effort to code a script-based qualitative color marking of Celestia's galaxies depending on the amount of redshift z. Nothing serious, really. ;-)

Fridger
Image


Return to “Scripting”