# Square wave generator for MECSim¶

The aim here is to mimic a square wave based on an input dc ramp from E_start to E_rev with a given scan rate. The ramp will be linear from E_start to E_rev and back to E_start with these parameters changed along with the scan rate.

Output from script is the N_ac signals to add to the Master.inp file to perform this hack. MECSim is not designed for square waves as the function has no derivatives at the edges of the square wave, so a smooth version must be used. This notebook will output these terms as well as a PNG image file.

## Parameters used for square wave(s)¶

In [1]:
E_start = 1.5    # Starting voltage (V)
E_rev = 0.5     # reverse voltage (V)
n_cycles = 1     # number of cycles of E_start to E_rev
vscan = 1.0      # V/s
N_squares = 2     # number of square waves
N_ac = 100        # number of AC sinusoids to add to make ramp look like a square wave


Import python packages as define some functions

In [2]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt

In [3]:
def ConstSquareWave(Amp = 1, T = 1, nres = 10):
a = np.zeros(nres)
f = np.zeros(nres)
for i in range(0,nres):
n = i*2+1
a[i] = 4.0/(np.pi * n)*1000 # output in mV
f[i] = n / (2.0 * T)
return a, f

In [4]:
def EvaluateEAC(ACAmp, ACFreq, time):
x = ACAmp*1.0e-3*np.sin(2.0*np.pi*ACFreq*time)
return x
def EvaluateRamp(Es, Er, vs, time):
DE = Es - Er
T = abs(DE) / vs
if(time < T):
x = Es - DE*time/T
else:
x = Er + DE*(time - T)/T
return x

In [5]:
def PrintSquareWave(a,f,nres=10):
print "Copy the following lines into Master.inp"
print nres,"    ! number of species (need n lines below)"
for i in range(0,nres):
print a[i],", ",f[i],"  ! AC sin wave: amp (mV), freq(Hz) (REPEAT)"
return


Re-calculate E_start (= E_end), E_rev and the scan rate

Minor change in these inputs is required to get a clean fit.

In [6]:
E_mid = 0.5*( E_start + E_rev )
DE = E_start - E_rev
T = n_cycles * DE / vscan
amp, freq = ConstSquareWave(DE, T/N_squares, N_ac)
DE_new = 1.0e-6
E_start_new = E_mid + DE_new/2.0
E_rev_new = E_mid - DE_new/2.0
vscan_new = DE_new / T
period_square = 2*T #/ N_squares


Plot the resulting approximate square wave to file

More accurate the larger the number of ac sinusoids added (N_ac).

In [7]:
time = np.arange(0, period_square, period_square/1000)
Eapp = np.arange(0, period_square, period_square/1000)
Es = E_start_new
Er = E_rev_new
vs = vscan_new
i = -1
for tt in time:
i=i+1
Eapp[i] = EvaluateRamp(Es, Er, vs, tt)
EAC = 0.0
for j in range(0,N_ac):
EAC = EAC + EvaluateEAC(amp[j], freq[j], tt)
Eapp[i] = Eapp[i] + EAC

plt.figure(figsize=(10,8), dpi=100)
plt.plot(time,Eapp)
plt.xlabel('Time (s)')
plt.ylabel('Voltage (V)')
plt.savefig('SquareWave.png')


## Output MECSim inputs¶

In [8]:
print "Change the following variables in Master.inp:"
print E_start_new, "     ! E_start (V)"
print E_rev_new, "     ! E_rev (V)"
print vscan_new, "     ! scan speed (V/s)"
print
PrintSquareWave(amp,freq,N_ac)

Change the following variables in Master.inp:
1.0000005      ! E_start (V)
0.9999995      ! E_rev (V)
1e-06      ! scan speed (V/s)

Copy the following lines into Master.inp
100     ! number of species (need n lines below)
1273.23954474 ,  1.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
424.413181578 ,  3.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
254.647908947 ,  5.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
181.891363534 ,  7.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
141.471060526 ,  9.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
115.749049521 ,  11.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
97.9415034412 ,  13.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
84.8826363157 ,  15.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
74.896443808 ,  17.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
67.0126076176 ,  19.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
60.6304545112 ,  21.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
55.3582410754 ,  23.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
50.9295817894 ,  25.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
47.1570201754 ,  27.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
43.9048118874 ,  29.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
41.0722433786 ,  31.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
38.5830165071 ,  33.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
36.3782727067 ,  35.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
34.4118795874 ,  37.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
32.6471678137 ,  39.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
31.0546230423 ,  41.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
29.6102219706 ,  43.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
28.2942121052 ,  45.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
27.0902030795 ,  47.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
25.9844805048 ,  49.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
24.9654812693 ,  51.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
24.0233876365 ,  53.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
23.1498099043 ,  55.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
22.3375358725 ,  57.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
21.5803312667 ,  59.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
20.8727794219 ,  61.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
20.2101515037 ,  63.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
19.5883006882 ,  65.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
19.0035752946 ,  67.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
18.4527470251 ,  69.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
17.9329513343 ,  71.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
17.4416375991 ,  73.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
16.9765272631 ,  75.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
16.5355785031 ,  77.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
16.1169562625 ,  79.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
15.7190067251 ,  81.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
15.3402354787 ,  83.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
14.9792887616 ,  85.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
14.6349372958 ,  87.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
14.3060623004 ,  89.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
13.9916433487 ,  91.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
13.6907477929 ,  93.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
13.4025215235 ,  95.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
13.1261808736 ,  97.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
12.8610055024 ,  99.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
12.6063321261 ,  101.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
12.361548978 ,  103.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
12.1260909022 ,  105.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
11.8994349975 ,  107.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
11.6810967407 ,  109.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
11.4706265291 ,  111.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
11.2676065906 ,  113.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
11.0716482151 ,  115.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
10.8823892712 ,  117.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
10.6994919726 ,  119.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
10.5226408656 ,  121.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
10.3515410141 ,  123.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
10.1859163579 ,  125.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
10.0255082263 ,  127.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
9.8700739902 ,  129.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
9.71938583767 ,  131.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
9.57322965966 ,  133.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
9.43140403508 ,  135.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
9.29371930464 ,  137.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
9.15999672471 ,  139.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
9.03006769316 ,  141.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.90377304011 ,  143.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.78096237748 ,  145.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.6614935016 ,  147.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.54523184386 ,  149.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.43204996513 ,  151.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.32182708977 ,  153.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.21444867571 ,  155.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.10980601742 ,  157.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
8.00779587884 ,  159.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.90832015363 ,  161.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.81128555052 ,  163.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.71660330143 ,  165.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.62418889063 ,  167.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.53396180317 ,  169.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.44584529085 ,  171.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.35976615454 ,  173.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.27565454134 ,  175.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.19344375557 ,  177.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.11307008232 ,  179.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
7.03447262285 ,  181.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.95759314063 ,  183.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.88237591749 ,  185.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.8087676189 ,  187.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.73671716791 ,  189.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.66617562689 ,  191.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.59709608671 ,  193.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.52943356274 ,  195.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.46314489713 ,  197.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)
6.39818866701 ,  199.0   ! AC sin wave: amp (mV), freq(Hz) (REPEAT)