hext_py.c

Go to the documentation of this file.
00001 /*
00002     This file is part of magpar.
00003 
00004     Copyright (C) 2002-2009 Werner Scholz, Amit Itagi
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: hext_py.c 2782 2009-10-16 20:37:45Z scholz $\n\n";
00025 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00026 
00027 #include "field.h"
00028 #include "util/util.h"
00029 
00030 static int       doinit=1;
00031 static int       fieldon=0;
00032 static Vec       VHthis;
00033 static PetscReal hextamp=0.0;
00034 
00035 int Hext_py_Init(GridData *gdata)
00036 {
00037   MagparFunctionLogBegin;
00038 
00039   PetscTruth flg;
00040   char str[256];
00041   ierr = PetscOptionsGetString(PETSC_NULL,"-hext_py",str,256,&flg);CHKERRQ(ierr);
00042   if (flg!=PETSC_TRUE) {
00043     fieldon=0;
00044     PetscPrintf(PETSC_COMM_WORLD,
00045       "Option -hext_py not found, using default value: %i (field off)\n",
00046       fieldon
00047     );
00048   }
00049   else {
00050     fieldon=1;
00051   }
00052 
00053   if (!fieldon) {
00054     PetscPrintf(PETSC_COMM_WORLD,"Hext_py off\n");
00055     MagparFunctionLogReturn(0);
00056   }
00057 
00058 #ifdef PYTHON
00059   PetscPrintf(PETSC_COMM_WORLD,"Hext_py on, using Python script: %s\n",str);
00060 
00061   ierr = VecDuplicate(gdata->M,&VHthis);CHKERRQ(ierr);
00062 
00063   /* Python stuff */
00064 
00065   PyObject *pFunc;
00066   pFunc=PyObject_GetAttrString(gdata->pModule,str);  /* new reference */
00067   if(!pFunc) {
00068     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Cannot find Python function %s!\n",str);
00069   }
00070   if (!PyCallable_Check(pFunc)) {
00071     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Python object %s is not callable (not a function)!\n",str);
00072   }
00073 
00074   /* Arguments for the module */
00075   PyObject *pTuple;
00076   pTuple = PyTuple_New(ND); /* new reference */
00077 
00078   PyObject *pVal=PETSC_NULL;
00079 
00080   /* make the vector of the external field accessible */
00081   PetscReal *ta_Hext;
00082   ierr = VecGetArray(VHthis,&ta_Hext);CHKERRQ(ierr);
00083 
00084   /* loop over all (local) nodes */
00085   for (int j=0; j<gdata->ln_vert; j++) {
00086     ierr = ProgressBar(j,gdata->ln_vert,10);
00087     PetscReal x,y,z;
00088     x=gdata->vertxyz[ND*gdata->vertl2g[j]+0];
00089     y=gdata->vertxyz[ND*gdata->vertl2g[j]+1];
00090     z=gdata->vertxyz[ND*gdata->vertl2g[j]+2];
00091 
00092     PyTuple_SetItem(pTuple,0,PyFloat_FromDouble(x)); /* The tuple steals the reference */
00093     PyTuple_SetItem(pTuple,1,PyFloat_FromDouble(y));
00094     PyTuple_SetItem(pTuple,2,PyFloat_FromDouble(z));
00095 
00096     /* Call the Python function */
00097     pVal = PyObject_CallObject(pFunc,pTuple);  /* new reference */
00098 
00099     /* set external field */
00100     for (int i=0; i<ND; i++) {
00101       ta_Hext[ND*j+i]=PyFloat_AsDouble(PyTuple_GetItem(pVal,i));
00102     }
00103   }
00104   ierr = ProgressBar(1,1,10);
00105   ierr = VecRestoreArray(VHthis,&ta_Hext);CHKERRQ(ierr);
00106 
00107   /* decrement references */
00108   Py_DECREF(pFunc);
00109   Py_DECREF(pTuple);
00110   Py_DECREF(pVal);
00111 
00112   int k;
00113   ierr = VecMax(VHthis,&k,&hextamp);CHKERRQ(ierr);
00114 #else
00115   SETERRQ(PETSC_ERR_ARG_CORRUPT,"magpar not linked with Python interpreter. Cannot execute Python scripts!\n");
00116 #endif
00117 
00118   MagparFunctionLogReturn(0);
00119 }
00120 
00121 
00122 int hext_f(GridData *gdata, PetscReal time_ns)
00123 {
00124   MagparFunctionInfoBegin;
00125 
00126 #ifdef PYTHON
00127   char str[256]="hext_f";
00128   PyObject *pFunc;
00129   pFunc=PyObject_GetAttrString(gdata->pModule,str);  /* new reference */
00130   if(!pFunc) {
00131     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Cannot find Python function %s!\n",str);
00132   }
00133   if (!PyCallable_Check(pFunc)) {
00134     SETERRQ1(PETSC_ERR_ARG_CORRUPT,"Python object %s is not callable (not a function)!\n",str);
00135   }
00136 
00137   /* Arguments for the module */
00138   PyObject *pTuple;
00139   pTuple = PyTuple_New(ND); /* new reference */
00140 
00141   PyObject *pVal=PETSC_NULL;
00142 
00143   PyTuple_SetItem(pTuple,0,PyFloat_FromDouble(time_ns)); /* The tuple steals the reference */
00144 
00145   /* Call the Python function */
00146   pVal = PyObject_CallObject(pFunc,pTuple);  /* new reference */
00147 
00148   PetscReal f;
00149   f=PyFloat_AsDouble(PyTuple_GetItem(pVal,0));
00150 
00151   /* decrement references */
00152   Py_DECREF(pFunc);
00153   Py_DECREF(pTuple);
00154   Py_DECREF(pVal);
00155 
00156   MagparFunctionInfoReturn(f);
00157 #else
00158   SETERRQ(PETSC_ERR_ARG_CORRUPT,"magpar not linked with Python interpreter. Cannot execute Python scripts!\n");
00159 #endif
00160 }
00161 
00162 int Hext_py(GridData *gdata,Vec VHextsum,PetscReal *hext)
00163 {
00164   MagparFunctionInfoBegin;
00165 
00166   /* to be implemented by the user */
00167 
00168   if (doinit) {
00169     ierr = Hext_py_Init(gdata);CHKERRQ(ierr);
00170     doinit=0;
00171   }
00172   if (!fieldon) {
00173     MagparFunctionInfoReturn(0);
00174   }
00175 
00176   ierr = VecAXPY(VHextsum,1.0,VHthis);CHKERRQ(ierr);
00177 
00178   /* get time in nanoseconds */
00179   PetscReal nstime;
00180   nstime=gdata->time*gdata->tscale*1e9;
00181 
00182   /* get scaling factor */
00183   PetscReal hext2;
00184 /* DEBUG */
00185 #if 0
00186   hext2=hext_f(gdata,nstime);
00187 #else
00188   hext2=0;
00189 #endif
00190 
00191   ierr = VecAXPY(VHextsum,hext2,VHthis);CHKERRQ(ierr);
00192 
00193   *hext=hextamp;
00194 
00195   MagparFunctionProfReturn(0);
00196 }
00197 

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