Giter VIP home page Giter VIP logo

Comments (8)

michaelbynum avatar michaelbynum commented on September 22, 2024

Interesting. I've never even considered what happens to units when using differentiate.

@dallan-keylogic - can you provide a little more context about what you are trying to do? Where are these derivatives getting used?

from pyomo.

dallan-keylogic avatar dallan-keylogic commented on September 22, 2024

@michaelbynum , they're getting used as part of building up an expression for excess molar enthalpy in an activity coefficient model. Initially, I was using AD to build up the entire expression, but it generally builds expressions that are considerably larger than those I can generate by hand. Right now, I'm using it to get derivatives of other user-defined properties to avoid requiring the user to provide the derivative in addition to the property.

v = pyunits.convert(
    getattr(b, pname + "_vol_mol_solvent"), pyunits.m**3 / pyunits.mol
)
dv_dT = differentiate(
    v,
    b.temperature,
    mode=Modes.reverse_symbolic
)

eps = getattr(b, pname + "_relative_permittivity_solvent")
d_eps_dT = getattr(b, pname + "_d_relative_permittivity_solvent_dT")
A_DH = getattr(b, pname + "_A_DH")
Ix = getattr(b, pname + "_ionic_strength")
rho = ClosestApproach
# Temperature derivative of Debye Huckel term
dA_dT = -A_DH / 2 * (
    1/v * dv_dT + 3 * (1/eps) * d_eps_dT
)

from pyomo.

michaelbynum avatar michaelbynum commented on September 22, 2024

Could you use numeric differentiation instead of symbolic? Or you need symbolic?

from pyomo.

dallan-keylogic avatar dallan-keylogic commented on September 22, 2024

This is in the context of building up a big Pyomo expression, so it has to be symbolic.

from pyomo.

michaelbynum avatar michaelbynum commented on September 22, 2024

I think we could add some logic to the differentiate function that checks if an int or a float is being returned and, if so, makes it a Pyomo expression with units that match what is expected. Thoughts, @jsiirola?

from pyomo.

michaelbynum avatar michaelbynum commented on September 22, 2024

Oh, this is actually because of the way we initialize the derivative map here:

self.der_dict[node] = 0
. I think I just need to update that to include units...

from pyomo.

michaelbynum avatar michaelbynum commented on September 22, 2024

No, I think this is probably easier to handle inside of differentiate.

from pyomo.

dallan-keylogic avatar dallan-keylogic commented on September 22, 2024

Somewhat tangential to this issue, I have another entry in the "zero should be treated as being equivalent with any set of units" file:

pyunits.convert(
      N_A * (q_e)**2/(8 * pi * eps_vacuum * eps_solvent)
      * (1 + b.temperature / eps_solvent * d_eps_solvent_dT)
      * sum(
          b.mole_frac_phase_comp_true[pname, s]
          * abs(cobj(b, s).config.charge) ** 2
          / cobj(b, s).born_radius
          for s in b.params.ion_set
      ),
      to_units=units.ENERGY/units.AMOUNT
)

I have this expression as part of an eNRTL expression, and have just debugged an issue arising when b.params.ion_set is empty. The summation, instead of picking up units of 1/length, returns the integer zero instead, which leads to dimensional inconsistency when the unit conversion is performed.

There's a straightforward solution in this case, however: return zero with correct units if len(b.params.ion_set) == 0, otherwise return this expression.

from pyomo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.