Problem with square root function and maximun function in initial condictions in problem advencion.

11 views
Skip to first unread message

Gustavo Quintero

unread,
Apr 16, 2015, 3:08:03 PM4/16/15
to claw-...@googlegroups.com
Hello,

I'm trying to modify the initial condition in advencion_1d.py, as follow:

#Initial data
    def G(x,beta):
return np.exp((beta)*(x)**2)
    
    def F(x,y,z):
return np.sqrt(max(1.0 - (y**2)*(x - z)**2,0.0))

    def H(x):
return 1.0 - abs(10.0*(x - 0.1))

    xc = state.grid.x.centers
    beta = 334.477773; a = 0.5; z = -0.7; al = 10.0; d = 0.005
    state.q[0,:] = np.zeros(xc.shape[0])
    state.q[0,:] =  ((-0.8 <= xc)*(xc <= -0.6)*(H(xc)))+((-0.4 <= xc)*(xc <= -0.2)*(1.0))+((0.0 <= xc)*(xc <= 0.2)*(max(xc,0.0))).

This throws me the following result:

"ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"

Similarly, I want to extract the square root of xc, as follow:

    state.q[0,:] = np.zeros(xc.shape[0])
    state.q[0,:] =  ((-0.8 <= xc)*(xc <= -0.6)*(xc))+((-0.4 <= xc)*(xc <= -0.2)*(max(xc,0.0)))+((0.0 <= xc)*(xc <= 0.2)*(H(xc))) + ((0.4 <= xc)*(xc <= 0.6)*(np.sqrt(xc))), 

but I get the same error above and also this error:



Note: with other functions I have not problem, for instance: xc**2, np.sin(xc), etc.

I would really appreciate any help.

Thanks in advance.

Kyle Mandli

unread,
Apr 16, 2015, 5:45:06 PM4/16/15
to claw-...@googlegroups.com
The first problem is due to the use of the max function, you need to explicitly use Numpy’s version, i.e. np.max, for that to work. The built-in max does not know about arrays.

The second problem, given your initial conditions, I am guessing you have xc running from -0.8 to 0.2 which means that you are taking the square-root of a negative number. Numpy (and python more generally) hands back a NaN and also prints the warning you mentioned.

Kyle


> On Apr 16, 2015, at 3:06 PM, Gustavo Quintero <gustavoqu...@gmail.com> wrote:
>
> Hello,
>
> I'm trying to modify the initial condition in advencion_1d.py, as follow:
>
> #Initial data
> def G(x,beta):
> return np.exp((beta)*(x)**2)
>
> def F(x,y,z):
> return np.sqrt(max(1.0 - (y**2)*(x - z)**2,0.0))
>
> def H(x):
> return 1.0 - abs(10.0*(x - 0.1))
>
> xc = state.grid.x.centers
> beta = 334.477773; a = 0.5; z = -0.7; al = 10.0; d = 0.005
> state.q[0,:] = np.zeros(xc.shape[0])
> state.q[0,:] = ((-0.8 <= xc)*(xc <= -0.6)*(H(xc)))+((-0.4 <= xc)*(xc <= -0.2)*(1.0))+((0.0 <= xc)*(xc <= 0.2)*(max(xc,0.0))).
>
> This throws me the following result:
>
> "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
>
> Similarly, I want to extract the square root of xc, as follow:
>
> state.q[0,:] = np.zeros(xc.shape[0])
> state.q[0,:] = ((-0.8 <= xc)*(xc <= -0.6)*(xc))+((-0.4 <= xc)*(xc <= -0.2)*(max(xc,0.0)))+((0.0 <= xc)*(xc <= 0.2)*(H(xc))) + ((0.4 <= xc)*(xc <= 0.6)*(np.sqrt(xc))),
>
> but I get the same error above and also this error:
>
> <error.png>
> ​
> Note: with other functions I have not problem, for instance: xc**2, np.sin(xc), etc.
>
> I would really appreciate any help.
>
> Thanks in advance.
>
> --
> You received this message because you are subscribed to the Google Groups "claw-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to claw-users+...@googlegroups.com.
> To post to this group, send email to claw-...@googlegroups.com.
> Visit this group at http://groups.google.com/group/claw-users.
> For more options, visit https://groups.google.com/d/optout.

Gustavo Quintero

unread,
Apr 22, 2015, 11:25:53 AM4/22/15
to claw-...@googlegroups.com
The error when using the maximum function is corrected, but does not work the way I want, ie when xc takes values between 0.4 and 0.6, for every desire value to calculate my maximum between the image of xc and any other value that I of, and what the program is calcularme global maximum of the function to evaluate it for xc xc values between 0.4 and 0.6. thank..!!

David Ketcheson

unread,
Apr 22, 2015, 1:07:31 PM4/22/15
to claw-...@googlegroups.com
I don't quite understand what you have written, but my best guess is that you have confused the functionality of np.max and np.maximum.  Please carefully read the docstring for each of them; I think it will explain the behavior you are seeing.
Reply all
Reply to author
Forward
0 new messages