hext_py.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     Copyright (C) 2009 Amit Itagi
00006 
00007     www:   http://www.magpar.net/
00008     email: magpar(at)magpar.net
00009 
00010     magpar is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     magpar is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU General Public License for more details.
00019 
00020     You should have received a copy of the GNU General Public License
00021     along with magpar; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 */
00024 
00025 static char const Id[] = "$Id: hext_py.c 2983 2010-02-22 22:56:23Z scholz $\n\n";
00026 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00027 
00028 #include "field.h"
00029 #include "util/util.h"
00030 
00031 static int       doinit=1;
00032 static int       fieldon=0;
00033 static Vec       VHthis;
00034 static PetscReal hextamp=0.0;
00035 
00036 int Hext_py_Init(GridData *gdata)
00037 {
00038   MagparFunctionLogBegin;
00039 
00040   PetscTruth flg;
00041   char str[256];
00042   ierr = PetscOptionsGetString(PETSC_NULL,"-hext_py",str,256,&flg);CHKERRQ(ierr);
00043   if (flg!=PETSC_TRUE) {
00044     fieldon=0;
00045     PetscPrintf(PETSC_COMM_WORLD,
00046       "Option -hext_py not found, using default value: %i (field off)\n",
00047       fieldon
00048     );
00049   }
00050   else {
00051     fieldon=1;
00052   }
00053 
00054   if (!fieldon) {
00055     PetscPrintf(PETSC_COMM_WORLD,"Hext_py off\n");
00056     MagparFunctionLogReturn(0);
00057   }
00058 
00059 #ifdef PYTHON
00060   PetscPrintf(PETSC_COMM_WORLD,"Hext_py on, using Python script: %s\n",str);
00061 
00062   ierr = VecDuplicate(gdata->M,&VHthis);CHKERRQ(ierr);
00063 
00064   /* Python stuff */
00065 
00066   PyObject *pFunc;
00067   pFunc=PyObject_GetAttrString(gdata->pModule,str);  /* new reference */
00068   if(!pFunc) {
00069     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Cannot find Python function %s!\n",str);
00070   }
00071   if (!PyCallable_Check(pFunc)) {
00072     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Python object %s is not callable (not a function)!\n",str);
00073   }
00074 
00075   /* Arguments for the module */
00076   PyObject *pTuple;
00077   pTuple = PyTuple_New(ND); /* new reference */
00078 
00079   PyObject *pVal=PETSC_NULL;
00080 
00081   /* make the vector of the external field accessible */
00082   PetscReal *ta_Hext;
00083   ierr = VecGetArray(VHthis,&ta_Hext);CHKERRQ(ierr);
00084 
00085   /* loop over all (local) nodes */
00086   for (int j=0; j<gdata->ln_vert; j++) {
00087     ierr = ProgressBar(j,gdata->ln_vert,10);
00088     PetscReal x,y,z;
00089     x=gdata->vertxyz[ND*gdata->vertl2g[j]+0];
00090     y=gdata->vertxyz[ND*gdata->vertl2g[j]+1];
00091     z=gdata->vertxyz[ND*gdata->vertl2g[j]+2];
00092 
00093     PyTuple_SetItem(pTuple,0,PyFloat_FromDouble(x)); /* The tuple steals the reference */
00094     PyTuple_SetItem(pTuple,1,PyFloat_FromDouble(y));
00095     PyTuple_SetItem(pTuple,2,PyFloat_FromDouble(z));
00096 
00097     /* Call the Python function */
00098     pVal = PyObject_CallObject(pFunc,pTuple);  /* new reference */
00099 
00100     /* set external field */
00101     for (int i=0; i<ND; i++) {
00102       ta_Hext[ND*j+i]=PyFloat_AsDouble(PyTuple_GetItem(pVal,i));
00103     }
00104   }
00105   ierr = ProgressBar(1,1,10);
00106   ierr = VecRestoreArray(VHthis,&ta_Hext);CHKERRQ(ierr);
00107 
00108   /* decrement references */
00109   Py_DECREF(pFunc);
00110   Py_DECREF(pTuple);
00111   Py_DECREF(pVal);
00112 
00113   int k;
00114   ierr = VecMax(VHthis,&k,&hextamp);CHKERRQ(ierr);
00115 #else
00116   SETERRQ(PETSC_ERR_ARG_CORRUPT,"magpar not linked with Python interpreter. Cannot execute Python scripts!\n");
00117 #endif
00118 
00119   MagparFunctionLogReturn(0);
00120 }
00121 
00122 
00123 int hext_f(GridData *gdata, PetscReal time_ns)
00124 {
00125 #ifdef PYTHON
00126   MagparFunctionInfoBegin;
00127 
00128   char str[256]="hext_f";
00129   PyObject *pFunc;
00130   pFunc=PyObject_GetAttrString(gdata->pModule,str);  /* new reference */
00131   if(!pFunc) {
00132     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Cannot find Python function %s!\n",str);
00133   }
00134   if (!PyCallable_Check(pFunc)) {
00135     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Python object %s is not callable (not a function)!\n",str);
00136   }
00137 
00138   /* Arguments for the module */
00139   PyObject *pTuple;
00140   pTuple = PyTuple_New(ND); /* new reference */
00141 
00142   PyObject *pVal=PETSC_NULL;
00143 
00144   PyTuple_SetItem(pTuple,0,PyFloat_FromDouble(time_ns)); /* The tuple steals the reference */
00145 
00146   /* Call the Python function */
00147   pVal = PyObject_CallObject(pFunc,pTuple);  /* new reference */
00148 
00149   PetscReal f;
00150   f=PyFloat_AsDouble(PyTuple_GetItem(pVal,0));
00151 
00152   /* decrement references */
00153   Py_DECREF(pFunc);
00154   Py_DECREF(pTuple);
00155   Py_DECREF(pVal);
00156 
00157   MagparFunctionInfoReturn(f);
00158 #else
00159   SETERRQ(PETSC_ERR_ARG_CORRUPT,"magpar not linked with Python interpreter. Cannot execute Python scripts!\n");
00160 #endif
00161 }
00162 
00163 int Hext_py(GridData *gdata,Vec VHextsum,PetscReal *hext)
00164 {
00165   MagparFunctionInfoBegin;
00166 
00167   /* to be implemented by the user */
00168 
00169   if (doinit) {
00170     ierr = Hext_py_Init(gdata);CHKERRQ(ierr);
00171     doinit=0;
00172   }
00173   if (!fieldon) {
00174     MagparFunctionInfoReturn(0);
00175   }
00176 
00177   ierr = VecAXPY(VHextsum,1.0,VHthis);CHKERRQ(ierr);
00178 
00179   /* get time in nanoseconds */
00180   PetscReal nstime;
00181   nstime=gdata->time*gdata->tscale*1e9;
00182 
00183   /* get scaling factor */
00184   PetscReal hext2;
00185 /* DEBUG */
00186 #if 0
00187   hext2=hext_f(gdata,nstime);
00188 #else
00189   hext2=0;
00190 #endif
00191 
00192   ierr = VecAXPY(VHextsum,hext2,VHthis);CHKERRQ(ierr);
00193 
00194   *hext=hextamp;
00195 
00196   MagparFunctionProfReturn(0);
00197 }
00198 

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