import femagtools.mcv

mcvreader = femagtools.mcv.Reader()
mcvreader.readMcv('M270-35A.MC')

mcvreader.get_results()

"""
Explanation: Material Handling
There are 3 groups of material in Femag: iron, magnets and conductors.
Iron Material
Iron is used for the lamination of stator and rotor. Its properties are included in MC/MCV files. This is how MCV files can be read:
End of explanation
"""


import matplotlib.pyplot as plt

plt.plot(mcvreader['curve'][0]['hi'], mcvreader['curve'][0]['bi'])
plt.grid()
plt.show()

"""
Explanation: Display the B(H) curve in a linear plot:
End of explanation
"""


import femagtools.plot
femagtools.plot.mcv_hbj(mcvreader, log=True)
plt.show()

"""
Explanation: or with the plot module:
End of explanation
"""


femagtools.plot.mcv_muer(mcvreader)
plt.show()

"""
Explanation: Display the relative permeability:
End of explanation
"""


mcvData = dict(curve=[ dict(
     bi=[0.0, 0.09, 0.179, 0.267, 0.358,
         0.45, 0.543, 0.6334, 0.727,
         0.819, 0.9142, 1.0142, 1.102,
         1.196, 1.314, 1.3845, 1.433,
         1.576, 1.677, 1.745, 1.787,
         1.81, 1.825, 1.836],
 
     hi=[0.0, 22.16, 31.07, 37.25, 43.174,
          49.54, 56.96, 66.11, 78.291,
          95, 120.64, 164.6, 259.36,
          565.86, 1650.26, 3631.12, 5000, 10000,
          15000, 20000, 25000, 30000, 35000, 40000]
      )],
      ctype=femagtools.mcv.MAGCRV,
      name='m270-35a',
      desc=u"Demo Steel",
      ch=0.0,
      cw_freq=2.0,
      cw=1.68)

"""
Explanation: A new BH curve can be created from a dict:
End of explanation
"""


femagtools.plot.mcv_hbj(mcvData, log=False)
plt.show()

mcv = femagtools.mcv.MagnetizingCurve(mcvData)
mcv.writefile('m270-35a')

mcvreader.readMcv('m270-35a.MCV')

mcvreader['curve'][0]['bi']

"""
Explanation: The MCV dict must contain a list of curves. If the material is soft iron this list has exactly one dict with the 2 lists of bi and hi.
End of explanation
"""


import femagtools.tks
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
tks = femagtools.tks.read('TKS-M400-65A.txt')
femagtools.plot.mcv_hbj(tks, log=False)
plt.show()

"""
Explanation: Magnetic curve dicts can also be created from TKS files:
End of explanation
"""


ch = tks['ch']
alpha = tks['ch_freq']
cw = tks['cw']
beta = tks['cw_freq']
gamma = tks['b_coeff']

femagtools.plot.felosses(tks['losses'], (ch, alpha, cw, beta, gamma), log=False)
plt.show()

"""
Explanation: If the TKS file includes losses they are fitted with the Jordan approximation:
End of explanation
"""


magnets = [dict(name='MX-333', remanenc=1.2, relperm=1.05)]

"""
Explanation: Magnet Material
Magnets can be defined by magnetizing curve or in the linear case which is used convienently by the remanence and the relative permeability:
End of explanation
"""


magnets=[dict(name='BH53M', mcvkey='BH53M',
                     orient='mcartiso', rlen=1.0)]

"""
Explanation: In cases where this not applicabble a magnetizing curve can be referenced:
End of explanation
"""


