Pages

Saturday, January 6, 2024

Sunday, December 1, 2019

gRPC and JSON

It is easy to provide JSON implementation in gRPC proto file but needs additional effort to setup Envoy proxy that can provide conversion between REST GET/POST/PUT requests and the gRPC:

https://blog.envoyproxy.io/envoy-and-grpc-web-a-fresh-new-alternative-to-rest-6504ce7eb880


Sunday, February 10, 2019

Kubernetes

Hello World

Following examples from https://github.com/DevOps-with-Kubernetes/examples

Starting minikube:
minikube start --vm-driver=none
Example helloworld_pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: example
spec:
  containers:
    - name: web
      image: nginx
      imagePullPolicy: Never
    - name: centos
      image: centos
      imagePullPolicy: Never
      command: ["bin/sh", "-c", "while : ; do curl http://localhost:80/ sleep 10; done"]
Run this using
kubectl create -f helloworld_pod.yaml
kubectl  get pods
kubectl logs  example -c centos

Cluster Service 

service_pod_1.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-1.12
spec:
  replicas: 2
  selector:
    project: service_clusterip
    service: web
    version: "0.1"
  template:
    metadata:
      name: nginx
      labels:
        project: service_clusterip
        service: web
        version: "0.1"
    spec:
      containers:
      - name: nginx
        image: nginx:1.12.0
        ports:
        - containerPort : 80  
service_pod_2.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-1.13
spec:
  replicas: 2
  selector:
    project: service_clusterip
    service: web
    version: "0.2"
  template:
    metadata:
      name: nginx
      labels:
        project: service_clusterip
        service: web
        version: "0.2"
    spec:
      containers:
      - name: nginx
        image: nginx:1.13.1
        ports:
        - containerPort : 80   
service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    project: service_clusterip
    service: web
  ports:
  - protocol: TCP
    port : 80
    targetPort: 80
    name: http 
Run Service
kubectl create -f service_pod_1.yaml
kubectl create -f service_pod_2.yaml
kubectl create -f service.yaml
kubectl get service
kubectl get pods
kubectl describe pods nginx-1.13-2r9rf
kubectl get endpoints 

checker.yaml

apiVersion: v1
kind: Pod
metadata:
  name: clusterip-chk
spec:
  containers:
  - name: centos
    image: centos
    command: ["/bin/sh", "-c", "while : ;do curl http://${NGINX_SERVICE_SERVICE_HOST}:80/; sleep 10; done"] 
Find the logs
kubectl logs -f clusterip-chk

Saturday, June 25, 2016

Working with MPI

Message Passing Interface is great because it provides interoperability among different programming platforms (C++, C, and Fortran). There is also support provided for Java, Perl, and Python. Working with large scale Fortran and C++ applications, it can be bit slow to use Oracle for interoperability.

I was working towards having an example working that talks between Fortran and C++.

Downloaded and installed MPI to linux machine:

http://www.mpich.org/downloads/

Found a nice example

http://stackoverflow.com/questions/11944356/send-mpi-message-from-a-c-code-to-fortran-90-code

C++ (Notice, fixed a simple typo from float to double)
# include 
# include 
# include 
using namespace std;

void printarray (double arg[], int length) {
   for (int n=0; n<length; n++)
    cout << arg[n] << " ";
  cout << "\n";
}

int main(int argc, char *argv[] ){
   double a[10];
   int myrank,i;
   MPI::Init ( argc, argv );
   myrank=MPI::COMM_WORLD.Get_rank();
   cout << "rank "<<myrank<<" is c++ rank."<<std::endl;
   for (i=0;i<10;i++){
      a[i]=10.0;
   }
   printarray(a,10);
   MPI::COMM_WORLD.Send(&a[0],1,MPI::DOUBLE_PRECISION,1,100);
   MPI::Finalize();
}
Fortran (Notice that there must be 7 spaces before writing line and if line is too long error use &. For this to work, the file has to be named as .f95
program main
implicit none
include "mpif.h"
integer:: ierr,stat(MPI_STATUS_SIZE)
real(8):: a(10)

call mpi_init(ierr)
a=0
print*,a
call mpi_recv(a(1),10,MPI_DOUBLE_PRECISION,0,100, &
MPI_COMM_WORLD,stat,ierr)
print*,a
call mpi_finalize(ierr)
end program

Compiled C++
mpic++ harmeet.cpp -o harmeet_cpp

Compiled Fortran
mpifort harmeet.f95 -o harmeet_f95

Run them both together
mpirun -n 1 ./harmeet_cpp : -n 1 harmeet_95

Notice there is a : between, otherwise we would get stuck at harmeet_cpp sending message that harmeet_95 never receives.

Sunday, September 15, 2013

Plotting Matlab Figures and linking them to Latex

I am writing my thesis which means I need to do lots of latex! I am plotting figures in matlab but it is bit tricky linking matlab figures to latex!

So, here's the perfect way to get things done!

1. Plot all the data
2. Call tightfig script to remove margins (
http://www.mathworks.com/matlabcentral/fileexchange/34055)
3. Save the file in pdf
4. Plot in latex!

Example Latex:
\begin{figure}[!ht]
 \centering
 \includegraphics[scale=0.45]{Figures/Chap2/Fault_Profile.pdf}
 \caption{Different Fault Resistances.}
 \label{fig:fault_profile}
\end{figure}
 
 
Example Matlab Script:
%impedance from substation
            impedance=[3.781923798    5.435882686    6.368358163    7.200380563    9.856371439 9.856371439    15.87650559    ...
                16.51528666    16.70110783    17.13156594    17.32078243    18.51808337    21.08402126];
           
           
            voltage_data=[25.9532622445019,25.7653016067929,25.6691255616736,25.5853983811606,25.3569910635690,25.3569868192443,24.9617555108483,...
                24.8825781473615,24.8598716415630,24.8123234278547,24.7888762649503,24.6898624381824,24.6335180770592
               
                26.0814492943857,25.9799517244703,25.9265392586507,25.8813822264662,25.7550839588058,25.7550599002203,25.5310154073116,...
                25.4821039369688,25.4685035318285,25.4401241743564,25.4258036302699,25.3646712093259,25.3299030962158
               
                26.1764298329928,26.1514908385483,26.1386241507187,26.1273111091974,26.0969883677681,26.0969850365599,26.0397214690784,...
                    26.0234564651756,26.0187420587268,26.0089169654977,26.0040301248486,25.9831659832180,25.9712770752272
                ];
           
            fig=plot(impedance',voltage_data(1,:)','kv','markersize',10);
            hold on;
            plot(impedance',voltage_data(2,:)','b*','markersize',10);
            plot(impedance',voltage_data(3,:)','ms','markersize',10);
           
            plot(xlim',ones(1,2)*25,'-.','color','black');
            text(12,25,'25 kV','background','w');
           
           
            plot(xlim',ones(1,2)*25*1.058,'-.','color','black');
            plot(xlim',ones(1,2)*25*0.95,'-.','color','black');
            plot(xlim',ones(1,2)*25*1.05,'-.','color','black');
            plot(xlim',ones(1,2)*25*0.975,'-.','color','black');
            text(11,25*1.056,'Range B Upper Limit','background','w')
            text(11,25*0.95,'Range B Lower Limit','background','w')
            text(11,25*1.05,'Range A Upper Limit','background','w')
            text(11,25*0.975,'Range A Lower Limit','background','w')
           

            xlabel ('Impedance from substation (Ohms)')
            ylabel ('Voltage (kV)')
           
            title ('System voltage profile for different system loadings');
            legend ('100%','60%','20%','Location','SouthWest');
           
            hold off;
            tightfig;
            saveas(fig,'C:\Users\hcheem2\Dropbox\Thesis\Thesis_hcheema\Figures\Chap2\Voltage_Profile.pdf','pdf');





Monday, June 3, 2013

Beware of matlab pu measurements for power systems

Note that Vbase and Ibase are defined to be peak voltage and current values as opposed to the RMS values.

http://www.mathworks.com/help/physmod/powersys/ref/threephasevimeasurement.html

This will result in the measurements to be sqrt(2) times smaller than actual values.

Thursday, October 18, 2012

Simplest matlab commands

Want to write that exp(j*t) into cos and sines?

Use the command simple

http://www.mathworks.com/help/symbolic/simple.html

What to see the results in decimals instead of fractions?

Use vpa(ans,4)

What evaluate a function with symbolic expressions?

Use subs(f)

Want to see pretty picture of your complicated equation?

type mupad

Nested symbolic sums?

Go to the mupad and type for example,
sum(sum(sum(1,i=1..j),j=n..N),n=1..N)


State Space modelling?

Step Response - http://www.mathworks.com/help/control/ref/initial.html

Impulse Response - http://www.mathworks.com/help/control/ref/impulse.html

e.g.
a = [-0.5572 -0.7814;0.7814  0];
b = [1 -1;0 2];
c = [1.9691  6.4493];
sys = ss(a,b,c,0);
impulse(sys)

Initial-
http://www.mathworks.com/help/control/ref/initial.html