Eomys / pyleecan

Electrical engineering open-source software providing a user-friendly, unified, flexible simulation framework for the multiphysic design and optimization of electrical machines and drives
https://www.pyleecan.org
Apache License 2.0
155 stars 130 forks source link

Issues with Datakeeper object in Tutorials #588

Open SupaDupaTwo opened 1 year ago

SupaDupaTwo commented 1 year ago

Hello, I encounter some errors while trying to understand the tutorials for operating point and Optimization. Initialization of the DataKeeper object fails. Can you spot the issue?

In tuto_Operating_point.ipynb as well as in tuto_Optimization.ipynb

TypeError                                 Traceback (most recent call last)
Cell In[4], line 12
      8     # Return the first torque harmonic
      9     return harm_list[1] 
     11 my_obj = [
---> 12     OptiObjective(
     13         name="Maximization of the average torque",
     14         symbol="Tem_av",
     15         unit="N.m",
     16         keeper="lambda output: -abs(output.mag.Tem_av)",  # keeper can be saved
     17     ),

TypeError: 'property' object is not callable

TypeError                                 Traceback (most recent call last)
Cell In[21], line 41
     38 varload.is_reuse_femm_file=True  
     40 # Add I0/Phi0 to the data available at the end of the simulation
---> 41 I0_dk = DataKeeper(
     42     name="Stator current rms amplitude",
     43     symbol="I0",
     44     unit="Arms",
     45     keeper="lambda output: output.elec.OP.get_I0_Phi0()['I0']",
     46 )

TypeError: 'property' object is not callable

BonneelP commented 1 year ago

Hello, The code looks right to me but we have recently merged a modification on the DataKeeper/Optimization so maybe we missed something. Which version of pyleecan are you using ? Are you running the latest version of the tutorial notebook without modifications ? Best regards, Pierre

SupaDupaTwo commented 1 year ago

I pulled the commit bda513c8 on the master branch Python 3.10.9 Pyleecan version:1.4.2 SciDataTool version:2.5.0

I did not modify tuto_Optimizaton.ipynb but I did modify tuto_Operating_point.ipynb -varload.set_OP_matrix(OP_matrix, "N0", "I0", "Phi0", "Tem") +varload.set_OP_array(OP_matrix, "N0", "I0", "Phi0", "Tem")

dyf502 commented 1 year ago

Is the problem solved? I have the same problem,I am running the latest version of the tutorial notebook without modifications. Python version 3.8.10 Pyleecan version:1.4.2 SciDataTool version:2.5.0

faraz4714 commented 1 year ago

hello, when I run a code in the tutorial I got an error in Datakeeper class and I don't know why? this is my code and the error

`from pyleecan.Classes.VarLoadCurrent import VarLoadCurrent
from pyleecan.Classes.DataKeeper import DataKeeper
from numpy import zeros, ones, linspace, array, sqrt, arange
# Create new Variable speed simulation
simu_vop = simu_op.copy()
simu_vop.mag.import_file = None
varload = VarLoadCurrent()
simu_vop.var_simu = varload

# Creating the Operating point matrix
OP_matrix = zeros((N_simu,4))

# Set N0 = 1000 [rpm] for all simulation
OP_matrix[:,0] = 1000 * ones((N_simu))

# Set I0 = 250 / sqrt(2) [A] (RMS) for all simulation
OP_matrix[:,1] = I0_rms * ones((N_simu))

# Set Phi0 from 60° to 180°
OP_matrix[:,2] = Phi0_ref

# Set reference torque from Yang et al, 2013
OP_matrix[:,3] = Tem_av_ref
print("Operating Point matrix:")
print(OP_matrix)
print("Original Operating point:")
print(simu_vop.input.OP.get_I0_Phi0())

# Column name are given in the order of OP_matrix
varload.set_OP_array(OP_matrix, "N0", "I0", "Phi0", "Tem")

# The operating point of the simulation is automatically set as
# the first one from the OP_matrix (to speed-up computation due to how pyleecan works)
print("Updated Operating point:")
print(simu_vop.input.OP.get_I0_Phi0())

# All the simulation use the same machine
# No need to draw the machine for all OP
varload.is_reuse_femm_file=True

# Add I0/Phi0 to the data available at the end of the simulation
I0_dk = DataKeeper(
    name="Stator current rms amplitude",
    symbol="I0",
    unit="Arms",
    keeper="lambda output: output.elec.OP.get_I0_Phi0()['I0']",
)
Phi0_dk = DataKeeper(
    name="Stator current phase",
    symbol="Phi0",
    unit="rad",
    keeper="lambda output: output.elec.OP.get_I0_Phi0()['Phi0']",
)
varload.datakeeper_list= [I0_dk, Phi0_dk]
`TypeError                                 Traceback (most recent call last)
Cell In [35], line 43
     40 varload.is_reuse_femm_file=True
     42 # Add I0/Phi0 to the data available at the end of the simulation
---> 43 I0_dk = DataKeeper(
     44     name="Stator current rms amplitude",
     45     symbol="I0",
     46     unit="Arms",
     47     keeper="lambda output: output.elec.OP.get_I0_Phi0()['I0']",
     48 )
     49 Phi0_dk = DataKeeper(
     50     name="Stator current phase",
     51     symbol="Phi0",
     52     unit="rad",
     53     keeper="lambda output: output.elec.OP.get_I0_Phi0()['Phi0']",
     54 )
     55 varload.datakeeper_list= [I0_dk, Phi0_dk]

File ~\AppData\Local\Programs\Python\Python39\lib\site-packages\pyleecan\Classes\DataKeeper.py:106, in DataKeeper.__init__(self, name, symbol, unit, keeper, error_keeper, result, result_ref, physic, init_dict, init_str)
    104 self.symbol = symbol
    105 self.unit = unit
--> 106 self.keeper = keeper
    107 self.error_keeper = error_keeper
    108 self.result = result

File ~\AppData\Local\Programs\Python\Python39\lib\site-packages\pyleecan\Classes\_frozen.py:35, in FrozenClass.__setattr__(self, key, value)
     31 if self.__isfrozen and not hasattr(self, key):
     32     raise FrozenError(
     33         self.__class__.__name__ + ' class has no "' + key + '" ' "property"
     34     )
---> 35 object.__setattr__(self, key, value)

TypeError: 'property' object is not callable
``
BonneelP commented 1 year ago

Hello,

Normally this bug is corrected on the master branch of the project. I will try to merge #616 and create a new release this week to solve this issue.

Best regards, Pierre

faraz4714 commented 1 year ago

hello, thank you for your reply. I got confused is there available any code and datakeeper class that this bug was solved or I should wait for the new release? if yes it's possible to guide me to access them

best regards, faraz

BonneelP commented 1 year ago

Hello,

You can get the code from Github using method 3: https://pyleecan.org/get.pyleecan.html You can then use the code that is not released yet and change it if needed. If you are not familiar with python or git, then you can wait for the release and you will just need to run pip install again.

Best regards, Pierre

BonneelP commented 1 year ago

Hello,

I just release pyleecan1.5.0 and updated all the tutorials on the website (there is a new loss tutorial). Do you need further help or can we close this issue ?

Best regards, Pierre

faraz4714 commented 1 year ago

Hello, Thank you for your help in advance, I used pip install pyleccan and now I have version 1.4.2 but I have the same problem before and I don't know how to fix it if is it possible for you please help me?

Best regards, Faraz

SupaDupaTwo commented 1 year ago

I still wonder what python version you are using, some package are quite old. I tried installing pyleecan 1.50 in a new environement and I have error with matplotlib and CedMrnl fork of swat-em (btw, what are the difference with the pip version of swat-em?)

     copying lib\matplotlib\mpl-data\fonts\afm\cmr10.afm -> build\lib.win-amd64-cpython-310\matplotlib\mpl-data\fonts\afm
      UPDATING build\lib.win-amd64-cpython-310\matplotlib\_version.py
      set build\lib.win-amd64-cpython-310\matplotlib\_version.py to '3.3.4'
      running build_ext
      error: Failed to download FreeType. Please download one of ['https://downloads.sourceforge.net/project/freetype/freetype2/2.6.1/freetype-2.6.1.tar.gz', 'https://download.savannah.gnu.org/releases/freetype/freetype-2.6.1.tar.gz'] and extract it into build\freetype-2.6.1 at the top-level of the source repository.
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.

  ERROR: Failed building wheel for matplotlib

Failed to build matplotlib
Cloning https://gitlab.com/CedMrnl/swat-em.git to c:\users\david.buehlmann\appdata\local\temp\pip-req-build-434ia23a
  Running command git clone --filter=blob:none --quiet https://gitlab.com/CedMrnl/swat-em.git 'C:\Users\david.buehlmann\AppData\Local\Temp\pip-req-build-434ia23a'
  fatal: unable to access 'https://gitlab.com/CedMrnl/swat-em.git/': SSL certificate problem: unable to get local issuer certificate
BonneelP commented 1 year ago

Hello,

As discussed in other issues, pyleecan is not (yet) compatible with Python 3.11 so I have added this limitation in pip. The issue is that as 1.5.0 is flagged as not available for Python 3.11, while using pip it revert back to the previous version which is 1.4.2 (which is not flagged as incompatible with 3.11) Regarding the modification for swat-em, we have an opened PR on the project that changes PyQt5 import to qtpy import since we are using PySide2.

Best regards, Pierre

BonneelP commented 1 year ago

By the way I'm personally working on Python 3.8.10

faraz4714 commented 1 year ago

Hello, I downgrade my python to 3.8.10 and now I have pyleecan 1.5 but now there is a strange error. i used pip install pyleecan more than one but there is this error

`%matplotlib inline

# Load the machine
from os.path import join
from pyleecan.Functions.load import load
from pyleecan.definitions import DATA_DIR

IPMSM_A = load(join(DATA_DIR, "Machine", "Toyota_Prius.json"))
# In Jupyter notebook, we set is_show_fig=False to skip call to fig.show() to avoid a warning message
# All plot methods return the corresponding matplotlib figure and axis to further edit the resulting plot
fig, ax = IPMSM_A.plot(is_show_fig=False)  
`ModuleNotFoundError                       Traceback (most recent call last)
Cell In[3], line 5
      3 # Load the machine
      4 from os.path import join
----> 5 from pyleecan.Functions.load import load
      6 from pyleecan.definitions import DATA_DIR
      8 IPMSM_A = load(join(DATA_DIR, "Machine", "Toyota_Prius.json"))

File ~\AppData\Local\Programs\Python\Python38\Lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py:142, in _import(name, *args, **kwargs)
    140     flag = -1
    141 pyside_feature_dict[importing_module] = flag
--> 142 return original_import(name, *args, **kwargs)

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Functions\load.py:8
      6 from .Load.import_class import import_class
      7 from .Load.load_hdf5 import load_hdf5
----> 8 from .Load.load_json import load_json
      9 from .Load.load_pkl import load_pkl
     10 from .Load.retrocompatibility import convert_init_dict

File ~\AppData\Local\Programs\Python\Python38\Lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py:142, in _import(name, *args, **kwargs)
    140     flag = -1
    141 pyside_feature_dict[importing_module] = flag
--> 142 return original_import(name, *args, **kwargs)

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Functions\Load\load_json.py:4
      2 from json import load as jload
      3 from os.path import isdir, isfile, splitext
----> 4 from ...Generator import PYTHON_DEFAULT_ENCODING
      7 def load_json(file_path):
      8     """Load a json file
      9 
     10     Parameters
   (...)
     20         data of the json file
     21     """

File ~\AppData\Local\Programs\Python\Python38\Lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py:142, in _import(name, *args, **kwargs)
    140     flag = -1
    141 pyside_feature_dict[importing_module] = flag
--> 142 return original_import(name, *args, **kwargs)

ModuleNotFoundError: No module named `'pyleecan.Generator'``
BonneelP commented 1 year ago

Hello,

I seems that a file is missing in the pip release. I will check this afternoon and if needed I will release 1.5.1.

Best regards, Pierre

BonneelP commented 1 year ago

Hello,

Indeed a file was missing so I has uploaded release 1.5.1 I installed pyleecan v1.5.1 with pip and I managed to run the FEMM simulation tutorial.

Thank you for spotting this. Best regards, Pierre

faraz4714 commented 1 year ago

hello, thank you for your help, I have another error in vtkExtractEdges could you please help me ? Best regards, Faraz

`All_core = out2.loss["stator core"] + out2.loss["rotor core"]
All_core.parent = out2.loss  # Needed to plot
All_core.plot_mesh(group_names=["stator core", "rotor core"])` 
ImportError                               Traceback (most recent call last)
Cell In[50], line 3
      1 All_core = out2.loss["stator core"] + out2.loss["rotor core"]
      2 All_core.parent = out2.loss  # Needed to plot
----> 3 All_core.plot_mesh(group_names=["stator core", "rotor core"])

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Methods\Output\OutLossModel\plot_mesh.py:13, in plot_mesh(self, group_names)
      1 def plot_mesh(self, group_names=None):
      2     """Plot the losses on the mesh solution
      3 
      4     Parameters
   (...)
     10 
     11     """
---> 13     self.get_mesh_solution().plot_contour(
     14         "freqs=sum",
     15         label=self.name + " loss density [W/m^3]",
     16         group_names=group_names,
     17     )

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Classes\MeshSolution.py:159, in MeshSolution.<lambda>(x)
    156 # cf Methods.Mesh.MeshSolution.plot_contour
    157 if isinstance(plot_contour, ImportError):
    158     plot_contour = property(
--> 159         fget=lambda x: raise_(
    160             ImportError(
    161                 "Can't use MeshSolution method plot_contour: " + str(plot_contour)
    162             )
    163         )
    164     )
    165 else:
    166     plot_contour = plot_contour

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Classes\_check.py:347, in raise_(ex)
    345 def raise_(ex):
    346     """Function to raise an exeption for the method import lambda"""
--> 347     raise ex

ImportError: Can't use MeshSolution method plot_contour: cannot import name 'vtkExtractEdges' from 'vtkmodules.vtkFiltersExtraction' (c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\vtkmodules\vtkFiltersExtraction.cp38-win_amd64.pyd)
BonneelP commented 1 year ago

Hello,

Which version of VTK are you using ? On my side I have: vtk 9.0.3 pyvista 0.31.3 pyvistaqt 0.5.0 meshio 4.4.6

Best regards, Pierre

faraz4714 commented 1 year ago

hello thank you for your response i set all my libraries as you but I get new error related to numpy

AttributeError                            Traceback (most recent call last)
Cell In[14], line 1
----> 1 out.loss.loss_dict["overall"].plot_mesh()  # overall on full mesh

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Methods\Output\OutLossModel\plot_mesh.py:13, in plot_mesh(self, group_names)
      1 def plot_mesh(self, group_names=None):
      2     """Plot the losses on the mesh solution
      3 
      4     Parameters
   (...)
     10 
     11     """
---> 13     self.get_mesh_solution().plot_contour(
     14         "freqs=sum",
     15         label=self.name + " loss density [W/m^3]",
     16         group_names=group_names,
     17     )

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Methods\Mesh\MeshSolution\plot_contour.py:164, in plot_contour(self, label, index, indices, is_surf, is_radial, is_center, clim, field_name, group_names, save_path, itimefreq, is_show_fig, win_title, factor, is_animated, title, p, colormap, *args)
    161         clim[0] = -abs(clim[1])
    162         clim[1] = abs(clim[1])
--> 164 plot_mesh_field(
    165     p,
    166     sargs,
    167     field_name,
    168     clim=clim,
    169     mesh_pv=mesh_pv,
    170     field=field,
    171     colormap=colormap,
    172 )
    174 ###########
    175 # Internal animation (cannot be combined with other plots)
    176 if is_animated:

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyleecan\Functions\Plot\Pyvista\plot_mesh_field.py:24, in plot_mesh_field(p, sargs, field_name, clim, mesh_pv, field, phase, colormap)
     13 def plot_mesh_field(
     14     p,
     15     sargs,
   (...)
     21     colormap=COLOR_MAP,
     22 ):
---> 24     mesh_pv[field_name] = real(field * phase)
     25     mesh_field = mesh_pv
     27     p.add_mesh(
     28         mesh_field,
     29         scalars=field_name,
   (...)
     33         scalar_bar_args=sargs,
     34     )

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyvista\core\dataset.py:654, in DataSet.__setitem__(self, name, scalars)
    652     self.point_arrays[name] = scalars
    653 elif scalars.shape[0] == self.n_cells:
--> 654     self.cell_arrays[name] = scalars
    655 else:
    656     # Field data must be set explicitly as it could be a point of
    657     # confusion for new users
    658     raise_not_matching(scalars, self)

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyvista\core\datasetattributes.py:57, in DataSetAttributes.__setitem__(self, key, value)
     55 def __setitem__(self, key: str, value: np.ndarray):
     56     """Implement setting with the [] operator."""
---> 57     self.append(narray=value, name=key)

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyvista\core\datasetattributes.py:247, in DataSetAttributes.append(self, narray, name, deep_copy, active_vectors, active_scalars)
    242 # this handles the case when an input array is directly appended on the
    243 # output. We want to make sure that the array added to the output is not
    244 # referring to the input dataset.
    245 copy = pyvista_ndarray(narray)
--> 247 vtk_arr = helpers.convert_array(copy, name, deep=deep_copy)
    248 self.VTKObject.AddArray(vtk_arr)
    249 try:

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\pyvista\utilities\helpers.py:116, in convert_array(arr, name, deep, array_type)
    113 else:
    114     # This will handle numerical data
    115     arr = np.ascontiguousarray(arr)
--> 116     vtk_data = _vtk.numpy_to_vtk(num_array=arr, deep=deep,
    117                                  array_type=array_type)
    118 if isinstance(name, str):
    119     vtk_data.SetName(name)

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\vtkmodules\util\numpy_support.py:164, in numpy_to_vtk(num_array, deep, array_type)
    161 result_array.SetNumberOfTuples(shape[0])
    163 # Ravel the array appropriately.
--> 164 arr_dtype = get_numpy_array_type(vtk_typecode)
    165 if numpy.issubdtype(z.dtype, arr_dtype) or \
    166    z.dtype == numpy.dtype(arr_dtype):
    167     z_flat = numpy.ravel(z)

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\vtkmodules\util\numpy_support.py:94, in get_numpy_array_type(vtk_array_type)
     92 def get_numpy_array_type(vtk_array_type):
     93     """Returns a numpy array typecode given a VTK array type."""
---> 94     return get_vtk_to_numpy_typemap()[vtk_array_type]

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\vtkmodules\util\numpy_support.py:74, in get_vtk_to_numpy_typemap()
     72 def get_vtk_to_numpy_typemap():
     73     """Returns the VTK array type to numpy array type mapping."""
---> 74     _vtk_np = {vtkConstants.VTK_BIT:numpy.bool,
     75                 vtkConstants.VTK_CHAR:numpy.int8,
     76                 vtkConstants.VTK_SIGNED_CHAR:numpy.int8,
     77                 vtkConstants.VTK_UNSIGNED_CHAR:numpy.uint8,
     78                 vtkConstants.VTK_SHORT:numpy.int16,
     79                 vtkConstants.VTK_UNSIGNED_SHORT:numpy.uint16,
     80                 vtkConstants.VTK_INT:numpy.int32,
     81                 vtkConstants.VTK_UNSIGNED_INT:numpy.uint32,
     82                 vtkConstants.VTK_LONG:LONG_TYPE_CODE,
     83                 vtkConstants.VTK_LONG_LONG:numpy.int64,
     84                 vtkConstants.VTK_UNSIGNED_LONG:ULONG_TYPE_CODE,
     85                 vtkConstants.VTK_UNSIGNED_LONG_LONG:numpy.uint64,
     86                 vtkConstants.VTK_ID_TYPE:ID_TYPE_CODE,
     87                 vtkConstants.VTK_FLOAT:numpy.float32,
     88                 vtkConstants.VTK_DOUBLE:numpy.float64}
     89     return _vtk_np

File c:\users\faraz\appdata\local\programs\python\python38\lib\site-packages\numpy\__init__.py:305, in __getattr__(attr)
    300     warnings.warn(
    301         f"In the future `np.{attr}` will be defined as the "
    302         "corresponding NumPy scalar.", FutureWarning, stacklevel=2)
    304 if attr in __former_attrs__:
--> 305     raise AttributeError(__former_attrs__[attr])
    307 # Importing Tester requires importing all of UnitTest which is not a
    308 # cheap import Since it is mainly used in test suits, we lazy import it
    309 # here to save on the order of 10 ms of import time for most users
    310 #
    311 # The previous way Tester was imported also had a side effect of adding
    312 # the full `numpy.testing` namespace
    313 if attr == 'testing':

AttributeError: module 'numpy' has no attribute 'bool'.
`np.bool` was a deprecated alias for the builtin `bool`. To avoid this error in existing code, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:
    https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
SupaDupaTwo commented 1 year ago

I think with issue comes from Pyvista. I had a similar issue and I had to change the import in Lib/site-packages/pyvista/_vtk.py, vtkExtractEdges is in vtkmodules.vtkFiltersCore now. In get_cell.py I had also to change the import -> collections.abc import Iterable

BonneelP commented 1 year ago

Hello,

Thank you for the answer. I'm not sure if understand perfectly your answer. Should we change some import within pyleecan or is it modifications to do in the site_package of VTK ? In the first case do you have the time to create a PR ? I can release 1.5.2 afterwards if needed. By the way, I'm using numpy 1.23.1

Best regards, Pierre

SupaDupaTwo commented 1 year ago

The issue is within pyvista I think. To solve it I changed the site_package of pyvista. A better option should be to upgrade pyvista to a newer version. If it is possible with the other requirements of pyleecan. https://github.com/pyvista/pyvista/pull/2585.

\pyleecan\Methods\Mesh\MeshMat\get_cell.py had a issue of import of the collections module but I think it is fixed in 1.50 I am running the unit test with the latesta pyvista to see it create new issue

faraz4714 commented 1 year ago

hello, thank you for your answer I change the versions of the numpy and matplotlib and now work correctly Best regards, Faraz Mirali