Skip to content

Instantly share code, notes, and snippets.

@thorade
thorade / isOdd.mo
Last active December 10, 2015 22:29
just a simple function for my very first gist,what a pity one can't select Modelica as language even though it is supported by pygments
function isOdd "if input value is odd return true, else false"
input Integer value;
output Boolean isOdd;
algorithm
isOdd := if mod(value, 2) <> 0 then true else false;
end isOdd;
@thorade
thorade / Modelica conditional printing.mo
Last active December 12, 2015 03:28
Modelica conditional printing
Boolean verbose;
...
assert(not verbose, "iteration steps " + String(iter), level=AssertionLevel.warning);
@thorade
thorade / polyIntersect.mo
Last active December 16, 2015 05:59
Modelica function that calculates intersection of two polynomials using Modelica.Math.Vectors.Utilities.roots
function polyIntersect
input Real[:] poly1={3,2,1,0};
input Real[:] poly2={8,7};
output Real[:,2] intersect;
protected
Integer nPoly1 = size(poly1,1);
Integer nPoly2 = size(poly2,1);
Integer nPolyShort = min(nPoly1, nPoly2);
Integer nPolyLong = max(nPoly1, nPoly2);
@thorade
thorade / sort_rotate_jpg.ps1
Last active May 13, 2016 08:46
using Powershell and IrfanView to rotate and rename all jpg files in the directory of the script
# Declare functions first
# this function reads the "date taken" from extended properties
function Get-DateTaken {
Param(
[string]$filePath
)
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $filePath
$file = Split-Path $filePath -Leaf
$shellfolder = $shell.Namespace($folder)
@thorade
thorade / isInteger.mo
Last active December 24, 2015 07:29
Modelica function that checks whether the (Real) input may be treated as an integer
function isInteger
"returns true if input u is an Integer, else false"
input Real u;
output Boolean y;
algorithm
y := if abs(u-integer(u))<Modelica.Constants.small then true else false;
end isInteger;
@thorade
thorade / logspace.mo
Last active December 27, 2015 19:19
emulate Matlabs logspace function in Modelica
function logspace
input Real a;
input Real b;
input Integer n;
output Real[n] y;
protected
Real[n] lin= linspace(a,b,n);
algorithm
y := {10^lin[i] for i in 1:n};
end logspace;
@thorade
thorade / plotyy.m
Last active December 28, 2015 21:49
create a Matlab plot with two y axes, then add a third and fourth line to it
x = -pi:.1:pi;
f1 = sin(x);
f2 = 100*cos(x);
f3 = cos(x/2);
f4 = 100*sin(x/2);
[axes,linehandle1,linehandle2] = plotyy(x, f1, x, f2);
linehandle3=line(x,f3,'Parent',axes(1),'LineWidth',2,'LineStyle','--');
linehandle4=line(x,f4,'Parent',axes(2),'LineWidth',2,'LineStyle','--');
@thorade
thorade / logmean.m
Last active December 28, 2015 21:49
Matlab function to calculate the log-mean of scalars or arrays
function m=logmean(a,b)
if abs(b-a)>1e-3
m=(b-a)./log(b./a);
else
m=(a+b)./2;
end
# This PowerShell script converts all eps files in all subfolders to pdf files
$curDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$epsFiles = Get-ChildItem -Recurse $curDir\*.eps
$texBin = "${env:ProgramFiles(x86)}\MiKTeX 2.9\miktex\bin"
Set-Alias eps2pdf $texBin\epstopdf.exe
ForEach ($file In $epsFiles){
eps2pdf $file
}
@thorade
thorade / remove_bakmo.ps1
Created October 22, 2014 07:28
PowerShell script that recursively deletes all files with extension .bak-mo
# This Windows PowerShell script
# recursively deletes Dymola *.bak-mo backup files
# from all folders below itself
# get current directory
$curDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# delete files
get-childitem $curDir -include *.bak-mo -recurse | foreach ($_) {remove-item $_.fullname}