maginit.c

Go to the documentation of this file.
00001 /*
00002     This file is part of magpar.
00003 
00004     Copyright (C) 2002-2009 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: maginit.c 2681 2009-07-31 04:30:53Z scholz $\n\n";
00025 static char const Td[] = "$Today: " __FILE__ "  " __DATE__ "  "  __TIME__  " $\n\n";
00026 
00027 #include "init.h"
00028 
00029 int MagInit(GridData *gdata)
00030 {
00031   MagparFunctionLogBegin;
00032 
00033   int rank,size;
00034   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
00035   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
00036 
00037   /* TODO: rely on readkrn to broadcast n_prop -
00038      we need it as along as M is a shared vector during serial init.
00039   */
00040 
00041   PetscPrintf(PETSC_COMM_WORLD,"Creating Vec M for %i nodes\n",gdata->n_vert);
00042   PetscSynchronizedPrintf(PETSC_COMM_WORLD,
00043     "<%i>local nodes: %i  n_prop: %i\n",
00044     rank,gdata->ln_vert,gdata->n_prop
00045   );
00046   PetscSynchronizedFlush(PETSC_COMM_WORLD);
00047 
00048   ierr = VecCreate(PETSC_COMM_WORLD,&gdata->M);CHKERRQ(ierr);
00049   ierr = VecSetSizes(gdata->M,ND*gdata->ln_vert,PETSC_DECIDE);CHKERRQ(ierr);
00050   ierr = VecSetFromOptions(gdata->M);CHKERRQ(ierr);
00051   ierr = VecSetBlockSize(gdata->M,ND);CHKERRQ(ierr);
00052 
00053   int       *init_mag;
00054   PetscReal *init_magparm;
00055   ierr = PetscMalloc(gdata->n_prop*sizeof(int),&init_mag);CHKERRQ(ierr);
00056   ierr = PetscMalloc(gdata->n_prop*sizeof(PetscReal),&init_magparm);CHKERRQ(ierr);
00057 
00058   for (int i=0;i<gdata->n_prop;i++){
00059     init_mag[i]=3;
00060     init_magparm[i]=0.0;
00061   }
00062 
00063   int nmax = gdata->n_prop;
00064 
00065   PetscTruth flg;
00066   ierr = PetscOptionsGetIntArray(PETSC_NULL,"-init_mag",(PetscInt*)init_mag,(PetscInt*)&nmax,&flg);CHKERRQ(ierr);
00067   if (flg!=PETSC_TRUE) {
00068     init_mag[0]=3;
00069     PetscPrintf(PETSC_COMM_WORLD,
00070       "Option -init_mag not found, using default value: %i\n",
00071       init_mag[0]
00072     );
00073   }
00074   if (nmax<=1) {
00075     ierr = PetscOptionsGetReal(PETSC_NULL,"-init_magparm",init_magparm,&flg);CHKERRQ(ierr);
00076     if (flg!=PETSC_TRUE) {
00077       init_magparm[0]=0;
00078       PetscPrintf(PETSC_COMM_WORLD,
00079         "Option -init_magparm not found, using default value: %g\n",
00080         init_magparm[0]
00081       );
00082     }
00083 
00084     ierr = MagSet(-1,init_mag[0],init_magparm[0],gdata);CHKERRQ(ierr);
00085   }
00086   else if (nmax==gdata->n_prop) {
00087     nmax = gdata->n_prop;
00088     ierr = PetscOptionsGetRealArray(PETSC_NULL,"-init_magparm",init_magparm,(PetscInt*)&nmax,&flg);CHKERRQ(ierr);
00089     if (flg!=PETSC_TRUE) {
00090       PetscPrintf(PETSC_COMM_WORLD,
00091         "Option -init_magparm not found, using default value %g for all volumes\n",
00092         init_magparm[0]
00093       );
00094     }
00095     else if (nmax!=gdata->n_prop) {
00096       SETERRQ2(PETSC_ERR_ARG_INCOMP,
00097         "Wrong number of values for option -init_magparm! %i != %i\n",
00098         nmax,gdata->n_prop
00099       );
00100     }
00101     for (int i=0;i<gdata->n_prop;i++){
00102       ierr = MagSet(i,init_mag[i],init_magparm[i],gdata);CHKERRQ(ierr);
00103     }
00104   }
00105   else {
00106     SETERRQ2(PETSC_ERR_ARG_INCOMP,
00107       "Wrong number of values for option -init_mag! %i != %i\n",
00108       nmax,gdata->n_prop
00109     );
00110   }
00111 
00112   ierr = PetscFree(init_mag);CHKERRQ(ierr);
00113   ierr = PetscFree(init_magparm);CHKERRQ(ierr);
00114 
00115   MagparFunctionLogReturn(0);
00116 }

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