linopy: multiply array of variables by a scalar array

162 views
Skip to first unread message

barry.m...@dcu.ie

unread,
Aug 11, 2023, 12:11:56 PM8/11/23
to pypsa
Hi Folks -

I'm just experimenting with the use of linopy for custom constraints in pypsa (but from a position with little/no prior xarray understanding...). So I have a set of linopy variables, indexed by snapshot, extracted like this:

    lpmodel = network.optimize.create_model()
    charge_vars = lpmodel.variables['StorageUnit-p_store']

and I now want to set up a set of constant scalar multipliers for each of these variables, but the specific constant value varies by snapshot. I already have this set of multipliers in a pd.Series, indexed by snapshot (call it "factors"). I somewhat naively hoped I might be able to so something like:

    lhs = factors * charge_vars

but that throws:

      ValueError: different number of dimensions on data and dims: 1 vs 2

So I'm guessing/hoping that there might be some concise (?) xarray syntax that will be able to do what I want here?

Any pointers appreciated.

Kind regards - Barry


barry.m...@dcu.ie

unread,
Aug 14, 2023, 7:42:55 AM8/14/23
to pypsa
So, just to say that I have resolved this problem (I think!).  The more substantive problem (that led to the error I reported) seems to be just that linopy expression arithmetic (multiplication in this case) can be sensitive to the operand order. Simply reversing the order to:

    lhs = charge_vars * factors

fixed this, and the resulting expression read as I intended (and indeed, the custom constraint I derived from the expression worked as I intended when I ran the optimisation). 

It's a little bit counter-intuitive (one tends to expect "arithmetic" operations to be commutative), but I'm happy that it is now working.

(There was also a subsidiary issue, that was not apparent in my previous message, in that I had initially derived the 'factors' pd. Series in such a way that the index label was not 'snapshot'; but that was fixed by taking the index from network.snapshots instead - as I should have done in the first place anyway...)

Regards - Barry

Fabian Hofmann

unread,
Aug 21, 2023, 3:53:16 AM8/21/23
to py...@googlegroups.com

Hey Barry,


just for some background:

It is indeed the order of the objects which make things safer here. In Python the first object in a multiplication is the "dominant" one which determines the resulting type of the multiplication (except for multiplication with inherited classes with the use of __rmul__ as well as multiplication with numpy objects and the use of __array_priority__). For multiplication with xarray objects (with xarray on the first place), we prioritize linopy object through some sly monkey patch (https://github.com/PyPSA/linopy/blob/master/linopy/monkey_patch_xarray.py), but we did not implement it for pandas for reasons of code security.


Best wishes

Fabian Hofmann

Séanadh Ríomhphoist/Email Disclaimer

Tá an ríomhphost seo agus aon chomhad a sheoltar leis faoi rún agus is lena úsáid ag an seolaí agus sin amháin é. Is féidir tuilleadh a léamh anseo. 

This e-mail and any files transmitted with it are confidential and are intended solely for use by the addressee. Read more here.



 
--
You received this message because you are subscribed to the Google Groups "pypsa" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pypsa+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/pypsa/e87bb9b8-cf5d-49b2-8c22-9c66b690785cn%40googlegroups.com.
-- 
Fabian Hofmann 

Postdoctoral Researcher
Institute of Energy Technology
Technische Universität Berlin
http://fabianhofmann.org/

Group website: https://tub-ensys.github.io/

Barry McMullin

unread,
Aug 21, 2023, 8:32:50 AM8/21/23
to Fabian Hofmann, py...@googlegroups.com
Hi Fabian -

Thanks for that - very useful to have this confirmation and clarification (even if the "sly monkey patch" is well beyond my own technical grasp!).

Kind regards - Barry



--
Barra McMaoláin
Barry McMullin
Ollamh | Scoil na hInnealtóireachta Leictreonaí | Ollscoil Chathair Bhaile Átha Cliath 
Professor | School of Electronic Engineering | Dublin City University 

S347 |  Foirgneamh Stokes  |  Ollscoil Chathair Bhaile Átha Cliath  |  Campas Ghlas Naíon  |  Bhaile Átha Cliath D09 DD7R

S347 |  Stokes Building  |  Dublin City University  |  Glasnevin Campus  |  Dublin D09 DD7R


Reply all
Reply to author
Forward
0 new messages