hanrach / p2d_fast_solver

14 stars 3 forks source link

need help for heat compute in charge and discharge cycle. #5

Open jsfeng-fudan opened 2 years ago

jsfeng-fudan commented 2 years ago

Hi@hanrach, I study to use the code to compute the heat during charge and discharge cycle. but the code do not provide example to consult with. I try to modified the code as below (naive/p2d_main_fn.py)

图片

I can obtain the Ohm heat and rxn heat, but can not arrive at reversible heat. 图片 It is appreciated if you help me to solve this problem.

cbm755 commented 2 years ago

Hi @jsfeng-fudan we can try to help. I'm not a expert at this code and @hanrach has moved on to other projects.

Can you post the complete error message?

The paper associated with this code might help too: https://arxiv.org/abs/2111.09251

jsfeng-fudan commented 2 years ago

@cbm755 Hi, thank you every much. I post the error message as below.

======================error ========================
Traceback (most recent call last):
  File "main_naive.py", line 30, in <module>
    U, voltages, temps, time = p2d_fn_short(Np, Nn, Mp, Mn, Ms, Ma, Mz, delta_t, fn, jac_fn, Iapp, 100)
  File "/data/run01/scv5096/software/p2d_fast_solver-main/naive/p2d_main_fn.py", line 97, in p2d_fn_short
    print(peq.Qrev(jvec_pe,Tvec_pe,cmat_pe,cmat_pe,51554))
  File "/data/run01/scv5096/software/p2d_fast_solver-main/model/ElectrodeEquation.py", line 94, in Qrev
    ans = F*self.a*j*T*self.entropy_change(cM, cMp,T,cmax)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/numpy/lax_numpy.py", line 6017, in deferring_binary_op
    return binary_op(self, other)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/traceback_util.py", line 162, in reraise_with_filtered_traceback
    return fun(*args, **kwargs)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/api.py", line 415, in cache_miss
    donated_invars=donated_invars, inline=inline)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/core.py", line 1616, in bind
    return call_bind(self, fun, *args, **params)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/core.py", line 1607, in call_bind
    outs = primitive.process(top_trace, fun, tracers, params)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/core.py", line 1619, in process
    return trace.process_call(self, fun, tracers, params)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/core.py", line 615, in process_call
    return primitive.impl(f, *tracers, **params)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/interpreters/xla.py", line 621, in _xla_call_impl
    *unsafe_map(arg_spec, args))
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/linear_util.py", line 262, in memoized_fun
    ans = call(fun, *args)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/interpreters/xla.py", line 698, in _xla_callable
    fun, abstract_args, pe.debug_info_final(fun, "jit"))
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/interpreters/partial_eval.py", line 1503, in trace_to_jaxpr_final
    jaxpr, out_avals, consts = trace_to_subjaxpr_dynamic(fun, main, in_avals)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/interpreters/partial_eval.py", line 1481, in trace_to_subjaxpr_dynamic
    ans = fun.call_wrapped(*in_tracers)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/linear_util.py", line 166, in call_wrapped
    ans = self.f(*args, **dict(self.params, **kwargs))
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/numpy/lax_numpy.py", line 436, in fn
    return lax_fn(x1, x2) if x1.dtype != bool_ else bool_lax_fn(x1, x2)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/lax/lax.py", line 352, in mul
    return mul_p.bind(x, y)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/core.py", line 267, in bind
    out = top_trace.process_primitive(self, tracers, params)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/interpreters/partial_eval.py", line 1278, in process_primitive
    out_avals = primitive.abstract_eval(*avals, **params)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/lax/lax.py", line 2149, in standard_abstract_eval
    return ShapedArray(shape_rule(*avals, **kwargs), dtype_rule(*avals, **kwargs),
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/lax/lax.py", line 2245, in _broadcasting_shape_rule
    raise TypeError(msg.format(name, ', '.join(map(str, map(tuple, shapes)))))
jax._src.traceback_util.UnfilteredStackTrace: TypeError: mul got incompatible shapes for broadcasting: (10,), (12,).

The stack trace below excludes JAX-internal frames.
The preceding is the original exception that occurred, unmodified.

--------------------

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main_naive.py", line 30, in <module>
    U, voltages, temps, time = p2d_fn_short(Np, Nn, Mp, Mn, Ms, Ma, Mz, delta_t, fn, jac_fn, Iapp, 100)
  File "/data/run01/scv5096/software/p2d_fast_solver-main/naive/p2d_main_fn.py", line 97, in p2d_fn_short
    print(peq.Qrev(jvec_pe,Tvec_pe,cmat_pe,cmat_pe,51554))
  File "/data/run01/scv5096/software/p2d_fast_solver-main/model/ElectrodeEquation.py", line 94, in Qrev
    ans = F*self.a*j*T*self.entropy_change(cM, cMp,T,cmax)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/numpy/lax_numpy.py", line 6017, in deferring_binary_op
    return binary_op(self, other)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/numpy/lax_numpy.py", line 436, in fn
    return lax_fn(x1, x2) if x1.dtype != bool_ else bool_lax_fn(x1, x2)
TypeError: mul got incompatible shapes for broadcasting: (10,), (12,).
======================================================

the error come from the funciton Qrev, its prototype is Qrev(j,Tc,cM, cMp,cmax). In my code ,I modified Qrev as peq.Qrev(jvec_pe,Tvec_pe,cmat_pe,cmat_pe,51554).

Thank you again !

hanrach commented 2 years ago

Hi @jsfeng-fudan, I noticed that when you call Qrev, you do peq.Qrev(jvec_pe, Tvec_pe, cmat_pe, cmat_pe, 51554). And the error message says TypeError: mul got incompatible shapes for broadcasting: (10,), (12,).. The Qrev function receives the scalar values j,T,cM, cMp' and cmax, so if you are using the function as it is, the arguments should be scalar values. To get a vector from this function, you would need to initialize a jax numpy array, and set the values of each entry. To do this, I used vmap function from jax. An example of this is here: https://github.com/hanrach/p2d_fast_solver/blob/8b1fa99ed61e947e4b48cf43a3c6d2045776fbef/naive/residual.py#L232

hanrach commented 2 years ago

If the code worked for Qohm and Qrev, I think it's an array size problem with cM and cMp. You can try using params cM = cmat[Np, :] and cMp = cmat[Np+1, :]. cM is the $c$ along $x$ at $N_p$ and cMp is the one at the ghost point, which we use to calculate the concentration at the interface.

jsfeng-fudan commented 2 years ago

Hi, @hanrach , thank you very! I modified the code as you suggested, but I still got the error message as before. I post the error :

=================================================================
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main_naive.py", line 30, in <module>
    U, voltages, temps, time = p2d_fn_short(Np, Nn, Mp, Mn, Ms, Ma, Mz, delta_t, fn, jac_fn, Iapp, 100)
  File "/data/run01/scv5096/software/p2d_fast_solver-main/naive/p2d_main_fn.py", line 108, in p2d_fn_short
    print(peq.Qrev(jvec_pe,Tvec_pe,cM,cMp,51554))
  File "/data/run01/scv5096/software/p2d_fast_solver-main/model/ElectrodeEquation.py", line 94, in Qrev
    ans = F*self.a*j*T*self.entropy_change(cM, cMp,T,cmax)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/numpy/lax_numpy.py", line 6017, in deferring_binary_op
    return binary_op(self, other)
  File "/data/home/scv5096/.conda/envs/jax/lib/python3.7/site-packages/jax/_src/numpy/lax_numpy.py", line 436, in fn
    return lax_fn(x1, x2) if x1.dtype != bool_ else bool_lax_fn(x1, x2)
TypeError: mul got incompatible shapes for broadcasting: (10,), (12,).
==================================================

I think it's an array size problem.

jsfeng-fudan commented 2 years ago

@hanrach by the way, why is the heat generated by Ohm mechanism so large? is it reasonable?

jsfeng-fudan commented 2 years ago

@hanrach hi, in runing the naive method, the alert message emerged as , /umfpack.py:721: UmfpackWarning: (almost) singular matrix! (estimated cond. number: 2.03e+14) warnings.warn(msg, UmfpackWarning) hence, is the result questionable?

jsfeng-fudan commented 2 years ago

@hanrach Hi, I haven't figured out the thermal output and SOC output. I wonder if you can write the output code for me? Thank you very much.

jsfeng-fudan commented 2 years ago

@cbm755 @hanrach I post heat generation, but the result is wrong, because it is too large for real Liion battery. 图片