pylbm / pylbm

Numerical simulations using flexible Lattice Boltzmann solvers
http://pylbm.readthedocs.io
Other
145 stars 46 forks source link

Attribute Error with Sympy 1.9 #161

Closed FlorianWilhelm closed 2 years ago

FlorianWilhelm commented 2 years ago

It seems like Sympy 1.9 made some internal changes, which lead to an AttributeError: can't set attribute when running pylbm.Simulation (see full error log below). My guess is that they now use NamedTuple or something similar as an internal data structure now, which is immutable. Going back go Sympy 1.8 solved this issue.

I was running the airconditioning demo to test this. BTW, really cool project! Would you mind to also add some more comments to those examples? I guess it's the convection-diffusion, right? Or at least some pointers would be really helpful, thanks.

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/var/folders/79/04017qsd7vs6pmdn2drbx1qh0000gn/T/ipykernel_59470/1705844052.py in <module>
    118 }
    119 
--> 120 sol = pylbm.Simulation(dico)
    121 
    122 # create the viewer to plot the solution

~/.mambaforge/envs/lb/lib/python3.8/site-packages/pylbm/simulation.py in __init__(self, dico, sorder, dtype, check_inverse, initialize)
     98         Monitor.set_size(domain_size)
     99 
--> 100         self.scheme = Scheme(dico, check_inverse=check_inverse, need_validation=False)
    101         if self.domain.dim != self.scheme.dim:
    102             log.error('Solution: the dimension of the domain and of the scheme are not the same\n')

~/.mambaforge/envs/lb/lib/python3.8/site-packages/pylbm/scheme.py in __init__(self, dico, check_inverse, need_validation)
    157 
    158         self._check_entry_size(scheme, 'relaxation_parameters')
--> 159         self.s = SymbolicVector([r for s in scheme for r in s['relaxation_parameters']])
    160 
    161         # TODO: add the possibility to have vectorial schemes when M matrix is defined

~/.mambaforge/envs/lb/lib/python3.8/site-packages/sympy/matrices/repmatrix.py in __new__(cls, *args, **kwargs)
    315 
    316     def __new__(cls, *args, **kwargs):
--> 317         return cls._new(*args, **kwargs)
    318 
    319     @classmethod

~/.mambaforge/envs/lb/lib/python3.8/site-packages/pylbm/symbolic.py in _new(cls, copy, *args, **kwargs)
     38         # if cols != 1:
     39         #     raise ShapeError("SymVector input must be a list")
---> 40         self._mat = flat_list
     41         return self
     42 

AttributeError: can't set attribute
gouarin commented 2 years ago

Thanks to have reported this issue. We will fix it soon and make a new release.

And sorry for the late reply!

FlorianWilhelm commented 2 years ago

Hi @gouarin, thanks for taking care of this 🙂 . Could you also give me a reference to the airconditioning demo? I would love to understand the math some more. This would be really appreciated. Thank you.

gouarin commented 2 years ago

@bgraille could you reply since you have implemented this example?

FlorianWilhelm commented 2 years ago

@bgraille is it the paper Application of a lattice Boltzmann method combined with a Smagorinsky turbulence model to spatially resolved heat flux inside a refrigerated vehicle?

bgraille commented 2 years ago

Hi @FlorianWilhelm. For this simulation, we use a vectorial scheme (D2Q9 for the fluid and D2Q5 for the temperature) to discretize the Boussinesq approximation. The reference paper is the following: http://dx.doi.org/10.1016/j.camwa.2012.07.001

FlorianWilhelm commented 2 years ago

Thanks @bgraille!

wusheng-ws commented 2 years ago

hello ,could you tell me why this code didn't work? i use the 'M' instead of 'polynomials' in 'schemes'

`import numpy as np import sympy as sp import pylab as plt %matplotlib inline from mpl_toolkits.axes_grid1 import make_axes_locatable import pylbm

u, X, Y ,LA= sp.symbols('u, X, Y,LA')

def solution(x, y, t, k, l): return np.sin(knp.pix)np.sin(lnp.piy)np.exp(-(k2+l2)*np.pi*2mu*t)

def plot(i, j, z, title): im = axarr[i,j].imshow(z) divider = make_axes_locatable(axarr[i, j]) cax = divider.append_axes("right", size="20%", pad=0.05) cbar = plt.colorbar(im, cax=cax, format='%6.0e') axarr[i, j].xaxis.set_visible(False) axarr[i, j].yaxis.set_visible(False) axarr[i, j].set_title(title)

xmin, xmax, ymin, ymax = 0., 1., 0., 1. N = 128 mu = 1. Tf = .1 dx = (xmax-xmin)/N # spatial step la = 1./dx s1 = 2./(1+4*mu) s2 = 1. k, l = 1, 1 # number of the wave

dico = { 'box': {'x':[xmin, xmax], 'y':[ymin, ymax], 'label': 0}, 'space_step': dx, 'scheme_velocity': la, 'schemes':[ { 'velocities': list(range(5)), 'conserved_moments': u, 'M':s[p.eye(),

'polynomials': [1, X/LA, Y/LA, (X2+Y2)/(2*LA2), (X2-Y*2)/(2LA**2)],

        'equilibrium': [u, 0., 0., .5*u, 0.],
        'relaxation_parameters': [0., s1, s1, s2, s2],
    }
],
'init': {u: (solution, (0., k, l))},
'boundary_conditions': {
    0: {'method': {0: pylbm.bc.AntiBounceBack,}},
},
'generator': 'cython',
'parameters': {LA: la},

}

sol = pylbm.Simulation(dico) x = sol.domain.x y = sol.domain.y

f, axarr = plt.subplots(2, 2) f.suptitle('Heat equation', fontsize=20)

plot(0, 0, sol.m[u].copy(), 'initial')

while sol.t < Tf: sol.one_time_step()

sol.f2m() z = sol.m[u] ze = solution(x[:, np.newaxis], y[np.newaxis, :], sol.t, k, l) plot(1, 0, z, 'final') plot(0, 1, ze, 'exact') plot(1, 1, z-ze, 'error')

plt.show()`

wusheng-ws commented 2 years ago

@FlorianWilhelm

FlorianWilhelm commented 2 years ago

I have no clue, I am no expert in pylbm. That being said, isn't 'polynomials' just a necessary attribute? And please don't misuse this issue for something completely different. Open up a new one.

gouarin commented 2 years ago

@wusheng-ws could you open an issue with your problem or chat with us on gitter https://gitter.im/pylbm/pylbm

gouarin commented 2 years ago

@FlorianWilhelm This issue is fixed in https://github.com/pylbm/pylbm/pull/162

And I made a new release(0.8.0) of pylbm wth the patch.

FlorianWilhelm commented 2 years ago

Thanks @gouarin!