cart2sphere.c

Go to the documentation of this file.
00001 /*
00002     This file is part of magpar.
00003 
00004     Copyright (C) 2002-2010 Werner Scholz
00005 
00006     www:   http://www.magpar.net/
00007     email: magpar(at)magpar.net
00008 
00009     magpar is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     magpar is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017     GNU General Public License for more details.
00018 
00019     You should have received a copy of the GNU General Public License
00020     along with magpar; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 static char const Id[] = "$Id: cart2sphere.c 2962 2010-02-04 19:50:44Z scholz $\n\n";
00025 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00026 
00027 #include "util.h"
00028 
00029 int Sphere2Cart(Vec S, Vec C)
00030 {
00031   MagparFunctionInfoBegin;
00032 
00033   int t_Clen,t_Slen;
00034   ierr = VecGetLocalSize(S,(PetscInt*)&t_Slen);CHKERRQ(ierr);
00035   ierr = VecGetLocalSize(C,(PetscInt*)&t_Clen);CHKERRQ(ierr);
00036   assert(t_Clen%ND==0);
00037   assert(t_Clen*(ND-1)/ND==t_Slen);
00038 
00039   PetscReal *ta_C,*ta_S;
00040   ierr = VecGetArray(S,&ta_S);CHKERRQ(ierr);
00041   ierr = VecGetArray(C,&ta_C);CHKERRQ(ierr);
00042 
00043   for (int i=0;i<t_Clen/ND;i++) {
00044     /* x=sin(theta)*cos(phi) */
00045     ta_C[i*ND+0]=sin(ta_S[(ND-1)*i+0])*cos(ta_S[(ND-1)*i+1]);
00046     /* y=sin(theta)*sin(phi) */
00047     ta_C[i*ND+1]=sin(ta_S[(ND-1)*i+0])*sin(ta_S[(ND-1)*i+1]);
00048     /* y=cos(theta) */
00049     ta_C[i*ND+2]=cos(ta_S[(ND-1)*i+0]);
00050   }
00051 
00052   ierr = VecRestoreArray(C,&ta_C);CHKERRQ(ierr);
00053   ierr = VecRestoreArray(S,&ta_S);CHKERRQ(ierr);
00054 
00055   MagparFunctionInfoReturn(0);
00056 }
00057 
00058 
00059 int Cart2Sphere(Vec C, Vec S)
00060 {
00061   MagparFunctionInfoBegin;
00062 
00063   int t_Clen,t_Slen;
00064   ierr = VecGetLocalSize(S,(PetscInt*)&t_Slen);CHKERRQ(ierr);
00065   ierr = VecGetLocalSize(C,(PetscInt*)&t_Clen);CHKERRQ(ierr);
00066   assert(t_Clen%ND==0);
00067   assert(t_Clen*(ND-1)/ND==t_Slen);
00068 
00069   PetscReal *ta_C,*ta_S;
00070   ierr = VecGetArray(S,&ta_S);CHKERRQ(ierr);
00071   ierr = VecGetArray(C,&ta_C);CHKERRQ(ierr);
00072 
00073   for (int i=0;i<t_Clen/ND;i++) {
00074     /* theta=acos(z) */
00075     ta_S[2*i+0]=acos(ta_C[ND*i+2]);
00076     /* phi=atan(y/x) */
00077     ta_S[2*i+1]=atan2(ta_C[ND*i+1],ta_C[ND*i+0]);
00078 
00079     /* map negative values [-Pi,0] to [Pi,2*Pi] */
00080 /*
00081     if (ta_C[ND*i+0]<0) ta_S[2*i+1] = 2.0*PETSC_PI+ta_S[2*i+1];
00082 */
00083   }
00084 
00085   ierr = VecRestoreArray(C,&ta_C);CHKERRQ(ierr);
00086   ierr = VecRestoreArray(S,&ta_S);CHKERRQ(ierr);
00087 
00088   MagparFunctionInfoReturn(0);
00089 }
00090 
00091 
00092 int Cart2SphereDiff(Vec C, Vec X, Vec S)
00093 {
00094   MagparFunctionInfoBegin;
00095 
00096   int t_Clen,t_Xlen,t_Slen;
00097   ierr = VecGetLocalSize(C,(PetscInt*)&t_Clen);CHKERRQ(ierr);
00098   ierr = VecGetLocalSize(X,(PetscInt*)&t_Xlen);CHKERRQ(ierr);
00099   ierr = VecGetLocalSize(S,(PetscInt*)&t_Slen);CHKERRQ(ierr);
00100   assert(t_Clen%ND==0);
00101   assert(t_Clen*(ND-1)/ND==t_Xlen);
00102   assert(t_Clen*(ND-1)/ND==t_Slen);
00103 
00104   PetscReal *ta_C,*ta_X,*ta_S;
00105   ierr = VecGetArray(C,&ta_C);CHKERRQ(ierr);
00106   ierr = VecGetArray(X,&ta_X);CHKERRQ(ierr);
00107   ierr = VecGetArray(S,&ta_S);CHKERRQ(ierr);
00108 
00109   /* cf. http://mathworld.wolfram.com/SphericalCoordinates.html */
00110   /* http://chsfpc5.chem.ncsu.edu/~franzen/CH795Z/math/ang_mom_op/ang_mom_op.html */
00111   for (int i=0;i<t_Clen/ND;i++) {
00112     ta_S[(ND-1)*i+0]=
00113        ta_C[ND*i+0]*cos(ta_X[(ND-1)*i+0])*cos(ta_X[(ND-1)*i+1])
00114       +ta_C[ND*i+1]*cos(ta_X[(ND-1)*i+0])*sin(ta_X[(ND-1)*i+1])
00115       -ta_C[ND*i+2]*sin(ta_X[(ND-1)*i+0]);
00116 
00117     ta_S[(ND-1)*i+1]=
00118       -ta_C[ND*i+0]*sin(ta_X[(ND-1)*i+0])*sin(ta_X[(ND-1)*i+1])
00119       +ta_C[ND*i+1]*sin(ta_X[(ND-1)*i+0])*cos(ta_X[(ND-1)*i+1]);
00120   }
00121 
00122   ierr = VecRestoreArray(C,&ta_C);CHKERRQ(ierr);
00123   ierr = VecRestoreArray(X,&ta_X);CHKERRQ(ierr);
00124   ierr = VecRestoreArray(S,&ta_S);CHKERRQ(ierr);
00125 
00126   MagparFunctionInfoReturn(0);
00127 }
00128 

magpar - Parallel Finite Element Micromagnetics Package
Copyright (C) 2002-2010 Werner Scholz