Skip to content

Commit aa2aa4b

Browse files
Implemented compute eff field in sim class. Simplified interactions array. Fixed circular dependency in sim and energy class headers
1 parent 5268274 commit aa2aa4b

File tree

5 files changed

+36
-22
lines changed

5 files changed

+36
-22
lines changed

fidimag/common/c_clib.pyx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ cdef class PyEnergy:
307307
return self.thisptr.compute_energy()
308308

309309
def add_interaction_to_sim(self, PyMicroSim sim):
310-
sim.thisptr.add_interaction(<void *> self.thisptr,
311-
self.thisptr.interaction_id)
310+
sim.thisptr.add_interaction(<Energy *> self.thisptr)
312311

313312
def get_interaction_id(self):
314313
return self._thisptr.interaction_id
@@ -375,8 +374,7 @@ cdef extern from "c_micro_sim.h":
375374
double *energy, double *field, int *pins
376375
)
377376

378-
void add_interaction(void * interaction, int int_id)
379-
void print_interactions_id()
377+
void add_interaction(Energy * interaction)
380378

381379

382380
cdef class PyMicroSim(object):
@@ -406,5 +404,3 @@ cdef class PyMicroSim(object):
406404

407405
def add_interaction(self, Interaction):
408406
Interaction.add_interaction_to_sim(self)
409-
self.thisptr.print_interactions_id()
410-

native/include/c_energy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class AnisotropyEnergy : public Energy {
4646
public:
4747
AnisotropyEnergy() {
4848
std::cout << "Instatiating AnisotropyEnergy class; at " << this << "\n";
49-
this->interaction_id = 1;
49+
this->interaction_id = UNIAXIAL_ANISOTROPY;
5050
};
5151
~AnisotropyEnergy() {std::cout << "Killing Anisotropy\n";};
5252
double *Ku;
@@ -64,7 +64,7 @@ class DMIEnergy : public Energy {
6464
public:
6565
DMIEnergy() {
6666
std::cout << "Instatiating DMIEnergy class; at " << this << "\n";
67-
this->interaction_id = 1;
67+
this->interaction_id = DMI;
6868
};
6969
~DMIEnergy() {std::cout << "Killing DMI\n";};
7070
double *D;

native/include/c_micro_sim.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
#include<cmath>
33
#include<iostream>
44
#include<vector>
5+
#include<memory>
56
// #define MAX_ENERGY_TERMS 20
67

8+
class Energy; // forward declaration; Energy is defined in c_energy.h
79
class MicroSim {
810
public:
911
MicroSim() {std::cout << "Instatiating MicroSim class; at " << this << "\n";};
@@ -28,22 +30,27 @@ class MicroSim {
2830
// Array with interactions
2931
// void * interactions;
3032
// int interactions_id[MAX_ENERGY_TERMS];
31-
std::vector<void *> interactions;
32-
std::vector<int> interactions_id;
33+
// Should we use a vector of weak_ptr? : std::vector<std::weak_ptr<Energy>> interactions;
34+
std::vector<Energy *> interactions;
35+
36+
// Not necessary at least we re creating an array of void pointers:
37+
// std::vector<int> interactions_id;
3338

3439
// Methods
3540
void setup(int nx, int ny, int nz, double dx, double dy, double dz,
36-
double unit_length, double *coordinates, int *ngbs,
37-
double *spin, double *Ms, double *Ms_inv,
41+
double unit_length, double *coordinates, int *ngbs,
42+
double *spin, double *Ms, double *Ms_inv,
3843
double *energy, double *field, int *pins
3944
);
4045

41-
void add_interaction(void * interaction, int int_id);
46+
void add_interaction(Energy * interaction);
47+
48+
// void print_interactions_id() {
49+
// for(int i : interactions_id) std::cout << i << "\n";
50+
// for(auto i : interactions) std::cout << i << "\n";
51+
// }
4252

43-
void print_interactions_id() {
44-
for(int i : interactions_id) std::cout << i << "\n";
45-
for(auto i : interactions) std::cout << i << "\n";
46-
}
53+
void compute_effective_field(double t);
4754
};
4855

4956
enum EnergyTermIDs {

native/src/c_micro_sim.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#include "c_energy.h"
12
#include "c_micro_sim.h"
23
#include "c_constants.h"
34

45

56
void MicroSim::setup(int nx, int ny, int nz, double dx, double dy, double dz,
6-
double unit_length, double *coordinates, int *ngbs,
7-
double *spin, double *Ms, double *Ms_inv,
7+
double unit_length, double *coordinates, int *ngbs,
8+
double *spin, double *Ms, double *Ms_inv,
89
double *energy, double *field, int *pins
910
) {
1011

@@ -29,9 +30,18 @@ void MicroSim::setup(int nx, int ny, int nz, double dx, double dy, double dz,
2930
set_up = true;
3031
}
3132

32-
void MicroSim::add_interaction(void * interaction_ptr, int int_id) {
33-
33+
void MicroSim::add_interaction(Energy * interaction_ptr) {
34+
3435
interactions.push_back(interaction_ptr);
35-
interactions_id.push_back(int_id);
36+
// interactions_id.push_back(int_id);
37+
38+
}
39+
40+
void MicroSim::compute_effective_field(double t) {
3641

42+
// Using indices
43+
std::vector<Energy *>::iterator it;
44+
for(it = interactions.begin(); it != interactions.end(); it++) {
45+
(*it)->compute_field(t);
46+
}
3747
}

native/src/m_dmi.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ void dmi_field(double * m, double * field,
373373

374374
void DMIEnergy::compute_field(double t) {
375375

376+
// Should use: std::unique_ptr<int[]> dxs ??
376377
/* These are for the DMI prefactor or coefficient */
377378
double dxs[6] = {dx, dx, dy, dy, dz, dz};
378379

0 commit comments

Comments
 (0)