dolfin team mailing list archive
-
dolfin team
-
Mailing list archive
-
Message #23750
Re: [Bug 793948] [NEW] Cannot subclass subclasses of dolfin.Epression
Neilen!
There is a lot of magic going on in the Expression class. I have not checked
that subclassing a subclass of Expression works. At least it should not fail
as it does. I can take a look.
In the meantime I suggest you controll the behaviour of your Expression using
the constructor instead. Code not tested:
class E_ff(dolfin.Expression):
def __init__(self, part='real', freq=1e9, l=1.0, I=1.0):
self.part = part
self.freq = freq
self.l = l
self.I = I
def eval(self, value, x):
k0 = 2*math.pi*self.freq/c0
r = max(math.sqrt(pow(x[0],2) + pow(x[1],2) + pow(x[2],2)), 1e-20)
theta = math.acos(x[2]/r)
phi = math.atan2(x[1], x[0])
theta_hat_x = math.cos(theta)*math.cos(phi)
theta_hat_y = math.cos(theta)*math.sin(phi)
theta_hat_z = -math.sin(theta)
H_phi = 1j*k0*self.l*self.I/(math.pi*4)*math.sin(theta)
H_phi = H_phi*numpy.exp(-1j*k0*r)/r
E_theta_complex = Z0*H_phi
E_theta = getattr(E_theta_complex, self.part)
value[0] = theta_hat_x*E_theta
value[1] = theta_hat_y*E_theta
value[2] = theta_hat_z*E_theta
def value_shape(self):
return (3,)
eff_real = E_ff()
eff_imag = E_ff('imag')
On Tuesday June 7 2011 03:11:19 Neilen Marais wrote:
> Public bug reported:
>
> It seems one cannot subclass a subclass of dolfin.Expression. I'm trying
> to set up a complex expression, and the haveing differnent subclasses
> for the real and imaginary parts to avoid duplication/cut-n-paste type
> errors. Code below produces following output:
>
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent call last)
>
> /home/nmarais/akademie/dev/femcode/main/fenics_test/<ipython console> in
> <module>()
>
> /tmp/python-2033LhI.py in <module>()
> 30 def value_shape(self):
> 31 return (3,)
> 32
> ---> 33 class E_ff_Im(E_ff_Re):
> 34 part = 'imag'
>
> /home/nmarais/local/lib/python2.6/site-packages/dolfin/functions/expression
> .pyc in __new__(mcs, name, bases, dict_) 245
> 246 # remove Expression, to be added later
>
> --> 247 user_bases.remove(Expression)
> 248
> 249 # Check the user has provided either an eval or eval_cell
> method
>
>
> ValueError: list.remove(x): x not in list
>
> from __future__ import division
>
> import dolfin
> import math
> import numpy
>
> Z0 = 376.73031346177066 # Intrinsic impedance of free-space
>
> class E_ff_Re(dolfin.Expression):
> part = 'real'
> freq = 1e9
> l = 1.
> I = 1.
> def eval(self, value, x):
> k0 = 2*math.pi*self.freq/c0
> r = max(math.sqrt(pow(x[0],2) + pow(x[1],2) + pow(x[2],2)), 1e-20)
> theta = math.acos(x[2]/r)
> phi = math.atan2(x[1], x[0])
> theta_hat_x = math.cos(theta)*math.cos(phi)
> theta_hat_y = math.cos(theta)*math.sin(phi)
> theta_hat_z = -math.sin(theta)
> H_phi = 1j*k0*self.l*self.I/(math.pi*4)*math.sin(theta)
> H_phi = H_phi*numpy.exp(-1j*k0*r)/r
> E_theta_complex = Z0*H_phi
> E_theta = getattr(E_theta_complex, self.part)
> value[0] = theta_hat_x*E_theta
> value[1] = theta_hat_y*E_theta
> value[2] = theta_hat_z*E_theta
>
> def value_shape(self):
> return (3,)
>
> class E_ff_Im(E_ff_Re):
> part = 'imag'
>
> ** Affects: dolfin
> Importance: Undecided
> Status: New
--
You received this bug notification because you are a member of DOLFIN
Team, which is subscribed to DOLFIN.
https://bugs.launchpad.net/bugs/793948
Title:
Cannot subclass subclasses of dolfin.Epression
Status in DOLFIN:
New
Bug description:
It seems one cannot subclass a subclass of dolfin.Expression. I'm
trying to set up a complex expression, and the haveing differnent
subclasses for the real and imaginary parts to avoid
duplication/cut-n-paste type errors. Code below produces following
output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/nmarais/akademie/dev/femcode/main/fenics_test/<ipython console>
in <module>()
/tmp/python-2033LhI.py in <module>()
30 def value_shape(self):
31 return (3,)
32
---> 33 class E_ff_Im(E_ff_Re):
34 part = 'imag'
/home/nmarais/local/lib/python2.6/site-packages/dolfin/functions/expression.pyc in __new__(mcs, name, bases, dict_)
245
246 # remove Expression, to be added later
--> 247 user_bases.remove(Expression)
248
249 # Check the user has provided either an eval or eval_cell method
ValueError: list.remove(x): x not in list
from __future__ import division
import dolfin
import math
import numpy
Z0 = 376.73031346177066 # Intrinsic impedance of free-space
class E_ff_Re(dolfin.Expression):
part = 'real'
freq = 1e9
l = 1.
I = 1.
def eval(self, value, x):
k0 = 2*math.pi*self.freq/c0
r = max(math.sqrt(pow(x[0],2) + pow(x[1],2) + pow(x[2],2)), 1e-20)
theta = math.acos(x[2]/r)
phi = math.atan2(x[1], x[0])
theta_hat_x = math.cos(theta)*math.cos(phi)
theta_hat_y = math.cos(theta)*math.sin(phi)
theta_hat_z = -math.sin(theta)
H_phi = 1j*k0*self.l*self.I/(math.pi*4)*math.sin(theta)
H_phi = H_phi*numpy.exp(-1j*k0*r)/r
E_theta_complex = Z0*H_phi
E_theta = getattr(E_theta_complex, self.part)
value[0] = theta_hat_x*E_theta
value[1] = theta_hat_y*E_theta
value[2] = theta_hat_z*E_theta
def value_shape(self):
return (3,)
class E_ff_Im(E_ff_Re):
part = 'imag'
References