Plugin development: problems assigning units and returning text

Plugin development: problems assigning units and returning text - Сообщения

#1 Опубликовано: 12.02.2015 01:58:22
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Hello!

I'm making a wrapper for CoolProp library (yes, I know that Ian promised to do it, but I'm afraid there's no activity there, and I thought like helping them).

My plugin implements numeric evaluation (IPluginMathNumericEvaluation).
Everything seem OK when I simply return unitless numbers.
But when I try to assign units to the result (it is a physical properties library), it becomes complicated.

I use SMath.Manager.UnitsManager.GetCurrentUnitName() to get current built-in units names; I use SMath.Manager.Converter.ToTerms() and SMath.Math.Symbolic.Converter.ToMItem() to make proper MItem to assign to resulting TNumber's obj.Units. But it seems I do it wrong.

When the unit current name (e.g. 'K, 'm, 'J) is one-letter, it works OK. The returning result may be displayed by user using different units (say, 'F, 'in, 'kJ).
But if the unit current name is multiletter (say, 'mol, or ('J in Russian locale) 'Дж), the result is displayed correctly, but cannot be normally converted to different units; SMath handles the plugin-assigned unit as a "blob" and makes funny transformations to show "'mol/'{m^3}" as "'kmol/{m^3}".

This is the first problem.
The second is when I make functions that return strings. There seem to be no "string" type in SMath. Instead, strings are a kind of TDouble with isText set to true. I make string results as

new SMath.Math.Numeric.TDouble("\"" + String + "\"" );

This produces a number with isText properly set to true.
But when such a function is used inside other that requests textual arguments, an error is emitted that says "Here must be a string!".

What is wrong with the code?

I attach the plugin source code here. The test file debug.sm is in the solution's root directory. The solution is made with VS 2013 Community. After building the debug version, it tries to copy plugin and CoolProp DLLs to SMath's user plugins directory. It should already exist (%appdata%\SMath\extensions\plugins\ca92ef03-c7da-4888-98ad-528482733e2f\1.0.0.0\). I haven't yet considered how the plugin should install and register itself; this is a work to do...

Thank you very much for attention, and even more for help!
coolprop_wrapper.zip (1 МиБ) скачан 67 раз(а).
С уважением, Михаил Каганский
1 пользователям понравился этот пост
Davide Carpi 13.02.2015 09:49:00
#2 Опубликовано: 12.02.2015 04:33:59
Вячеслав Мезенцев

Вячеслав Мезенцев

1402 сообщений из 1708 понравились пользователям.

Группа: Moderator

Если Давид оперативно не ответит, то можно по-русски пообщаться на эту тему на русском форуме. Мне здесь нужно вникать, давно не занимался.
Russia ☭ forever, Viacheslav N. Mezentsev
#3 Опубликовано: 12.02.2015 08:11:32
Davide Carpi

Davide Carpi

1415 сообщений из 2872 понравились пользователям.

Группа: Moderator

Nice work

My experience with numeric interface is limited to a couple of plugins, but I've not noticed obvious errors; therefore I've tried a "mixed approach" like in the GPCWrapper (code written with numeric library and I/O handled with lowlevel evaluation) and as you can see from the attachment this works fine, thus the core of your work basically it works (and the issue probably is in the IPluginMathNumericEvaluation.NumericEvaluation, or maybe we are missing something else...)

The implementation is a little bit naive, just to make the test

- input arguments are not preprocessed (overriden with values taken from your debug file)
- units in some cases uses new MItem() instead of unit(uname()) (this approach seems better but I don't have idea if it works with localizations)
2015-02-12 11_50_39-SMath Studio Desktop - [debug.sm_].png
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 пользователям понравился этот пост
Mike Kaganski 12.02.2015 11:26:00
#4 Опубликовано: 12.02.2015 11:37:52
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Thank you Davide!

I seem to have solved the units problem.
Unfortunately, the new MItem(string) doesn't handle localization; thus I had to keep using the long way.
It turned out that it is crucial to call Expression.SimplifyEx() upon MItem. And taking into account that there's no context in numeric evaluation available (or I miss it), I had to split all the units to basic SI parts (e.g. Pa -> kg/{m*s^2}). Then the units work as intended, and localization works.

The second problem (text that is "not text") seem to be over my skills; hope that someone could shed some light there.

Other than that (unimportant?) issue, the plugin is ready to use. I post the source here, in case someone could make some suggestions.

I don't know how to install it properly; is there some procedure for that?

I don't intend to support it in the future. I'll handle it to CoolProp library makers, and if they wish, they will merge it with their other wrappers and (hopefully) keep it in sync with the library.

Thanks again!
coolprop_wrapper.zip (1 МиБ) скачан 54 раз(а).
С уважением, Михаил Каганский
#5 Опубликовано: 12.02.2015 13:30:40
Davide Carpi

Davide Carpi

1415 сообщений из 2872 понравились пользователям.

Группа: Moderator

You're welcome

Probably Andrey knows a better way to handle units, but your approach seems it works :d

Usually to develop plugins I (manually) copy the libraries to the Plugins directory of the executable, thus I can use the automatic numbering without looking in the %APPDATA% folder (but this may be matter of taste)

I've uploaded a copy in the EM (marked as Beta Version 0.1.*) that should install all that is needed without additional efforts from the users.

BTW I have no idea on what could be different from the Viacheslav plugin CoolProp for PTC Mathcad, any hint to provide documentation/improvements is welcome.


EDIT: Code uploaded in the SVN repository (CoolPropWrapper), to make it easily available for further changes.
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 пользователям понравился этот пост
Mike Kaganski 12.02.2015 19:23:00
#6 Опубликовано: 12.02.2015 19:23:20
Mike Kaganski

Mike Kaganski

184 сообщений из 434 понравились пользователям.

Группа: User

Thanks again!

Different is that this is native SMath plugin, that doesn't depend on Mathcad EFI, only on the CoolProp library itself. It may be used both with 32- and 64-bit .NET (as the CoolProp library is available for both 32-bit and 64-bit).

Now I need to analyze the comments that Vyacheslav has made in Russian topic. Maybe I need to use a different approach to simplify units handling (Vyacheslav suggested using ExpressionEvaluation instead of NumericEvaluation). Though initially I tried to follow that path, and it seemed to me that it was more complicated.
С уважением, Михаил Каганский
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений