cosimoNigro / agnpy

Modelling jetted Active Galactic Nuclei radiative processes with python
https://agnpy.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
48 stars 33 forks source link

calculation of synchrotron radiation fails with the current code #74

Closed jsitarek closed 3 years ago

jsitarek commented 3 years ago

the following code fails, while it was running fine in the past (Jun 2020 version). the error message is: UnitConversionError: 'cm(5/2) Fr3 g(1/2) / (cm2 J s2)' and 'erg / (cm2 s)' are not convertible this is probably again a similar problem like we had with transform of B into cgs units, but this time with the electric charge

import numpy as np import math import astropy.units as u import astropy.constants as const from astropy.coordinates import Distance

import sys sys.path.append("/home/jsitarek/zdalne/agnpy/agnpy_js/")

sys.path.append("/home/jsitarek/zdalne/agnpy/agnpy_b2-1420/")

import agnpy classes

from agnpy.emission_regions import Blob from agnpy.synchrotron import Synchrotron

z = 0.94 # redshift

delta_D=15 Gamma=15 theta=1./Gamma

far part

spectrum_norm2=2.e-7u.Unit("erg cm-3") B2=np.sqrt(spectrum_norm2.value8np.pi) u.G gmin2=2. # minimum Lorentz factor xi2=3.e-10 # acceleration parameter dist2=100 3.e18u.cm # distance of the blob p1_2=2.0 p2_2=p1_2+1

radius2=dist2*theta

gbreak=43.e3 # tentative values gmax=43.e3 # tentative values parameters2 = { "p1": p1_2, "p2": p2_2, "gamma_b": gbreak, "gamma_min": gmin2, "gamma_max": gmax, } spectrum_dict2 = {"type": "BrokenPowerLaw", "parameters": parameters2} blob2 = Blob(radius2, z, delta_D, Gamma, B2, spectrum_norm2, spectrum_dict2, xi=xi2)

nu = np.logspace(8, 28, 100) * u.Hz synch2 = Synchrotron(blob2, ssa=True)

let us compute synchrotron SED

synch_sed2 = synch2.sed_flux(nu)

cosimoNigro commented 3 years ago

Hi @jsitarek, thanks for reporting, very strange that is not matched by the tests. I just pulled the latest version and executed all the tests, they work. I will take another look executing your snippet.

cosimoNigro commented 3 years ago

Hi @jsitarek, to me it works with the latest version of the code. I have cleaned your snippet, please next time post a more ordered snipppet :stuck_out_tongue_winking_eye:

import numpy as np
import math
import astropy.units as u
import astropy.constants as const
from astropy.coordinates import Distance
from agnpy.emission_regions import Blob
from agnpy.synchrotron import Synchrotron
from agnpy.utils.plot import plot_sed
import matplotlib.pyplot as plt

z = 0.94 # redshift
delta_D = 15
Gamma = 15
theta = 1. / Gamma
spectrum_norm2 = 2.e-7 * u.Unit("erg cm-3")
B2 = np.sqrt(spectrum_norm2.value * 8 * np.pi) * u.G
gmin2 = 2. # minimum Lorentz factor
xi2 = 3.e-10 # acceleration parameter
dist2 = 100 * 3.e18 * u.cm # distance of the blob
p1_2 = 2.0
p2_2 = p1_2 + 1

radius2 = dist2 * theta

gbreak = 43.e3 # tentative values
gmax = 43.e3 # tentative values
parameters2 = {
    "p1": p1_2,
    "p2": p2_2,
    "gamma_b": gbreak,
    "gamma_min": gmin2,
    "gamma_max": gmax,
}
spectrum_dict2 = {"type": "BrokenPowerLaw", "parameters": parameters2}
blob2 = Blob(radius2, z, delta_D, Gamma, B2, spectrum_norm2, spectrum_dict2, xi=xi2)

nu = np.logspace(8, 28, 100) * u.Hz
synch2 = Synchrotron(blob2, ssa=True)
# let us compute synchrotron SED
synch_sed2 = synch2.sed_flux(nu)
# let us plot it
plot_sed(nu, synch_sed2)
plt.show()

Figure_1

Can you try:

cd agnpy
git pull origin master
python -m pip install .

this will install the development agnpy version in your miniconda base environment

In [1]: import agnpy
In [2]: agnpy.__file__
Out[2]: '/home/cosimo/software/miniconda3/lib/python3.7/site-packages/agnpy/__init__.py'

you can un-install it any moment with pip

cosimo@posimo:~$ pip uninstall agnpy
Found existing installation: agnpy 0.0.8
Uninstalling agnpy-0.0.8:
  Would remove:
    /home/cosimo/software/miniconda3/lib/python3.7/site-packages/agnpy-0.0.8.dist-info/*
    /home/cosimo/software/miniconda3/lib/python3.7/site-packages/agnpy/*
Proceed (y/n)? y
  Successfully uninstalled agnpy-0.0.8

I recommend you stop using sys.path.append.

jsitarek commented 3 years ago

Hi @cosimoNigro,

Thx for checking and sorry for a messy code example. I checked the snippet again and mysteriously it works now, I have no idea what happened here, I will check again my on laptop at home, maybe I screwed up something with the code there. I checked also the other issue #75 since it was reporting also a similarly looking problem, but it seems to be something else there.

About sys.path.append I'm using two versions of agnpy, the current one, and some older one which had some differences in API, and also having both was good for tests, and this was the simplest way to switch between them. Installing it via pip is convenient for the user, but then if I install it via pip I guess I would have to reinstall it after every modification of the code, or modify directly the installed code and then for commits move those files back to git directory. Or does it just install links to git directory?

Let me close the issue now, if I find out what happened that caused the original problem I will let you know, and sorry again for wasting your time on checking it.